Return to Snippet

Revision: 13271
at April 19, 2009 08:29 by zhengrenchi


Updated Code
package {
import flash.display.Sprite;
import flash.errors.*;
import flash.events.*;
import flash.net.Socket;

public class FetchPR extends Sprite
{
	public const GOOGLE_MAGIC:Number = 0xE6359A60;
	public var resultPR:Number = 0;
	public var callback:Function;
	private var sock:Socket;
 
	public function zeroFill(a:Number, b:Number):Number
	{
		var z:Number = 0x80000000;
		var sa:Number = a;
		var sb:Number = b;

		if (z & sa) {
			sa = (sa>>1);
			sa &= (~z);
			sa |= 0x40000000;
			sa = (sa>>(sb-1));
		}
		else {
			sa = (sa>>sb);
		}
		return sa;
	}

	public function mixa(a:Number,b:Number,c:Number):Array
	{
		var sa:Number = a;
		var sb:Number = b;
		var sc:Number = c;
	
		sa -= sb; sa -= sc; sa ^= (zeroFill(sc,13));
		sb -= sc; sb -= sa; sb ^= (sa << 8);
		sc -= sa; sc -= sb; sc ^= (zeroFill(sb,13));
		sa -= sb; sa -= sc; sa ^= (zeroFill(sc,12));
		sb -= sc; sb -= sa; sb ^= (sa<<16);
		sc -= sa; sc -= sb; sc ^= (zeroFill(sb,5));
		sa -= sb; sa -= sc; sa ^= (zeroFill(sc,3));
		sb -= sc; sb -= sa; sb ^= (sa<<10);
		sc -= sa; sc -= sb; sc ^= (zeroFill(sb,15));

		return new Array(sa,sb,sc);
	}

	public function GoogleCH(url:Array, length:Number = 0, init:Number=GOOGLE_MAGIC):Number
	{
		if (length == 0) {
			length = url.length;
		}
		var sa:Number = 0x9E3779B9;
		var sb:Number = 0x9E3779B9;
		var sc:Number = init;
		var k:Number = 0;
		var len:Number = length;
		var mix:Array;
	
		while(len >= 12) {
			sa += (url[k+0] +(url[k+1]<<8) +(url[k+2]<<16) +(url[k+3]<<24));
			sb += (url[k+4] +(url[k+5]<<8) +(url[k+6]<<16) +(url[k+7]<<24));
			sc += (url[k+8] +(url[k+9]<<8) +(url[k+10]<<16)+(url[k+11]<<24));
			mix = mixa(sa,sb,sc);
			sa = mix[0]; sb = mix[1]; sc = mix[2];
			k += 12;
			len -= 12;
		}
		sc += length;
		switch (len) {
		case 11: sc+=(url[k+10]<<24);
		case 10: sc+=(url[k+9]<<16);
		case 9 : sc+=(url[k+8]<<8);
		case 8 : sb+=(url[k+7]<<24);
		case 7 : sb+=(url[k+6]<<16);
		case 6 : sb+=(url[k+5]<<8);
		case 5 : sb+=(url[k+4]);
		case 4 : sa+=(url[k+3]<<24);
		case 3 : sa+=(url[k+2]<<16);
		case 2 : sa+=(url[k+1]<<8);
		case 1 : sa+=(url[k+0]);
		}
		mix = mixa(sa,sb,sc);
		return mix[2];
	}

	public function strord(str:String):Array
	{
		var result:Array = new Array();

		for (var i:Number = 0; i < str.length; i++) {
			result[i] = str.charCodeAt(i);
		}

		return result;
	}

	public function getPR(url:String):void
	{
		sock = new Socket("www.google.com", 80);
		url = "info:" + url;

		var ch:Number = GoogleCH(strord(url));
		var urlstring:String = "/search?client=navclient-auto&ch=6" + ch + "&features=Rank&q=" + url;

		sock.writeUTFBytes("GET " + urlstring + " HTTP/1.0
");
		sock.writeUTFBytes("Host: toolbarqueries.google.com
");
		sock.writeUTFBytes("User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114.9-big; Linux 2.6)
");
		sock.writeUTFBytes("Connection: Close

");
		sock.flush();
		sock.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
	}

	private function socketDataHandler(event:ProgressEvent):void 
	{
		var res:String = sock.readUTFBytes(sock.bytesAvailable);
		var offset:Number = res.lastIndexOf("Rank");
		res = res.substr(offset);
		var result:Array = res.split(":");
		resultPR = result[2];
		callback.call(resultPR);
	}

	public function FetchPR()
	{
	}
}

}

Revision: 13270
at April 17, 2009 05:16 by zhengrenchi


Initial Code
/*
 * 
 *
*/

package {
	import flash.display.Sprite;
	import flash.errors.*;
	import flash.events.*;
	import flash.net.Socket;

	
	public class FetchPR extends Sprite
	{
		public const GOOGLE_MAGIC:Number = 0xE6359A60;
		public var resultPR:Number = 0;
	
		public function zeroFill(a:Number, b:Number):Number
		{
			var z:Number = 0x80000000;
			var sa:Number = a;
			var sb:Number = b;
			if (z & sa) {
				sa = (sa>>1);
				sa &= (~z);
				sa |= 0x40000000;
				sa = (sa>>(sb-1));
			}
			else {
				sa = (sa>>sb);
			}
			return sa;
		}
		
		public function mixa(a:Number,b:Number,c:Number):Array
		{
			var sa:Number = a;
			var sb:Number = b;
			var sc:Number = c;
			sa -= sb; sa -= sc; sa ^= (zeroFill(sc,13));
			sb -= sc; sb -= sa; sb ^= (sa << 8);
			sc -= sa; sc -= sb; sc ^= (zeroFill(sb,13));
			sa -= sb; sa -= sc; sa ^= (zeroFill(sc,12));
			sb -= sc; sb -= sa; sb ^= (sa<<16);
			sc -= sa; sc -= sb; sc ^= (zeroFill(sb,5));
			sa -= sb; sa -= sc; sa ^= (zeroFill(sc,3));
			sb -= sc; sb -= sa; sb ^= (sa<<10);
			sc -= sa; sc -= sb; sc ^= (zeroFill(sb,15));
			
			return new Array(sa,sb,sc);
		}
		
		public function GoogleCH(url:Array, length:Number = 0, init:Number=GOOGLE_MAGIC):Number
		{
			if (length == 0) {
				length = url.length;
			}
			var sa:Number = 0x9E3779B9;
			var sb:Number = 0x9E3779B9; 
			var sc:Number = init;
			var k:Number = 0;
			var len:Number = length;
			var mix:Array;
			while(len >= 12) {
				sa += (url[k+0] +(url[k+1]<<8) +(url[k+2]<<16) +(url[k+3]<<24));
				sb += (url[k+4] +(url[k+5]<<8) +(url[k+6]<<16) +(url[k+7]<<24));
				sc += (url[k+8] +(url[k+9]<<8) +(url[k+10]<<16)+(url[k+11]<<24));
				mix = mixa(sa,sb,sc);
				sa = mix[0]; sb = mix[1]; sc = mix[2];
				k += 12;
				len -= 12;
			}
			sc += length;
			switch (len) {
				case 11: sc+=(url[k+10]<<24);
				case 10: sc+=(url[k+9]<<16);
				case 9 : sc+=(url[k+8]<<8);
				case 8 : sb+=(url[k+7]<<24);
				case 7 : sb+=(url[k+6]<<16);
				case 6 : sb+=(url[k+5]<<8);
				case 5 : sb+=(url[k+4]);
				case 4 : sa+=(url[k+3]<<24);
				case 3 : sa+=(url[k+2]<<16);
				case 2 : sa+=(url[k+1]<<8);
				case 1 : sa+=(url[k+0]);
			}
			mix = mixa(sa,sb,sc);
			return mix[2];
		}
		
		public function strord(str:String):Array
		{	
			var result:Array = new Array();
			for(var i:Number = 0; i < str.length; i++) {
				result[i] = str.charCodeAt(i);
			}
			return result;
		}
		
		private var sock:Socket;
		
		public function getPR(url:String):void
		{
			sock = new Socket("www.google.com", 80);
			
			url = "info:" + url;
			var ch:Number = GoogleCH(strord(url));
			var urlstring:String = "/search?client=navclient-auto&ch=6" + ch + "&features=Rank&q=" + url;
			
			sock.writeUTFBytes("GET " + urlstring + " HTTP/1.0
");
			sock.writeUTFBytes("Host: toolbarqueries.google.com
");
			sock.writeUTFBytes("User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114.9-big; Linux 2.6)
");
			sock.writeUTFBytes("Connection: Close

");
			sock.flush();
			
			sock.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
		}
		
		private function socketDataHandler(event:ProgressEvent):void {
        	var res:String = sock.readUTFBytes(sock.bytesAvailable);
        	
        	var offset:Number = res.lastIndexOf("Rank");
        	res = res.substr(offset);
        	var result:Array = res.split(":");
        	resultPR = result[2];
	    }

		
		public function FetchPR()
		{
			getPR("http://www.comen.net");
		}
	}
}

Initial URL

                                

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

Initial Title
Use Actionscript to get google page rank

Initial Tags
google

Initial Language
ActionScript 3