Posted By

zhengrenchi on 04/17/09


Tagged

google pr


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

gudeidea
flashinit


Use Actionscript to get google page rank


 / Published in: ActionScript 3
 

I convert the google pr fetch code from php to actionscript.

  1. package {
  2. import flash.display.Sprite;
  3. import flash.errors.*;
  4. import flash.events.*;
  5. import flash.net.Socket;
  6.  
  7. public class FetchPR extends Sprite
  8. {
  9. public const GOOGLE_MAGIC:Number = 0xE6359A60;
  10. public var resultPR:Number = 0;
  11. public var callback:Function;
  12. private var sock:Socket;
  13.  
  14. public function zeroFill(a:Number, b:Number):Number
  15. {
  16. var z:Number = 0x80000000;
  17. var sa:Number = a;
  18. var sb:Number = b;
  19.  
  20. if (z & sa) {
  21. sa = (sa>>1);
  22. sa &= (~z);
  23. sa |= 0x40000000;
  24. sa = (sa>>(sb-1));
  25. }
  26. else {
  27. sa = (sa>>sb);
  28. }
  29. return sa;
  30. }
  31.  
  32. public function mixa(a:Number,b:Number,c:Number):Array
  33. {
  34. var sa:Number = a;
  35. var sb:Number = b;
  36. var sc:Number = c;
  37.  
  38. sa -= sb; sa -= sc; sa ^= (zeroFill(sc,13));
  39. sb -= sc; sb -= sa; sb ^= (sa << 8);
  40. sc -= sa; sc -= sb; sc ^= (zeroFill(sb,13));
  41. sa -= sb; sa -= sc; sa ^= (zeroFill(sc,12));
  42. sb -= sc; sb -= sa; sb ^= (sa<<16);
  43. sc -= sa; sc -= sb; sc ^= (zeroFill(sb,5));
  44. sa -= sb; sa -= sc; sa ^= (zeroFill(sc,3));
  45. sb -= sc; sb -= sa; sb ^= (sa<<10);
  46. sc -= sa; sc -= sb; sc ^= (zeroFill(sb,15));
  47.  
  48. return new Array(sa,sb,sc);
  49. }
  50.  
  51. public function GoogleCH(url:Array, length:Number = 0, init:Number=GOOGLE_MAGIC):Number
  52. {
  53. if (length == 0) {
  54. length = url.length;
  55. }
  56. var sa:Number = 0x9E3779B9;
  57. var sb:Number = 0x9E3779B9;
  58. var sc:Number = init;
  59. var k:Number = 0;
  60. var len:Number = length;
  61. var mix:Array;
  62.  
  63. while(len >= 12) {
  64. sa += (url[k+0] +(url[k+1]<<8) +(url[k+2]<<16) +(url[k+3]<<24));
  65. sb += (url[k+4] +(url[k+5]<<8) +(url[k+6]<<16) +(url[k+7]<<24));
  66. sc += (url[k+8] +(url[k+9]<<8) +(url[k+10]<<16)+(url[k+11]<<24));
  67. mix = mixa(sa,sb,sc);
  68. sa = mix[0]; sb = mix[1]; sc = mix[2];
  69. k += 12;
  70. len -= 12;
  71. }
  72. sc += length;
  73. switch (len) {
  74. case 11: sc+=(url[k+10]<<24);
  75. case 10: sc+=(url[k+9]<<16);
  76. case 9 : sc+=(url[k+8]<<8);
  77. case 8 : sb+=(url[k+7]<<24);
  78. case 7 : sb+=(url[k+6]<<16);
  79. case 6 : sb+=(url[k+5]<<8);
  80. case 5 : sb+=(url[k+4]);
  81. case 4 : sa+=(url[k+3]<<24);
  82. case 3 : sa+=(url[k+2]<<16);
  83. case 2 : sa+=(url[k+1]<<8);
  84. case 1 : sa+=(url[k+0]);
  85. }
  86. mix = mixa(sa,sb,sc);
  87. return mix[2];
  88. }
  89.  
  90. public function strord(str:String):Array
  91. {
  92. var result:Array = new Array();
  93.  
  94. for (var i:Number = 0; i < str.length; i++) {
  95. result[i] = str.charCodeAt(i);
  96. }
  97.  
  98. return result;
  99. }
  100.  
  101. public function getPR(url:String):void
  102. {
  103. sock = new Socket("www.google.com", 80);
  104. url = "info:" + url;
  105.  
  106. var ch:Number = GoogleCH(strord(url));
  107. var urlstring:String = "/search?client=navclient-auto&ch=6" + ch + "&features=Rank&q=" + url;
  108.  
  109. sock.writeUTFBytes("GET " + urlstring + " HTTP/1.0
  110. ");
  111. sock.writeUTFBytes("Host: toolbarqueries.google.com
  112. ");
  113. sock.writeUTFBytes("User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114.9-big; Linux 2.6)
  114. ");
  115. sock.writeUTFBytes("Connection: Close
  116.  
  117. ");
  118. sock.flush();
  119. sock.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
  120. }
  121.  
  122. private function socketDataHandler(event:ProgressEvent):void
  123. {
  124. var res:String = sock.readUTFBytes(sock.bytesAvailable);
  125. var offset:Number = res.lastIndexOf("Rank");
  126. res = res.substr(offset);
  127. var result:Array = res.split(":");
  128. resultPR = result[2];
  129. callback.call(resultPR);
  130. }
  131.  
  132. public function FetchPR()
  133. {
  134. }
  135. }
  136.  
  137. }

Report this snippet  

You need to login to post a comment.