GooglePageRank - Class to get PageRank info for a URL


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public static class GooglePageRank
  2. {
  3. private static void Mix(ref uint a, ref uint b, ref uint c)
  4. {
  5. a -= b;
  6. a -= c;
  7. a ^= c >> 13;
  8. b -= c;
  9. b -= a;
  10. b ^= a << 8;
  11. c -= a;
  12. c -= b;
  13. c ^= b >> 13;
  14. a -= b;
  15. a -= c;
  16. a ^= c >> 12;
  17. b -= c;
  18. b -= a;
  19. b ^= a << 16;
  20. c -= a;
  21. c -= b;
  22. c ^= b >> 5;
  23. a -= b;
  24. a -= c;
  25. a ^= c >> 3;
  26. b -= c;
  27. b -= a;
  28. b ^= a << 10;
  29. c -= a;
  30. c -= b;
  31. c ^= b >> 15;
  32. }
  33.  
  34. private static string GoogleChecksum(string url)
  35. {
  36. uint GoogleMagic = 0xE6359A60;
  37.  
  38. uint a, b;
  39. uint c = GoogleMagic;
  40.  
  41. a = b = 0x9E3779B9;
  42.  
  43. int k = 0;
  44. int length = url.Length;
  45.  
  46. //Algorithm
  47. while (length >= 12)
  48. {
  49. a += (uint)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
  50. b += (uint)(url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24));
  51. c += (uint)(url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24));
  52.  
  53. Mix(ref a, ref b, ref c);
  54.  
  55. k += 12;
  56. length -= 12;
  57. }
  58.  
  59. c += (uint)url.Length;
  60.  
  61. //All cases fall through
  62. switch (length)
  63. {
  64. case 11:
  65. c += (uint)(url[k + 10] << 24);
  66. goto case 10; //fall through
  67. case 10:
  68. c += (uint)(url[k + 9] << 16);
  69. goto case 9;
  70. case 9:
  71. c += (uint)(url[k + 8] << 8);
  72. goto case 8;
  73. case 8:
  74. b += (uint)(url[k + 7] << 24);
  75. goto case 7;
  76. case 7:
  77. b += (uint)(url[k + 6] << 16);
  78. goto case 6;
  79. case 6:
  80. b += (uint)(url[k + 5] << 8);
  81. goto case 5;
  82. case 5:
  83. b += (uint)(url[k + 4]);
  84. goto case 4;
  85. case 4:
  86. a += (uint)(url[k + 3] << 24);
  87. goto case 3;
  88. case 3:
  89. a += (uint)(url[k + 2] << 16);
  90. goto case 2;
  91. case 2:
  92. a += (uint)(url[k + 1] << 8);
  93. goto case 1;
  94. case 1:
  95. a += (uint)(url[k + 0]);
  96. break;
  97. default:
  98. break;
  99. }
  100.  
  101. Mix(ref a, ref b, ref c);
  102.  
  103. return string.Format("6{0}", c);
  104. }
  105.  
  106. public static int getPageRank(string url)
  107. {
  108. //Calculate URL checksum
  109. string checkSum = GoogleChecksum("info:" + url);
  110. string file = "http://toolbarqueries.google.com/search?client=navclient-auto&ch=" + checkSum + "&features=Rank&q=info:" + url;
  111.  
  112. try
  113. {
  114. //Request PR from Google
  115. WebRequest request = WebRequest.Create(file);
  116. WebResponse response = request.GetResponse();
  117.  
  118. StreamReader reader = new StreamReader(response.GetResponseStream());
  119. string data = reader.ReadToEnd();
  120.  
  121. reader.Close();
  122. response.Close();
  123.  
  124. //Parse PR from string
  125. int pageRank = -1;
  126. if (data.IndexOf(':') != -1)
  127. {
  128. data = data.Substring(data.LastIndexOf(':') + 1);
  129. }
  130.  
  131. int.TryParse(data, out pageRank);
  132.  
  133. return pageRank;
  134. }
  135. catch (Exception)
  136. {
  137. return -1;
  138. }
  139. }
  140. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.