-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_UploadToImgur.ahk
66 lines (52 loc) · 1.48 KB
/
class_UploadToImgur.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
; Link: https://gist.github.com/tmplinshi/352830d69874dd70ef0c8ea04ab8a732
; Author:
; Date:
; for: AHK_L
; Create Imgurl ClientID -- https://api.imgur.com/oauth2/addclient
/*
ImageFile := "D:\Desktop\test.gif"
try {
result := UploadToImgur(ImageFile)
MsgBox, 64, Upload Successful!, % result
} catch errMsg {
MsgBox, 48, Error, % errMsg
}
*/
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
}
}