Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

AHKGooglePR - Calculate Google PageRank with native AHK code


  • Please log in to reply
6 replies to this topic
HuBa
  • Members
  • 175 posts
  • Last active: Feb 13 2012 09:51 AM
  • Joined: 24 Feb 2007
Oh, yes, I finally made it! :D
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:

Posted Image

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.

Zippo()
  • Guests
  • Last active:
  • Joined: --
Wonderful script! I'll use this all the time.

Thanks for sharing it :)

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

Wonderful script! I'll use this all the time.

Well, there is also the Google toolbar which does the same thing... But you might not want to install it just for that! :-)

Anyway, that's a very nice script.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Zippo()
  • Guests
  • Last active:
  • Joined: --

Well, there is also the Google toolbar which does the same thing... But you might not want to install it just for that! :-)


Exactly. I was using this just to get out of using the toolbar.

This script is a much nicer and cleaner solution :)

Icarus
  • Members
  • 851 posts
  • Last active: Jan 02 2012 11:17 AM
  • Joined: 24 Nov 2005
@HuBa - looks great.
I was just searching the web for pagerank code for a while, and stumbled upon some PHP code.

Just a minute before I started to do the porting to AHK myself, I figured someone here probably already made it!

Looking good, thanks for sharing.

EDIT:
Is this still working?
Any URL I put in the GUI gets an Invalid Pagerank error
Any request I make to GetPR() returns empty

EDIT2:
I think the problem may be in the UrlDownloadToVar
When I place a DownloadToFile there, I do get a downloaded HTML with pagerank in the format that is expected by the parsing code that follows.

EDIT3:
This works:
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)
    GoogleURL := "http://www.google.com/search?client=navclient-auto&ch=6" ch "&ie=UTF-8&oe=UTF-8&features=Rank&q=info:" url
    UrlDownloadToFile %GoogleUrl% , _tmp.html
    FileRead pr, _tmp.html
    FileDelete _tmp.html
    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
}


Didnt care to check why the DownloadToVar did not work.
Sector-Seven - Freeware tools built with AutoHotkey

DeWild1
  • Members
  • 369 posts
  • Last active: Feb 28 2014 08:15 PM
  • Joined: 30 Apr 2006
Here is one that tells you what page of a search you come up on.. first 1000 results...
Google has a way of blocking you if you do to many requests, that's why I have a long - random pause...
:lol:


#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
setbatchlines -1



 
Menu, Tray, Icon, shell32.dll, 147
menu, tray, NoStandard 
Menu, tray, add, Stop, MenuHandler2

 

InputBox, strSpam, Search String, Enter your search string EXACTLY as you would for a search. (ie. the keywords you want to be found through),, 350, 140,,,,, keywords 
StringReplace, strSpam, strSpam, %A_Space%, +, 1


InputBox, Needle, URL, Enter your web site address. (ie. www.yourwebsite.com),, 350, 140,,,,, www.yourwebsite.com
 
 
 





 FileDelete, garbage*.dat
 UrlDownloadtoFile, http://www.google.com/search?q=%strSpam%&btnG=Search&hl=en&sa=2, garbage46545645641.dat
  fileread, Haystack, garbage46545645641.dat

  
 FileDelete, garbage46545645641.dat
IfInString, Haystack, %Needle%
{
    MsgBox, %Needle% is on the FIRST Google search page!
	exitapp
   
	}
		counter := 1
loop
{
	
	Menu, tray, add, Stop, MenuHandler2
		Menu, tray, add, On page %counter% of 100, MenuHandler3
	if counter >= 100
    {
	msgbox, not found in the first 1000 results
	exitapp
	}
sleep, 2
random, rand6, 2001, 5000, NewSeed
random, rand7, 10, 99, NewSeed
sleep, 4
random, rand8, 336, 2906, NewSeed
random, rand10, 12, 660, NewSeed
sleep, 20
random, rand11, 77, 999, NewSeed
random, rand4, 700, 900, NewSeed
sleep, 23
random, rand5, 510, 700, NewSeed
sleep, %rand6%
random, rand9, 51, 700, NewSeed
random, rand12, 25, 760, NewSeed
random, rand13, 19, 25, NewSeed
sleep, 2
random, rand0, 10, 1400, NewSeed
sleep, 3
random, rand1, 1000, 1500, NewSeed
random, rand2, 1000, 2030, NewSeed
sleep, 20
random, rand3, 900, 1100, NewSeed

sleep, %rand6%

FileDelete, garbage%rand7%%rand8%.dat
sleep, %rand2%
strSpam2 = http://www.google.com/search?hl=en&q=%strSpam%&start=%counter%0&sa=N

sleep, %rand13%%rand9%
 UrlDownloadtoFile, %strSpam2%, garbage%rand7%%rand8%.dat
sleep, %rand3%
  fileread, Haystack, garbage%rand7%%rand8%.dat
counter += 1 
  sleep, %rand4%5
 FileDelete, garbage%rand7%%rand8%.dat
 sleep, %rand5%
IfInString, Haystack, %Needle%
{
    run, %strSpam2% 

    MsgBox, %Needle% is on Google search page %counter%. The URL is %strSpam2% 
	exitapp
    
	}




	sleep, %rand7%%rand8%
	menu, tray, DeleteAll

}



 ;///////////////////////////
MenuHandler3:
sleep, 100

MenuHandler2:
 FileDelete, garbage*.dat
  ExitApp
return 
MSN has no checker so its very fast...


#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
setbatchlines -1



 
Menu, Tray, Icon, shell32.dll, 147
menu, tray, NoStandard 
Menu, tray, add, Stop, MenuHandler2

 

InputBox, strSpam, Search String, Enter your search string EXACTLY as you would for a search. (ie. the keywords you want to be found through),, 350, 140,,,,, keywords 
StringReplace, strSpam, strSpam, %A_Space%, +, 1


InputBox, Needle, URL, Enter your web site address. (ie. www.yourwebsite.com),, 350, 140,,,,, www.yourwebsite.com
 
 
 


 







 FileDelete, garbage*.dat

 UrlDownloadtoFile,  http://search.live.com/results.aspx?q=%strSpam%&go=&form=QBRE, garbage46545645641.dat
  fileread, Haystack, garbage46545645641.dat
  
 FileDelete, garbage46545645641.dat
IfInString, Haystack, %Needle%
{
    MsgBox, %Needle% is on the FIRST MSN search page!
	exitapp
   
	}
		counter := 1
loop
{
	
	Menu, tray, add, Stop, MenuHandler2
		Menu, tray, add, On page %counter% of 100, MenuHandler3
	if counter >= 100
    {
	msgbox, not found in the first 1000 results
	exitapp
	}


random, rand10, 12, 660, NewSeed
strSpam2 = http://search.msn.com/results.aspx?q=%strSpam%&first=%counter%1&FORM=PERE2

 UrlDownloadtoFile, %strSpam2%, garbage%rand10%.dat

  fileread, Haystack, garbage%rand10%.dat
counter += 1 
  
 FileDelete, garbage%rand10%.dat
IfInString, Haystack, %Needle%
{
    run, %strSpam2% 
	
    MsgBox, %Needle% is on MSN search page %counter%. The URL is %strSpam2% 

	exitapp
    
	}
	menu, tray, DeleteAll

}



 ;///////////////////////////
MenuHandler3:
sleep, 100

MenuHandler2:
 FileDelete, garbage*.dat
  ExitApp
return 


kaka
  • Members
  • 76 posts
  • Last active: Jul 02 2018 05:24 AM
  • Joined: 05 Jan 2007
Hi Guys,

i download it and run it. But its giving me This Error


Invalid PageRank:
202.69.38.235)<br><br>
:?: :(
Posted Image