Module: SeoTools::Engine::Google::PageRank

Included in:
SeoTools::Engine::Google
Defined in:
lib/seotools/engine/google/pagerank.rb

Constant Summary collapse

GOOGLE_MAGICK =
0xE6359A60
MODULO =
0x100000000

Instance Method Summary collapse

Instance Method Details

#chars_to_i(str, k) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/seotools/engine/google/pagerank.rb', line 44

def chars_to_i(str, k)
	i = 0
	substr = str.slice(k..str.length)
	return 0 unless substr
	substr.unpack('C4').compact.each_with_index do |value, index|
		i += value << (index * 8)
	end
	return i
end

#get_check_sum(uri) ⇒ Object Also known as: check_sum



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/seotools/engine/google/pagerank.rb', line 23

def get_check_sum(uri)
	a = b = 0x9E3779B9
	c = GOOGLE_MAGICK
	k = 0
	iuri = "info:#{uri}"
	len = iuri.length
	while len >= k + 12
		a = (a + chars_to_i(iuri, k))
		b = (b + chars_to_i(iuri, k+4))
		c = (c + chars_to_i(iuri, k+8))
		a, b, c = mix(a, b, c)
		k = k + 12
	end
	a = (a + chars_to_i(iuri, k))
	b = (b + chars_to_i(iuri, k + 4))
	c = (c + (chars_to_i(iuri, k + 8) << 8) + len)
	a, b, c = mix(a, b, c)
	return c
end

#get_page_rank(uri) ⇒ Object Also known as: page_rank



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/seotools/engine/google/pagerank.rb', line 9

def get_page_rank(uri)
	ch = sprintf("6%u", self.get_check_sum(uri))
	query_uri = sprintf("http://toolbarqueries.google.co.jp/search?client=navclient-auto&features=Rank&failedip=216.239.51.102;821&q=info:%s&ch=%s", uri, ch)
	open(query_uri) do |f|
		body = f.read
		# Rank_1:1:3
		return $1.to_i if body =~ /Rank_1:1:(\d)/
	end
	nil
#		rescue
#			nil
end

#mix(a, b, c) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/seotools/engine/google/pagerank.rb', line 55

def mix(a, b, c)
	a, b, c = a % MODULO, b % MODULO, c % MODULO 
	a = (((a + (MODULO - b) + (MODULO - c)) % MODULO) ^ zero_fill(c, 13)) % MODULO
	b = (((b + (MODULO - c) + (MODULO - a)) % MODULO) ^ (a << 8)) % MODULO
	c = (((c + (MODULO - a) + (MODULO - b)) % MODULO) ^ zero_fill(b, 13)) % MODULO
	a = (((a + (MODULO - b) + (MODULO - c)) % MODULO) ^ zero_fill(c, 12)) % MODULO
	b = (((b + (MODULO - c) + (MODULO - a)) % MODULO) ^ (a << 16)) % MODULO
	c = (((c + (MODULO - a) + (MODULO - b)) % MODULO) ^ zero_fill(b, 5)) % MODULO
	a = (((a + (MODULO - b) + (MODULO - c)) % MODULO) ^ zero_fill(c, 3)) % MODULO
	b = (((b + (MODULO - c) + (MODULO - a)) % MODULO) ^ (a << 10)) % MODULO
	c = (((c + (MODULO - a) + (MODULO - b)) % MODULO) ^ zero_fill(b, 15)) % MODULO
	return [a, b, c]
end

#zero_fill(a, b) ⇒ Object



69
70
71
# File 'lib/seotools/engine/google/pagerank.rb', line 69

def zero_fill(a, b)
	(a >> b) % MODULO
end