Advertisement
Guest User

[Fixed] AutoIt-Imgur-UDF

a guest
Aug 7th, 2016
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.56 KB | None | 0 0
  1. #include 'include/Base64.au3' ; by Ward
  2. #include 'include/JSON.au3' ; by Ward
  3.  
  4. ; #INDEX# =======================================================================================================================
  5. ; Title .........: Imgur
  6. ; Version....: 1.1.0
  7. ; Description ...: Upload images using Imgur API v3
  8. ; Author(s) .....: Juno_okyo
  9. ; Editor(s) .....: Mattias Ghodsian
  10. ; ===============================================================================================================================
  11.  
  12. Global Const $IMGUR_API_ENDPOINT = 'https://api.imgur.com/3/upload.json'
  13. Global Const $client_id = '' ; your client id here
  14.  
  15. Func _imgur_upload($sFile, $sType = 'base64')
  16.     Local $fp, $sData, $sPostData, $sRequest
  17.  
  18.     If $sType = 'base64' Then
  19.         If Not FileExists($sFile) Then
  20.             SetError(1)
  21.             Return False
  22.         EndIf
  23.  
  24.         $fp = FileOpen($sFile, 16)
  25.         $sData = FileRead($fp)
  26.         FileClose($fp)
  27.  
  28.         $sData = _Base64Encode($sData)
  29.     EndIf
  30.  
  31.    $sPostData = 'type=' & _url_encode($sType) & _
  32.         '&image=' & _url_encode($sData)
  33.  
  34.    $sRequest = _simple_post_request($IMGUR_API_ENDPOINT, $sPostData)
  35.    Local $json = Json_Decode($sRequest)
  36.  
  37.    ; return Json_Get($json, '["data"]["link"]') ; url to file with filetype
  38.    return 'http://imgur.com/' & Json_Get($json, '["data"]["id"]') ; url to file view
  39. EndFunc
  40.  
  41. Func _imgur_remote_upload($sURL)
  42.     Return _imgur_upload($sURL, 'url')
  43. EndFunc
  44.  
  45. #Region <INTERNAL_USE_ONLY>
  46. Func _url_encode($vData)
  47.     If IsBool($vData) Then Return $vData
  48.     Local $aData = StringToASCIIArray($vData, Default, Default, 2)
  49.     Local $sOut = '', $total = UBound($aData) - 1
  50.     For $i = 0 To $total
  51.         Switch $aData[$i]
  52.             Case 45, 46, 48 To 57, 65 To 90, 95, 97 To 122, 126
  53.                 $sOut &= Chr($aData[$i])
  54.             Case 32
  55.                 $sOut &= '+'
  56.             Case Else
  57.                 $sOut &= '%' & Hex($aData[$i], 2)
  58.         EndSwitch
  59.     Next
  60.     Return $sOut
  61. EndFunc
  62.  
  63. Func _simple_post_request($url, $postData)
  64.     If Not $url Or Not $postData Then Return False
  65.  
  66.     Local $oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
  67.     $oHTTP.Option(6) = False
  68.     $oHTTP.Open('post', $url, False)
  69.     $oHTTP.SetRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; rv:34.0) Gecko/20100101 Firefox/34.0')
  70.     $oHTTP.SetRequestHeader('Referer', 'http://imgur.com/')
  71.     $oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  72.     $oHTTP.SetRequestHeader('Authorization', 'Client-ID ' & $client_id)
  73.     $oHTTP.SetRequestHeader('Content-Length', StringLen($postData))
  74.     $oHTTP.Send($postData)
  75.     $oHTTP.WaitForResponse
  76.     Return $oHTTP.Responsetext
  77.     ConsoleWrite($oHTTP.Responsetext)
  78. EndFunc
  79. #EndRegion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement