Created
May 23, 2010 15:00
-
-
Save jaimeiniesta/410998 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "net/http" | |
| module GooglePageRank | |
| M=0x100000000 # modulo for unsigned int 32bit(4byte) | |
| def m1(a,b,c,d) | |
| return (((a+(M-b)+(M-c))%M)^(d%M))%M # mix/power mod | |
| end | |
| def c2i(s="",k=0) | |
| # char codes to int. Little Endian | |
| return ((s[k+3].to_i*0x100+s[k+2].to_i)*0x100+s[k+1].to_i)*0x100+s[k].to_i | |
| end | |
| def mix(a,b,c) | |
| a=a%M; b=b%M; c=c%M | |
| a = m1(a,b,c, c >> 13); b = m1(b,c,a, a << 8); c = m1(c,a,b, b >> 13); | |
| a = m1(a,b,c, c >> 12); b = m1(b,c,a, a << 16); c = m1(c,a,b, b >> 5); | |
| a = m1(a,b,c, c >> 3); b = m1(b,c,a, a << 10); c = m1(c,a,b, b >> 15); | |
| return [a,b,c]; | |
| end | |
| def checkSum(url="http://sample/index.html") | |
| a= 0x9E3779B9; b= 0x9E3779B9; c= 0xE6359A60; | |
| iurl="info:"+url; len = iurl.size; k=0; | |
| while (len>=k+12) do | |
| a += c2i(iurl,k) ; b += c2i(iurl,k+4); c += c2i(iurl,k+8); a,b,c = mix(a,b,c); | |
| k=k+12 | |
| end | |
| a += c2i(iurl,k); b += c2i(iurl,k+4); c += (c2i(iurl,k+8)<<8)+len; a,b,c = mix(a,b,c); | |
| return c; | |
| end | |
| def get(url="http://sample/index.html",port=80,proxy=nil,proxy_port=nil) | |
| # get Google PageRank | |
| # 2007.07.10 | |
| ch = checkSum(url); | |
| # printf("CheckSUM: 6%u\n", ch); | |
| ###### format changed | |
| #g_path=sprintf("/search?client=navclient-auto&failedip=216.239.51.102;821&ch=6%u&q=info:%s", ch, url); | |
| g_path=sprintf("/search?client=navclient-auto&features=Rank&failedip=216.239.51.102;821&q=info:%s&ch=6%u", url, ch); | |
| p="" # rank | |
| # printf("%s\n",g_path) | |
| # http://www.google.co.jp/search?client=navclient-auto&ch=63055969557&features=Rank&q=info:http://www.hyperposition.com/se3blog/ | |
| # http://www.google.co.jp/search?client=navclient-auto&features=Rank&q=info:http://www.hyperposition.com/&ch=6768349016 | |
| g_server="toolbarqueries.google.com" # toolbarqueries.google.co.jp | |
| Net::HTTP::new(g_server, port, proxy, proxy_port).get(g_path){|line| | |
| # printf("%s\n", line) | |
| ###### format changed | |
| pos=line.index("Rank_1:1:") # format: Rank_1:1:4 | |
| if( pos != nil) then p=(line[pos+9,2]).to_i; break; end; | |
| } | |
| if (p.size>0) then return p.to_i; else return -1; end | |
| end | |
| module_function :m1, :c2i, :mix | |
| module_function :checkSum, :get | |
| end; | |
| if $0 == __FILE__ then | |
| require "getoptlong" | |
| opt_url="" | |
| opt_silent=0; | |
| port=80 # 80 World Wide Web HTTP | |
| proxy=nil; proxy_port=nil | |
| # proxy="10.1.4.1"; proxy_port=8080 # 8080 HTTP Alternate | |
| def usage | |
| puts "usage: foo.rb [-h] [-s] -u http://... " | |
| puts "Options:\n"; | |
| puts "-h help\n"; | |
| puts "-s silent\n" | |
| end; | |
| arg_parser=GetoptLong.new | |
| arg_parser.set_options( | |
| ["-h", "--help" , GetoptLong::NO_ARGUMENT], | |
| ["-s", "--silent" , GetoptLong::NO_ARGUMENT], | |
| ["-u", "--url", GetoptLong::REQUIRED_ARGUMENT] | |
| ) | |
| arg_parser.each{|opt,arg| | |
| begin | |
| case opt | |
| when "-h"; usage; exit; | |
| when "-s"; opt_silent=1; | |
| when "-u"; opt_url=arg; | |
| end; | |
| rescue => err; puts err; break | |
| end | |
| } | |
| #puts "opt_silent= #{opt_silent}" | |
| #puts "opt_url= #{opt_url}" | |
| if (opt_silent == 0) then | |
| rank = GooglePageRank.get(opt_url,port,proxy,proxy_port) | |
| if (rank >= 0) then | |
| printf("PageRank: %d : %s\n", rank, opt_url); | |
| else | |
| printf("PageRank: NO_INDEX : %s\n", opt_url); | |
| end | |
| else | |
| printf("%d\n", GooglePageRank.get(opt_url)); | |
| end | |
| exit(0); | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment