public string GetRank(string webSiteUrl)
   {
       string urlToCall = GetRankUrl(webSiteUrl);
       string serverResponse = RunUrl(urlToCall);

       string[] response = serverResponse.Split(':');
       if (response.Length <= 1)
           return null;
       return response[2];
   }

   private string GetRankUrl(string webSiteUrl)
   {
       string url = "info:" + webSiteUrl;
       int ch = GoogleCH(StrOrdinals(url));
       return
           "http://www.google.com/search?client=navclient-auto&ch=6"
           + ch.ToString()
           + "&features=Rank&q="
           + url;
   }

   // Starting here, this is mostly converted and adapted PHP source code
   // I definitely did not take the time to tidy it and / or try to understand the algorithms

   private static uint GOOGLE_MAGIC = 0xE6359A60;

   // Converts a string into an array of integers containing the numeric value of the char
   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;
   }

   // Unsigned shift right
   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)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)
       /*all the case statements fall through*/
       {

           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);
               /*the first byte of c is reserved for the length*/
               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]);
               /*case 0: nothing left to add*/
               break;

       }
       mix = Mix(a, b, c);

       return mix[2];
   }

   // Nice & handy non-caching Web request method
   public static string RunUrl(string url)
   {
       WebRequest request = WebRequest.Create(url);

       //HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
       //request.CachePolicy = noCachePolicy;

       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;
   }


   private int GetLinks(string searchURL, string OutAs, string anchor, string trail)
   {
       int count = 0;
       string serverResponse = RunUrl(searchURL);
       int pos = serverResponse.IndexOf(anchor);
       if (pos > 1)
       {
           serverResponse = serverResponse.Substring(pos + anchor.Length);
           pos = serverResponse.IndexOf(trail); //" results";
           string value = serverResponse.Substring(0, pos);
           value = value.Replace(",", "");
           value = value.Replace(".", "");
           count = Int32.Parse(value);
       }

       Out(OutAs + ": " + count);

       return count;
   }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

You are attempting to access restricted resource. i.e. you haven't permission to access this resource.

This article has been dead for over six months. Start a new discussion instead.