Up ảnh nhanh lên imgur và tự lấy link ảnh

N.E.M
8/9/2013 7:27Phản hồi: 1
Up ảnh nhanh lên imgur và tự lấy link ảnh
Công dụng dùng để up một hoặc nhiều bức ảnh lên trang chia sẻ ảnh imgur.com để lấy link ảnh đó, đoạn code sau sẽ tự up và tự lấy link ảnh cho các bạn.


Để up ảnh lên imgur thì có 2 loại là up ảnh nặc danh có nghĩa là không vào tài khoản của mình, và up ảnh có định danh nghĩa là vào tài khoản của mình.
Ở đây thì mình chỉ cách up định danh. Ảnh sẽ vào album tài khoản của các bạn cho dễ quản lý.

Đầu tiên thì cần phải có 1 tài khoản trên imgur.com, sau đó các bạn vào https://imgur.com/register/api_anon để đăng kí sửa dụng hàm api


Các bạn chọn như trong hình phần application name và description có thể điền tuỳ thích.
Sau khi đăng kí xong các bạn sẽ có 2 dãy biến là Client ID và Client secret.
Vào trình duyệt paste đoạn url sau:
Code:
https://api.imgur.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=token
Thay YOUR_CLIENT_ID bằng dạy chữ số Client ID mà các bạn có được khi đăng kí api.
Nhấn enter và lúc này trang imgur sẽ hỏi các bạn có cấp phép cho ứng dụng vừa rồi của các bạn kết nối tới tài khoản của bạn không. Nhấn Allow để cấp phép.


Khi nhấn Allow trang web sẽ trả về 1 đoạn link trên trình duyệt các bạn, vd như của mình sẽ là:
Code:
http://example.com/#access_token=d86109136f0f8419181f5f06d65c92f54f616d00&expires_in=3600&token_type=bearer&refresh_token=256306a996f9007e73a06cec346b31082c244d4f&account_username=nemtabi
Các bạn chú ý phần refresh_token= XXXXX &account_username ( các bạn copy phần XXX này nhớ là tới & thôi đừng copy &, vd của mình sẽ là 256306a996f9007e73a06cec346b31082c244d4f

Ok phần khó nhất đã xong, giờ vào cái chính.
Các bạn mở chương trình Applescript Editor
Paste đoạn code sau vào đó.

Code:
property Client_ID : "XXXXXXX"
property Client_Secret : "XXXXXXX"
property refresh_token : "XXXXXXX"
 
-- Supported file types
property fileTypes : {"jpeg", "jpg", "gif", "png", "apng", "tiff", "bmp", "pdf", "xcf"}
 
-- If true, this will enclose the URL in a [img][/img] tag
property img_tag : true
 
property listOfLinks : {}
 
on run
    -- Verify the selected file is a supported file type
    tell application "Finder"
        set userSelect to (selection as alias list)
        if (count of userSelect) < 1 then
            tell me to badChoice()
        else if name extension of first item of userSelect is not in fileTypes then
            tell me to badChoice()
        end if
    end tell
 
    --Request new token
    set a to do shell script "curl -X POST -F client_id=" & Client_ID & " -F client_secret=" & Client_Secret & " -F grant_type=refresh_token -F refresh_token=" & refresh_token & " https://api.imgur.com/oauth2/token"
    set {TID, text item delimiters} to {text item delimiters, {"\"access_token\":\"", "\",\"expires_in"}}
    set new_token to text item 2 of a
    set text item delimiters to TID
    -- Upload the file
    repeat with i from 1 to number of items of the userSelect
        set selectedItem to item i of the userSelect
        set theFile to quoted form of POSIX path of selectedItem
        set imgurUpload to do shell script "curl -X POST -H 'Authorization: Bearer " & new_token & "' -F image=@" & theFile & " https://api.imgur.com/3/upload "
   
        -- Copy the URL into your clipboard
        set {TID, text item delimiters} to {text item delimiters, {"http:\\/\\/i.imgur.com\\/", "\"},\"success\":true,\"status\":200}"}}
        if img_tag then
            copy "[img]http://i.imgur.com/" & text item 2 of imgurUpload & "[/img]" & "\n" to the end of listOfLinks
        else
            copy "http://i.imgur.com/" & text item 2 of imgurUpload & "\n" to the end of listOfLinks
        end if
   
        -- Reset delimiters
        set text item delimiters to TID
    end repeat
    set the clipboard to listOfLinks as string
    set listOfLinks to {}
end run
 
on badChoice()
    using terms from application "Finder"
        tell me
            activate
            display alert "Please select ONE supported file and try again." message "Allowed file types: JPEG, JPG, GIF, PNG, APNG, TIFF, BMP, PDF, XCF (GIMP). " & return & return & "Please note that the TIFF, BMP, PDF and XCF formats will be converted to PNG on upload." buttons "OK" cancel button "OK"
        end tell
    end using terms from
end badChoice
Các bạn thay lần lượt dãy chữ số của Client ID, Client secret và refresh_token vào phần XXXX ở đầu.
Sau đó chọn export trong chương trình Applescript editor và chọn File Format là Application rồi save lại, cuối cùng kéo ứng dụng này lên thanh toolbar của finder, khi nào muốn up ảnh nào chỉ việc chọn ảnh đó và click vào biểu tượng của chương trình này, nó sẽ chạy khi nào xong tự tắt khi đó link ảnh sẽ được copy và clipboard.

Quảng cáo


À nếu muốn bỏ tag
Code:
[IMG] [/IMG]
trong link thì đổi property img_tag : true thành false nhé.
1 bình luận
Chia sẻ

Xu hướng

huyaikido
ĐẠI BÀNG
6 năm
Mình dùng windows thì làm sao làm được nhỉ?

Xu hướng

Bài mới









  • Chịu trách nhiệm nội dung: Trần Mạnh Hiệp
  • © 2024 Công ty Cổ phần MXH Tinh Tế
  • Địa chỉ: Số 70 Bà Huyện Thanh Quan, P. Võ Thị Sáu, Quận 3, TPHCM
  • Số điện thoại: 02822460095
  • MST: 0313255119
  • Giấy phép thiết lập MXH số 11/GP-BTTTT, Ký ngày: 08/01/2019