I know it is not that kind of script that people would use every day, but good to know.
It was an experiment, a proof-of-concept to show the power of AutoHotkey 8)
How to calculate and retrieve Google PageRank value?
The idea came from this PHP script.
I thought it will be an easy job to convert PHP sourcecode to AHK...
Well, definitely NOT!
AHK handles the binary operations in a different way then C or PHP.
AHK uses 64 bit length for integer variables, while PHP only 32 bit.
I had to simulate this limitation in AHK code to perform the original binary operations.
It lasted 3 days to debug all of the traps.
After I finished the GetPR() function, I made a Gui framework to the project to demonstrate the usage:
And here is the script (AHKGooglePR.ahk):
#Include URLDownloadToVar.ahk ; URLDownloadToVar function by olfen ; You can download it from http://www.autohotkey.com/forum/topic10466.html NormalizeTo32bit(Value) ; Helper function, simulates 32 bit binary operation overflow { return Mod(Value, 0x100000000) } NormalizeTo31bit(ByRef Value) ; Helper function, simulates 32 bit binary operation overflow { Value := NormalizeTo32bit(Value) if Abs(Value) >= 0x100000000 >> 1 if Value >= 0x100000000 >> 1 Value -= 0x100000000 else Value += 0x100000000 } PHPShiftRight(ByRef ShiftVar, ShiftValue) ; 32 bit shift { ShiftVar := NormalizeTo32bit(ShiftVar) ShiftVar >>= ShiftValue ShiftVar := NormalizeTo32bit(ShiftVar) } PHPShiftLeftXOR(ByRef XORVar, ShiftVar, ShiftValue) ; 32 bit shift and xor { XORVar := NormalizeTo32bit(XORVar) XORVar ^= NormalizeTo32bit(NormalizeTo32bit(ShiftVar) << ShiftValue) NormalizeTo31bit(XORVar) } zeroFill(a, b) { z := 0x80000000 if z & a { PHPShiftRight(a, 1) a &= ~z a |= 0x40000000 PHPShiftRight(a, b-1) } else PHPShiftRight(a, b) return NormalizeTo32bit(a) } mixValues(ByRef a, ByRef b, ByRef c) { a -= b a -= c a ^= zeroFill(c, 13) NormalizeTo31bit(a) b -= c b -= a PHPShiftLeftXOR(b, a, 8) c -= a c -= b c ^= zeroFill(b, 13) NormalizeTo31bit(c) a -= b a -= c a ^= zeroFill(c, 12) NormalizeTo31bit(a) b -= c b -= a PHPShiftLeftXOR(b, a, 16) c -= a c -= b c ^= zeroFill(b, 5) NormalizeTo31bit(c) a -= b a -= c a ^= zeroFill(c, 3) NormalizeTo31bit(a) b -= c b -= a PHPShiftLeftXOR(b, a, 10) c -= a c -= b c ^= zeroFill(b, 15) NormalizeTo31bit(c) } GCH(url, init=0xE6359A60) ; GMAG = 0xE6359A60 { length := StrLen(url) Loop %length% ;converts a string into an array of integers containing the numeric value of the char Array%A_Index% := Asc(SubStr(url, A_Index, 1)) a := 0x9E3779B9 b := a c := init k := 0 len := length Loop { if len < 12 Break k++ a += Array%k% k++ a += Array%k%<<8 k++ a += Array%k%<<16 k++ a += Array%k%<<24 k++ b += Array%k% k++ b += Array%k%<<8 k++ b += Array%k%<<16 k++ b += Array%k%<<24 k++ c += Array%k% k++ c += Array%k%<<8 k++ c += Array%k%<<16 k++ c += Array%k%<<24 mixValues(a, b, c) len -= 12 } c += length ; all the case statements fall through t := k+11 if len >= 11 c += Array%t%<<24 t-- if len >= 10 c += Array%t%<<16 t-- if len >= 9 c += Array%t%<<8 t-- ; the first byte of c is reserved for the length if len >= 8 b += Array%t%<<24 t-- if len >= 7 b += Array%t%<<16 t-- if len >= 6 b += Array%t%<<8 t-- if len >= 5 b += Array%t% t-- if len >= 4 a += Array%t%<<24 t-- if len >= 3 a += Array%t%<<16 t-- if len >= 2 a += Array%t%<<8 t-- if len >= 1 a += Array%t% ; case 0: nothing left to add mixValues(a, b, c) ;-------------------------------------------- report the result return c } GetPR(url) { ch := GCH("info:" url) ; http://www.autohotkey.com -> ch := "-612422425" StringReplace url, url, :, `%3A, All ; similar to urlencode PHP function StringReplace url, url, /, `%2F, All ; similar to urlencode PHP function pr := UrlDownloadToVar("http://www.google.com/search?client=navclient-auto&ch=6" ch "&ie=UTF-8&oe=UTF-8&features=Rank&q=info:" url) Loop Parse, pr, `n, `r IfInString A_LoopField, : ; Example: pr = "Rank_1:1:6" return SubStr(A_LoopField, InStr(A_LoopField, ":", true, 0) + 1) return "" ; Return an empty string in case of invalid result }Many thanx to olfen for making URLDownloadToVar function.
And here is the script for the Gui:
#Include AHKGooglePR.ahk #NoTrayIcon IfExist google.ico Menu Tray, Icon, google.ico Gui Add, Button, gPRCalc x186 y20 w130 h30 Default, &Calculate PageRank Gui Add, Button, gPRPasteNCalc x26 y20 w130 h30, &Paste && Calc PR Gui Add, Text, x26 y70 w160 h20, &URL: Gui Add, ComboBox, vPRComboBox x56 y67 w260 h10 r8 HwndCB Gui Add, Text, vPRankLabel x26 y100 w160 h32 Hidden, PageRank: Gui Add, Text, vPRankText x170 y100 w200 h32 Hidden Gui Add, Progress, vPRProgress x26 y140 w290 h20 cGreen Hidden Gui Font, s18, Verdana GuiControl Font, PRankLabel Gui Font, cGreen Bold, Verdana GuiControl Font, PRankText Gui Show, w350 h180, AHKGooglePR - Calculate Google PageRank IfInString Clipboard, :// GuiControl Text, PRComboBox, %Clipboard% else GuiControl Text, PRComboBox, http://www.autohotkey.com Return PRPasteNCalc: GuiControl Text, PRComboBox, %Clipboard% GoSub PRCalc Return PRCalc: GuiControlGet pr_url, , PRComboBox IfNotInString pr_url, :// { MsgBox 16, Error, Invalid URL: "%pr_url%" Return } SplashTextOn, , , Working... PageRank := GetPR(pr_url) SplashTextOff if PageRank not between 0 and 10 { MsgBox 16, Error, Invalid PageRank:`n%PageRank% Return } ControlGet URLList, List, , ComboBox1 pr_AlreadyAdded := False Loop Parse, URLList, `n if (pr_url = A_LoopField) { pr_AlreadyAdded := True Break } if not pr_AlreadyAdded GuiControl, , PRComboBox, %pr_url% GuiControl, , PRProgress, % PageRank * 10 GuiControl Show, PRProgress GuiControl, , PRankText, %PageRank% / 10 GuiControl Show, PRankLabel GuiControl Show, PRankText Return GuiClose: ExitApp
The full project can be downloaded from HERE.
You can read more about the PageRank technology at Google or at Wikipedia.