I would like to make a code doing this:
- upload one or several (let's say up to 10) images to Imgur (preferably without an Imgur account)
- apply a RegEx to the imgur links list to put them in BBCode image tags ()
I found this post by @tmplinshi that gives a code to achieve this: https://www.autohotkey.com/boards/viewtopic.php?t=15697 and I could use the clipboard content by adapting it like explained here: https://www.autohotkey.com/boards/viewtopic.php?p=196761#p196761
I tried to run this code after copying an image link (right click/copy image address):
Code: Select all
; Create Imgurl ClientID -- https://api.imgur.com/oauth2/addclient
UploadToImgur(FileName, ClientID = "fbf77ff49c42c8a") {
return UploadToImgur.DoIt(FileName, ClientID)
}
Class UploadToImgur {
DoIt(FileName, ClientID) {
postData := this.FileToStream(FileName)
header := {"Authorization": "Client-ID " ClientID}
whr := this.Http("POST", "https://api.imgur.com/3/image", postData, header)
if RegExMatch(whr.ResponseText, """link"":""\K[^""]+", result) {
return StrReplace(result, "\")
} else {
RegExMatch(whr.ResponseText, """error"":""\K[^""]+", errMsg)
throw, errMsg ? errMsg : "Unkown Error"
}
}
FileToStream(FileName) {
ADO := ComObjCreate("ADODB.Stream")
ADO.Type := 1 ; adTypeBinary
ADO.Open()
ADO.LoadFromFile(FileName)
Stream := ADO.Read()
ADO.Close()
return Stream
}
Http(Method, Url, PostData="", Obj_Headers="") {
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open(Method, Url, "True")
for k, v in Obj_Headers {
whr.SetRequestHeader(k, v)
}
if (Method = "POST") && !Obj_Headers["Content-Type"] {
whr.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
}
whr.Send(PostData)
whr.WaitForResponse()
return whr
}
}
ImageFile := clipboard
try {
result := UploadToImgur(ImageFile)
MsgBox, 64, Upload Successful!, % result
} catch errMsg {
MsgBox, 48, Error!, % errMsg
}
Maybe Imgur has changed its uploading methods since, or maybe I didn't understand how this code chooses between a registered ClientID or the "default" ID this function is said to provide.
Anyway, I cannot make it work myself, so any help using this code or another similar one would be verry appreciated.
Regards.