Today I had a thought that it is possible to programmatically query the Google page rank. As we all know PageRank is an objective measurement of the importance of web pages. Because it is important, I had to periodically check page rank by visiting some sites who gives free service to check your PageRank.
While surfing I had seen it done in PHP (seen in Google Community site), but I don't have PHP skills to learn/translate it. So I decided to convert the PHP sample into a .NET component, so that I can use it in my applications.
For that I came across the Microsoft's PHP to ASP.NET Migration Assistant. Below is the final CSharp class which you can use it to create the assembly.
// To get Google Page Rank in .NET
// Created/Last Changed: 12/5/2005
// By: Harsha @ infosrama.com
// Note: there is always a better way to do it.....
using System;
using System.IO;
using System.Net;
namespace infosramadotnet
{
///
/// Summary description for Class1.
///
public class GooglePageRank
{
private static uint GOOGLE_MAGIC = 0xE6359A60;
// The main function to get the page Rank
#region GetRank
public string GetRank(string SiteUrl)
{
try
{
string urlToCall = GetUrl(SiteUrl);
string serverResponse = GetResponse(urlToCall);
string[] response = serverResponse.Split(':');
return response[2] + " infosrama";
}
catch(Exception ex)
{
throw new ApplicationException("REQUEST FAILED");
}
}
#endregion
#region GetUrl
private string GetUrl(string SiteUrl)
{
string url = "info:" + SiteUrl;
int ch = GoogleCH(StrOrdinals(url));
return "http://www.google.com/search?client=navclient-auto&ch=6"
+ ch.ToString()
+ "&features=Rank&q="
+ url;
}
#endregion
#region StrOrdinals
private int[] StrOrdinals(string inputString)
{
int lengthOfString = inputString.Length;
int[] result = new int[lengthOfString];
for (int i = 0; i < lengthOfString; i++)
{
result[i] = (int)inputString[i];
}
return result;
}
#endregion
private int zeroFill(int a, int b)
{
uint z = 0x80000000;
if ((z & a) != 0)
{
a = (a >> 1);
a &= (int)(~z);
a |= (int)(0x40000000);
a = (a >> (b - 1));
}
else
{
a = (a >> b);
}
return a;
}
private int[] mix(int a, int b, int c)
{
a -= b; a -= c; a = (int)(a) ^ (zeroFill((int)(c), 13));
b -= c; b -= a; b = (int)(b) ^ ((int)(a) << 8);
c -= a; c -= b; c = (int)(c) ^ (zeroFill((int)(b), 13));
a -= b; a -= c; a = (int)(a) ^ (zeroFill((int)(c), 12));
b -= c; b -= a; b = (int)(b) ^ ((int)(a) << 16);
c -= a; c -= b; c = (int)(c) ^ (zeroFill((int)(b), 5));
a -= b; a -= c; a = (int)(a) ^ (zeroFill((int)(c), 3));
b -= c; b -= a; b = (int)(b) ^ ((int)(a) << 10);
c -= a; c -= b; c = (int)(c) ^ (zeroFill((int)(b), 15));
return new int[] { a, b, c };
}
private int GoogleCH(int[] url)
{
int a, b, c;
int k, length, len, init;
unchecked
{
init = (int)GooglePageRank.GOOGLE_MAGIC;
a = b = (int)0x9E3779B9;
}
int[] mix = new int[3];
length = url.Length;
c = init;
k = 0;
len = length;
while (len >= 12)
{
a += (url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
b += (url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24));
c += (url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24));
mix = mix(a, b, c);
a = mix[0]; b = mix[1]; c = mix[2];
k += 12;
len -= 12;
}
c += length;
switch (len)
{
case 11:
c += (url[k + 10] << 24);
goto case 10;
case 10:
c += (url[k + 9] << 16);
goto case 9;
case 9:
c += (url[k + 8] << 8);
goto case 8;
case 8:
b += (url[k + 7] << 24);
goto case 7;
case 7:
b += (url[k + 6] << 16);
goto case 6;
case 6:
b += (url[k + 5] << 8);
goto case 5;
case 5: b = b + (url[k + 4]);
goto case 4;
case 4:
a += (url[k + 3] << 24);
goto case 3;
case 3:
a += (url[k + 2] << 16);
goto case 2;
case 2:
a += (url[k + 1] << 8);
goto case 1;
case 1: a = a + (url[k + 0]);
break;
}
mix = mix(a, b, c);
return mix[2];
}
private string GetResponse(string url)
{
WebRequest request = WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
}
}