google_pr 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +0 -10
  2. data/lib/google_pr.rb +92 -62
  3. metadata +31 -43
  4. data/init.rb +0 -2
data/README CHANGED
@@ -1,12 +1,6 @@
1
1
  GooglePR
2
2
  ===============
3
3
 
4
- This plugin for Ruby on Rails allow to check the Google PageRank of an URL
5
- The original script was wrote by Vsevolod S. Balashov <vsevolod@balashov.name>
6
- and is base on code snippets found on the web (see code comments)
7
-
8
- This script was rewritten to become a Rails gem
9
-
10
4
  Example
11
5
  =======
12
6
 
@@ -18,7 +12,3 @@ Specs
18
12
  Run this after each improvement in this project
19
13
  spec spec/pagerank_checker_specs.rb
20
14
  It requires Rspec installation: gem install rspec
21
-
22
-
23
- (C) 2006-2007 under terms of LGPL v2.1
24
- by Vsevolod S. Balashov <vsevolod@balashov.name>
data/lib/google_pr.rb CHANGED
@@ -11,82 +11,112 @@ require 'uri'
11
11
  require 'open-uri'
12
12
 
13
13
  # http://blog.outer-court.com/archive/2004_06_27_index.html#108834386239051706
14
- class AutomatedQueryError < StandardError; end;
14
+ class AutomatedQueryError < StandardError
15
+ end
15
16
 
16
- class GooglePR
17
17
 
18
- M=0x100000000 # modulo for unsigned int 32bit(4byte)
19
- attr_accessor :uri
20
18
 
21
- # Create a new GooglePR object with the given 'uri' parameter
22
- def initialize(uri)
23
- # TODO should raise a URI::InvalidURIError with a invalid URI parameter
24
- @uri = uri
25
- end
19
+ # extracted from PageRankr
20
+ # cf. https://github.com/blatyo/page_rankr
21
+ # use: Google::Pagerank.new(domain).check
26
22
 
27
- def m1(a,b,c,d)
28
- (((a+(M-b)+(M-c))%M)^(d%M))%M # mix/power mod
29
- end
23
+ class GoogleChecksum
24
+ class << self
25
+ def generate(site)
26
+ bytes = byte_array(site)
27
+ length = bytes.length
28
+ a = b = 0x9E3779B9
29
+ c = 0xE6359A60
30
30
 
31
- def i2c(i)
32
- [i&0xff, i>>8&0xff, i>>16&0xff, i>>24&0xff]
33
- end
31
+ k, len = 0, length
32
+ while(len >= 12)
33
+ a, b, c = mix(*shift(a, b, c, k, bytes))
34
+ k += 12
35
+ len -= 12
36
+ end
34
37
 
35
- def c2i(s,k=0)
36
- ((s[k+3].to_i*0x100+s[k+2].to_i)*0x100+s[k+1].to_i)*0x100+s[k].to_i
37
- end
38
+ c = c + length
38
39
 
39
- def mix(a,b,c)
40
- a = a%M; b = b%M; c = c%M
41
- a = m1(a,b,c, c >> 13); b = m1(b,c,a, a << 8); c = m1(c,a,b, b >> 13)
42
- a = m1(a,b,c, c >> 12); b = m1(b,c,a, a << 16); c = m1(c,a,b, b >> 5)
43
- a = m1(a,b,c, c >> 3); b = m1(b,c,a, a << 10); c = m1(c,a,b, b >> 15)
44
- [a, b, c]
45
- end
40
+ c = mix(*toss(a, b, c, bytes, len, k))[2]
41
+ "6" + c.to_s
42
+ end
46
43
 
47
- def old_cn(iurl = 'info:' + @uri)
48
- a = 0x9E3779B9; b = 0x9E3779B9; c = 0xE6359A60
49
- len = iurl.size
50
- k = 0
51
- while (len >= k + 12) do
52
- a += c2i(iurl,k); b += c2i(iurl,k+4); c += c2i(iurl,k+8)
53
- a, b, c = mix(a, b, c)
54
- k = k + 12
44
+ private
45
+
46
+ def byte_array(site)
47
+ bytes = []
48
+ site.each_byte {|b| bytes << b}
49
+ bytes
50
+ end
51
+
52
+ # Need to keep numbers in the unsigned int 32 range
53
+ def m(v)
54
+ v % 0x100000000
55
+ end
56
+
57
+ def shift(a, b, c, k, bytes)
58
+ a = m(a + bytes[k + 0] + (bytes[k + 1] << 8) + (bytes[k + 2] << 16) + (bytes[k + 3] << 24))
59
+ b = m(b + bytes[k + 4] + (bytes[k + 5] << 8) + (bytes[k + 6] << 16) + (bytes[k + 7] << 24))
60
+ c = m(c + bytes[k + 8] + (bytes[k + 9] << 8) + (bytes[k + 10] << 16) + (bytes[k + 11] << 24))
61
+
62
+ [a, b, c]
63
+ end
64
+
65
+ def mix(a, b, c)
66
+ a, b, c = m(a), m(b), m(c)
67
+
68
+ a = m(a-b-c) ^ m(c >> 13)
69
+ b = m(b-c-a) ^ m(a << 8)
70
+ c = m(c-a-b) ^ m(b >> 13)
71
+
72
+ a = m(a-b-c) ^ m(c >> 12)
73
+ b = m(b-c-a) ^ m(a << 16)
74
+ c = m(c-a-b) ^ m(b >> 5)
75
+
76
+ a = m(a-b-c) ^ m(c >> 3)
77
+ b = m(b-c-a) ^ m(a << 10)
78
+ c = m(c-a-b) ^ m(b >> 15)
79
+
80
+ [a, b, c]
81
+ end
82
+
83
+ def toss(a, b, c, bytes, len, k)
84
+ case len
85
+ when 9..11
86
+ c = c + (bytes[k+len-1] << ((len % 8) * 8))
87
+ when 5..8
88
+ b = b + (bytes[k+len-1] << ((len % 5) * 8))
89
+ when 1..4
90
+ a = a + (bytes[k+len-1] << ((len - 1) * 8))
91
+ else
92
+ return [a, b, c]
93
+ end
94
+ toss(a, b, c, bytes, len-1, k)
55
95
  end
56
- a += c2i(iurl,k); b += c2i(iurl,k+4); c += (c2i(iurl,k+8) << 8) + len
57
- a,b,c = mix(a,b,c)
58
- return c
59
- end
60
-
61
- # Calculates de checksum to use as 'ch' parameter on request_uri,
62
- # for example the checksum of www.rubyonrails.com is 6602033163
63
- def cn
64
- ch = old_cn
65
- ch = ((ch/7) << 2) | ((ch-(ch/13).floor*13)&7)
66
- new_url = []
67
- 20.times { i2c(ch).each { |i| new_url << i }; ch -= 9 }
68
- ('6' + old_cn(new_url).to_s).to_i
69
96
  end
97
+ end
98
+
99
+ class GooglePR
100
+ attr_accessor :domain, :checksum
70
101
 
71
- # URI that gets the pagerank,
72
- # for example to get the Google's pagerank of www.rubyonrails.com the request_uri is http://toolbarqueries.google.com/search?client=navclient-auto&hl=en&ch=6602033163&ie=UTF-8&oe=UTF-8&features=Rank&q=info:www.rubyonrails.com
73
- def request_uri
74
- # http://www.bigbold.com/snippets/posts/show/1260 + _ -> %5F
75
- "http://toolbarqueries.google.com/tbr?client=navclient-auto&hl=en&ch=#{cn}&ie=UTF-8&oe=UTF-8&features=Rank&q=info:#{URI.escape(@uri, /[^-.!~*'()a-zA-Z\d]/)}"
102
+ def initialize(domain)
103
+ self.domain = domain
104
+ self.checksum = GoogleChecksum.generate("info:#{self.domain}")
76
105
  end
77
106
 
78
107
  # Return a number between 0 to 10, that represents the Google PageRank
79
- def page_rank(uri = @uri)
80
- @uri = uri if uri != @uri
81
- res = HTTParty.get(request_uri, :headers => {"User-Agent" => Object.const_defined?("WITH_USER_AGENT") ? Object.const_get("WITH_USER_AGENT") : "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; fr; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10" })
82
- if (m=res.to_s.match(/Rank_1:\d:(\d+)/))
83
- return m[1].to_i
84
- elsif res.to_s.match(/automated queries/im)
85
- raise AutomatedQueryError.new("Blacklisted for automated queries")
86
- end
87
- rescue OpenURI::HTTPError, SocketError
88
- nil
108
+ def check
109
+ url = "#{toolbar_url}?" + params.collect {|k,v| "#{k}=#{v}"}.join("&")
110
+ res = HTTParty.get(url, :headers => {"User-Agent" => "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" }, :format => :raw)
111
+ res.to_s.match(/Rank_\d+:\d+:(\d+)/) ? $1.to_i : nil
89
112
  end
90
113
 
91
- private :m1, :i2c, :c2i, :mix, :old_cn
114
+ private
115
+ def toolbar_url
116
+ "http://toolbarqueries.google.com/tbr"
117
+ end
118
+
119
+ def params
120
+ {:client => "navclient-auto", :ch => @checksum, :features => "Rank", :q => "info:#{self.domain}"}
121
+ end
92
122
  end
metadata CHANGED
@@ -1,70 +1,58 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: google_pr
3
- version: !ruby/object:Gem::Version
4
- hash: 21
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 1
10
- version: 1.0.1
11
6
  platform: ruby
12
- authors:
13
- - Vsevolod S. Balashov
7
+ authors:
8
+ - Various
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-11-07 00:00:00 +01:00
19
- default_executable:
20
- dependencies: []
21
-
12
+ date: 2012-02-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &70337698930980 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70337698930980
22
25
  description: Check Google Pagerank
23
26
  email:
24
27
  executables: []
25
-
26
28
  extensions: []
27
-
28
29
  extra_rdoc_files: []
29
-
30
- files:
30
+ files:
31
31
  - README
32
- - init.rb
33
32
  - lib/google_pr.rb
34
33
  - test/pagerank_checker_test.rb
35
- has_rdoc: true
36
34
  homepage: https://github.com/veilleperso/google_pr
37
35
  licenses: []
38
-
39
36
  post_install_message:
40
37
  rdoc_options: []
41
-
42
- require_paths:
38
+ require_paths:
43
39
  - lib
44
- required_ruby_version: !ruby/object:Gem::Requirement
40
+ required_ruby_version: !ruby/object:Gem::Requirement
45
41
  none: false
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- hash: 3
50
- segments:
51
- - 0
52
- version: "0"
53
- required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
47
  none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
- version: "0"
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
62
52
  requirements: []
63
-
64
53
  rubyforge_project:
65
- rubygems_version: 1.6.2
54
+ rubygems_version: 1.8.15
66
55
  signing_key:
67
56
  specification_version: 3
68
57
  summary: Google PR check
69
58
  test_files: []
70
-
data/init.rb DELETED
@@ -1,2 +0,0 @@
1
- # Include hook code here
2
- require 'google_pr'