google_pr 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,24 @@
1
+ GooglePR
2
+ ===============
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
+ Example
11
+ =======
12
+
13
+ GooglePR.new("http://www.microsoft.com").page_rank
14
+
15
+ Specs
16
+ =====
17
+
18
+ Run this after each improvement in this project
19
+ spec spec/pagerank_checker_specs.rb
20
+ 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/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'google_pr'
data/lib/google_pr.rb ADDED
@@ -0,0 +1,92 @@
1
+ # PagerankChecker
2
+
3
+ # (C) 2006-2007 under terms of LGPL v2.1
4
+ # by Vsevolod S. Balashov <vsevolod@balashov.name>
5
+ # based on 3rd party code snippets (see comments)
6
+ #
7
+ # transformed as a rails plugin by Olivier Ruffin (http://www.veilleperso.com)
8
+ #
9
+
10
+ require 'uri'
11
+ require 'open-uri'
12
+
13
+ # http://blog.outer-court.com/archive/2004_06_27_index.html#108834386239051706
14
+ class AutomatedQueryError < StandardError; end;
15
+
16
+ class GooglePR
17
+
18
+ M=0x100000000 # modulo for unsigned int 32bit(4byte)
19
+ attr_accessor :uri
20
+
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
26
+
27
+ def m1(a,b,c,d)
28
+ (((a+(M-b)+(M-c))%M)^(d%M))%M # mix/power mod
29
+ end
30
+
31
+ def i2c(i)
32
+ [i&0xff, i>>8&0xff, i>>16&0xff, i>>24&0xff]
33
+ end
34
+
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
+
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
46
+
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
55
+ 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
+ end
70
+
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/search?client=navclient-auto&hl=en&ch=#{cn}&ie=UTF-8&oe=UTF-8&features=Rank&q=info:#{URI.escape(@uri, /[^-.!~*'()a-zA-Z\d]/)}"
76
+ end
77
+
78
+ # 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
89
+ end
90
+
91
+ private :m1, :i2c, :c2i, :mix, :old_cn
92
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+
3
+ class PagerankCheckerTest < Test::Unit::TestCase
4
+ # Replace this with your real tests.
5
+ def test_this_plugin
6
+ flunk
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_pr
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors: []
13
+
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-17 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Check Google Pagerank
23
+ email:
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README
32
+ - init.rb
33
+ - lib/google_pr.rb
34
+ - test/pagerank_checker_test.rb
35
+ has_rdoc: true
36
+ homepage: https://github.com/veilleperso/google_pr
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ 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
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.4.2
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Google PR check
69
+ test_files: []
70
+