0xcafebabe -- per aspera ad astra

Dokument pobrany z: http://www.anadoxin.org/blog/node/30

Odczytywanie PageRank w Java
Kod odczytujący ranking PageRank w Javie.

 1 package org.anadoxin.prlib;
 2
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.net.MalformedURLException;
 7 import java.net.URL;
 8
 9 public class PageRank {
10     public static String googleHash(String string) {
11         String seed = "Mining PageRank is AGAINST GOOGLE'S TERMS OF SERVICE. Yes, I'm talking to you, scammer.";
12         
13         int hash = 16909125;
14         for(int i = 0; i < string.length(); i++) {
15             hash ^= seed.charAt(i % seed.length()) ^ string.charAt(i);
16             hash = hash >>> 23 | hash << 9;
17         }
18         
19         return String.format("8%x", hash);
20     }
21     
22     public static int acquire(String surl) throws MalformedURLException, IOException {
23         String hash = googleHash(surl);
24         URL url = new URL(
25             String.format("http://www.google.com/search?client=%s&features=%s&ch=%s&q=%s",
26                     "navclient-auto",
27                     "Rank",
28                     hash,
29                     "info:" + surl)
30         );
31         
32         BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
33         String line;
34         while((line = reader.readLine()) != null) {
35             String[] entries = line.split(":");
36             if(entries.length > 2 && entries[0] != null && entries[0].equals("Rank_1")) {
37                 return Integer.parseInt(entries[2]);
38             }
39         }
40         
41         return 0;
42     }
43 }
Klasę możesz ściągnąć w archiwum jar tutaj. Możesz ją użyć w ten sposób:
  1. String url = "http://anadoxin.org/blog";
  2. int pr = PageRank.acquire(url);
  3. System.out.println("PageRank for " + url + ": " + pr);
Miłego naginania regulaminu Google.