Skip to content

Instantly share code, notes, and snippets.

@G33kDude
Last active July 29, 2017 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save G33kDude/4421950 to your computer and use it in GitHub Desktop.
Save G33kDude/4421950 to your computer and use it in GitHub Desktop.
Gist.ahk
Gist(Code, NewUser="", NewPass="", Title="AutoHotkey", Public="1")
{
static Basic, User, Pass
if (User != NewUser || Pass != NewPass) ; If new credentials
{
User := NewUser
Pass := NewPass
if (User && Pass) ; If not blank
Basic := Base64(User ":" Pass) ; Create new basic auth code
else
Basic := ; Don't auth (anonymous)
}
Public := Public ? "true" : "false"
Code := SanitizeJSON(Code)
JSON = {"public":"%Public%","files":{"%Title%":{"content":"%Code%"}}}
Github := ComObjCreate("WinHttp.WinHttpRequest.5.1")
Github.Open("POST", "https://api.github.com/gists")
if (Basic)
Github.SetRequestHeader("authorization", "basic " Basic)
Github.Send(JSON)
If !RegExMatch(Github.ResponseText, """html_url""\:""(.*?)""", Out)
throw Github.ResponseText
return Out1
}
Base64(string)
{ ; http://www.autohotkey.com/forum/viewtopic.php?t=5896
Loop Parse, string
{
If Mod(A_Index,3) = 1
buffer := Asc(A_LoopField) << 16
Else If Mod(A_Index,3) = 2
buffer += Asc(A_LoopField) << 8
Else {
buffer += Asc(A_LoopField)
out := out . Code(buffer>>18) . Code(buffer>>12) . Code(buffer>>6) . Code(buffer)
}
}
If Mod(StrLen(string),3) = 0
Return out
If Mod(StrLen(string),3) = 1
Return out . Code(buffer>>18) . Code(buffer>>12) "=="
Return out . Code(buffer>>18) . Code(buffer>>12) . Code(buffer>>6) "="
}
Code(i) ; <== Chars[i & 63], 0-base index
{
static Chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
StringMid i, Chars, (i&63)+1, 1
Return i
}
SanitizeJSON(J)
{
StringReplace, J, J, \, \\, All
StringReplace, J, J, `b, \b, All
StringReplace, J, J, `f, \f, All
StringReplace, J, J, `n, \n, All
StringReplace, J, J, `r, \r, All
StringReplace, J, J, `t, \t, All
StringReplace, J, J, `", \`", All
return J
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment