Need .Net method to compute a Google Pagerank request checksum.
- by Steve K
The company I work for is currently developing a SEO tool which needs to include a domain or url Pagerank. It is possible to retrieve such data directly from Google by sending a request to the url called by the Google ToolBar. On of the parameters send to that url is a checksum of the domain whose pagerank is being requested. I have found multiple .Net methods for calculating that check sum; however, every one randomly returns corrupt values every so often. I can only handle errors to a certain point before my final data set becomes useless. I know that there are countless tools out there, from browser plugins to desktop applications, that can process page rank, so it can't be impossible.
My question, then, is two fold:
1) Any anyone heard of the problem I am having? (specifically in .Net) If so, how can it (or has it) be resolved?
2) Is there a better source for retrieving Pagerank data?
Below is the Url and checksum code I have been using.
"http://toolbarqueries.google.com/search?client=navclient-auto&ie=UTF-8&oe=UTF-8&features=Rank:&q=info:" & strUrl & "ch=" & strCheckSum
where:
strUrl = the url being queried
strCheckSum = CheckHash(GetHash(url)) (see code below)
Any help would be greatly appreciated.
''' <summary>
''' Returns a hash-string from the site's URL
''' </summary>
''' <param name="_SiteURL">full URL as indexed by Google</param>
''' <returns>HASH for site as a string</returns>
Private Shared Function GetHash(ByVal _SiteURL As String) As String
Try
Dim _Check1 As Long = StrToNum(_SiteURL, 5381, 33)
Dim _Check2 As Long = StrToNum(_SiteURL, 0, 65599)
_Check1 >>= 2
_Check1 = ((_Check1 >> 4) And 67108800) Or (_Check1 And 63)
_Check1 = ((_Check1 >> 4) And 4193280) Or (_Check1 And 1023)
_Check1 = ((_Check1 >> 4) And 245760) Or (_Check1 And 16383)
Dim T1 As Long = ((((_Check1 And 960) << 4) Or (_Check1 And 60)) << 2) Or (_Check2 And 3855)
Dim T2 As Long = ((((_Check1 And 4294950912) << 4) Or (_Check1 And 15360)) << 10) Or (_Check2 And 252641280)
Return Convert.ToString(T1 Or T2)
Catch
Return "0"
End Try
End Function
''' <summary>
''' Checks the HASH-string returned and adds check numbers as necessary
''' </summary>
''' <param name="_HashNum">generated HASH-string</param>
''' <returns>modified HASH-string</returns>
Private Shared Function CheckHash(ByVal _HashNum As String) As String
Try
Dim _CheckByte As Long = 0
Dim _Flag As Long = 0
Dim _tempI As Long = Convert.ToInt64(_HashNum)
If _tempI < 0 Then
_tempI = _tempI * (-1)
End If
Dim _Hash As String = _tempI.ToString()
Dim _Length As Integer = _Hash.Length
For x As Integer = _Length - 1 To 0 Step -1
Dim _quick As Char = _Hash(x)
Dim _Re As Long = Convert.ToInt64(_quick.ToString())
If 1 = (_Flag Mod 2) Then
_Re += _Re
_Re = CLng(((_Re \ 10) + (_Re Mod 10)))
End If
_CheckByte += _Re
_Flag += 1
Next
_CheckByte = _CheckByte Mod 10
If 0 <> _CheckByte Then
_CheckByte = 10 - _CheckByte
If 1 = (_Flag Mod 2) Then
If 1 = (_CheckByte Mod 2) Then
_CheckByte >>= 1
End If
End If
End If
If _Hash.Length = 9 Then
_CheckByte += 5
End If
Return "7" + _CheckByte.ToString() + _Hash
Catch
Return "0"
End Try
End Function
''' <summary>
''' Converts the string (site URL) into numbers for the HASH
''' </summary>
''' <param name="_str">Site URL as passed by GetHash()</param>
''' <param name="_Chk">Necessary passed value</param>
''' <param name="_Magic">Necessary passed value</param>
''' <returns>Long Integer manipulation of string passed</returns>
Private Shared Function StrToNum(ByVal _str As String, ByVal _Chk As Long, ByVal _Magic As Long) As Long
Try
Dim _Int64Unit As Long = Convert.ToInt64(Math.Pow(2, 32))
Dim _StrLen As Integer = _str.Length
For x As Integer = 0 To _StrLen - 1
_Chk *= _Magic
If _Chk >= _Int64Unit Then
_Chk = (_Chk - (_Int64Unit * Convert.ToInt64(_Chk \ _Int64Unit)))
_Chk = IIf((_Chk < -2147483648), (_Chk + _Int64Unit), _Chk)
End If
_Chk += CLng(Asc(_str(x)))
Next
Catch
End Try
Return _Chk
End Function