Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Created April 22, 2021 21:06
Show Gist options
  • Save JoeGlines/31189f708b20e4e3dbcc6db5d0b53e8f to your computer and use it in GitHub Desktop.
Save JoeGlines/31189f708b20e4e3dbcc6db5d0b53e8f to your computer and use it in GitHub Desktop.
;*******************************************************
; Want a clear path for learning AutoHotkey; Take a look at our AutoHotkey Udemy courses. They're structured in a way to make learning AHK EASY
; Right now you can get a coupon code here: https://the-Automator.com/Learn
;*******************************************************
#SingleInstance,Force
Blog_URI:="https://the-Automator.com" ;note, even if WordPress is in another folder, use this
IniRead, API_Key,Auth.ini,API, Key ;API Key is your key to Akismet
;*******************************************************
;*********Make sure you have not turned-off your API access in WordPress!******
;~ some documentation here: https://stats.wordpress.com/csv.php
;~ period = days,week,month
;~ limit = deault is 100 -1 is unlimitedIniRead, API_Token ,Auth.ini,API, Token
;~ table = One of views, postviews, referrers, referrers_grouped, searchterms, clicks, videoplays.
;********************Views***********************************
If (){
oAHK:=API_Call({"api_key":api_key,"blog_uri":Blog_URI,"table":"views","period":"days","format":"json"})
data:="Views`tDate`n"
for k, v in oAHK
data.=v.views "`t" v.date "`n"
LV_Table(Clipboard:=data,delimiter:="`t",UseHeader:=1,Title:="Views")
}
;********************Refererrs***********************************
If (0){
oAHK:=API_Call({"api_key":api_key,"blog_uri":Blog_URI,"table":"referrers","period":"month","days":"-1","format":"json"})
data:="Views`tReferrers`n"
for k, v in oAHK.1.referrers
data.=v.views "`t" v.referrer "`n"
LV_Table(Clipboard:=data,delimiter:="`t",UseHeader:=1,Title:="Search terms")
}
;********************clicks***********************************
If (0){
oAHK:=API_Call({"api_key":api_key,"blog_uri":Blog_URI,"table":"clicks","period":"month","days":"30","format":"json"}) ;,"summarize":""
data:="Clicks`tURL`n"
for a, b in oAHK
for c,d in b.clicks
data.=d.views "`t" d.click "`n"
LV_Table(Clipboard:=data, delimiter:="`t",UseHeader:=1,Title:="Clicks") ;Still need to aggregate numbers
}
;********************Search***********************************
If (1){
oAHK:=API_Call({"api_key":api_key,"blog_uri":Blog_URI,"table":"searchterms","period":"month","days":"-1","format":"json"})
data:="Views`tSearch Terms`n"
for k, v in oAHK
for a,b in v.SearchTerms{
if (b.SearchTerm !="encrypted_search_terms")
data.=b.views "`t" b.searchterm "`n"
}
LV_Table(Clipboard:=data,delimiter:="`t",UseHeader:=1,Title:="Search terms")
}
;********************API Call***********************************
API_Call(QS){
QS:=QSB(QS)
HTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1") ;Create COM Object
HTTP.Open("GET","http://stats.wordpress.com/csv.php" QS ) ; "&summarize" this will return summary of metric
HTTP.SetRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
HTTP.Send() ;If POST request put data in "Payload" variable
oAHK:=ParseJSON(HTTP.ResponseText) ;Make sure the ParseJSON function is in your library
return oAHK
}
;********************Query String Builder***********************************
QSB(x){
for a,b in x
String.=(A_Index=1?"?":"&") a "=" b
return String
}
;********************Shove inot List View***********************************
LV_Table(Data_Source, delimiter="`t",UseHeader=1,Title=""){ ; default delimiter set to tab
if FileExist(Data_Source) ;if file exists use it as source
FileRead, Data_Source, %Data_Source% ;read in and store as variable
;***********parse the data in variable and store in object*******************
data_obj := StrSplit(Data_Source,"`n","`r") ;parse earch row and store in object
rowHeader:=StrReplace(data_obj.RemoveAt(1),Delimiter,"|",Numb_Columns) ; Remove header from Object and convert to pipe delimited
if (useHeader=0){
loop, % Numb_Columns+1
RH.="Col_" A_Index "|"
rowHeader:=RH
}
dCols:= (Numb_Columns<8) ? 400: ((Numb_Columns<80) ? 650 : 1100) ;if cols <10 use 400; if cols <80 use 650 ; else use 1100
dRows:= (data_obj.MaxIndex() >27) ? 26 : data_obj.MaxIndex() ;if rows >27 use 26 else use # of rows
Gui, Table_View: New,,%Title% ;create new gui window and set title
Gui, Add, ListView, w%dCols% r%dRows% grid , % rowHeader ;set headers
For Each, Row In data_obj ;add the data lines to the ListView
LV_Add("", StrSplit(Row, Delimiter)*) ;LV_Add is a variadic function
Gui, Table_View:Show
}
;********************Maestrit Parse JSON***********************************
ParseJSON(jsonStr){
static SC:=ComObjCreate("ScriptControl"),C:=Chr(125)
SC.Language:="JScript",ComObjError(0),SC.ExecuteStatement("function arrangeForAhkTraversing(obj){if(obj instanceof Array){for(var i=0; i<obj.length; ++i)obj[i]=arrangeForAhkTraversing(obj[i]);return ['array',obj];" C "else if(obj instanceof Object){var keys=[],values=[];for(var key in obj){keys.push(key);values.push(arrangeForAhkTraversing(obj[key]));" C "return ['object',[keys,values]];" C "else return [typeof obj,obj];" C ";obj=" jsonStr)
return convertJScriptObjToAhks(SC.Eval("arrangeForAhkTraversing(obj)"))
}ConvertJScriptObjToAhks(JSObj){
if(JSObj[0]="Object"){
Obj:=[],Keys:=JSObj[1][0],Values:=JSObj[1][1]
while(A_Index<=Keys.length)
Obj[Keys[A_Index-1]]:=ConvertJScriptObjToAhks(Values[A_Index-1])
return Obj
}else if(JSObj[0]="Array"){
Array:=[]
while(A_Index<=JSObj[1].length)
Array.Push(ConvertJScriptObjToAhks(JSObj[1][A_Index-1]))
return Array
}else
return JSObj[1]
}
</code>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment