keyword_rankr 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/keyword_rankr.rb +125 -0
  2. metadata +46 -0
@@ -0,0 +1,125 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+ require 'net/http'
4
+
5
+ class Array
6
+ def extract_options!
7
+ last.is_a?(::Hash) ? pop : {}
8
+ end
9
+ end
10
+
11
+ class Ranking
12
+ @@default_options = { :limit => 200, :res_per_page => 10, :supported_engines => [:bing, :bingIND, :bingCA ,:bingUK,:bingFR , :google, :googleUS, :yahoo, :googleUSA, :googleIND, :googleFR, :googleCA, :yahooIND, :yahooFR, :yahooCA ] } #careful, res_per_page doesn't work with more than 10 for all search engines!
13
+
14
+ attr_reader :options
15
+
16
+ def initialize(*options) # keyword, url, limit
17
+ self.options = options.extract_options!
18
+ end
19
+
20
+ def options=(options)
21
+ @options = @@default_options.merge(options)
22
+ @options[:url].gsub!(/(http:\/\/|https:\/\/)/, '')
23
+ @options[:url].gsub!(/^www/, '')
24
+ end
25
+
26
+ def from
27
+ validate_options
28
+ @finder ||= Finder.new
29
+ @finder.find(@options)
30
+ end
31
+
32
+ def method_missing(method, *args, &block)
33
+ engine = @@default_options[:supported_engines].find{ |name| ("from_" << name.to_s).match /#{method}/ }
34
+ if engine
35
+ @options.merge!({:engine => engine})
36
+ send("from")
37
+ else
38
+ super(method, *args, &block)
39
+ end
40
+ end
41
+
42
+ protected
43
+ def validate_options
44
+ raise "Keyword and site parameters must be Strings" unless @options[:keyword].is_a?(String) and @options[:url].is_a?(String)
45
+ raise "Engine should be 'bing', 'google' or 'yahoo'" unless @@default_options[:supported_engines].include?(@options[:engine].to_sym)
46
+ end
47
+
48
+ end
49
+
50
+ class Finder
51
+
52
+ def find(options) #keyword, url, limit, engine, res_per_page
53
+ options[:keyword].gsub!(/\s/, '+')
54
+ request_url, results_selector, cite_selector = case options[:engine].to_sym
55
+ when :bing
56
+ ["http://www.bing.com/search?q=#{options[:keyword]}&count=#{options[:res_per_page]}&first=", '#wg0 > li', 'cite']
57
+ when :google
58
+ ["http://www.google.co.uk/search?q=#{options[:keyword]}&num=#{options[:res_per_page]}&start=", '#ires > ol > li', 'cite']
59
+ when :googleUS
60
+ ["http://www.google.com/search?q=#{options[:keyword]}&num=#{options[:res_per_page]}&start=", '#ires > ol > li', 'cite']
61
+ when :yahoo
62
+ ["http://search.yahoo.com/search?p=#{options[:keyword]}&n=#{options[:res_per_page]}&b=", '#web > ol > li', 'span']
63
+ when :googleIND
64
+ ["http://www.google.co.in/search?q=#{options[:keyword]}&num=#{options[:res_per_page]}&start=", '#ires > ol > li', 'cite']
65
+ when :googleFR
66
+ ["http://www.google.fr/search?q=#{options[:keyword]}&num=#{options[:res_per_page]}&start=", '#ires > ol > li', 'cite']
67
+ when :googleUSA
68
+ ["http://www.google.com/search?q=#{options[:keyword]}&num=#{options[:res_per_page]}&start=", '#ires > ol > li', 'cite']
69
+ when :googleCA
70
+ ["http://www.google.ca/search?q=#{options[:keyword]}&num=#{options[:res_per_page]}&cc=ca&start=", '#ires > ol > li', 'cite']
71
+ when :bingIND
72
+ ["http://www.bing.com/search?q=#{options[:keyword]}&count=#{options[:res_per_page]}&cc=in&first=", '#wg0 > li', 'cite']
73
+ when :bingCA
74
+ ["http://www.bing.com/search?q=#{options[:keyword]}&count=#{options[:res_per_page]}&cc=ca&first=", '#wg0 > li', 'cite']
75
+ when :bingFR
76
+ ["http://www.bing.com/search?q=#{options[:keyword]}&count=#{options[:res_per_page]}&cc=fr&first=", '#wg0 > li', 'cite']
77
+ when :bingUK
78
+ ["http://www.bing.com/search?q=#{options[:keyword]}&count=#{options[:res_per_page]}&cc=uk&first=", '#wg0 > li', 'cite']
79
+ when :yahooIND
80
+ ["http://in.search.yahoo.com/search?p=#{options[:keyword]}&n=#{options[:res_per_page]}&b=", '#web > ol > li', 'span']
81
+ when :yahooFR
82
+ ["http://fr.search.yahoo.com/search?p=#{options[:keyword]}&n=#{options[:res_per_page]}&b=", '#web > ol > li', 'span']
83
+ when :yahooCA
84
+ ["http://ca.search.yahoo.com/search?p=#{options[:keyword]}&n=#{options[:res_per_page]}&b=", '#web > ol > li', 'span']
85
+ when :yahoo
86
+ ["http://search.yahoo.com/search?p=#{options[:keyword]}&n=#{options[:res_per_page]}&b=", '#web > ol > li', 'span']
87
+ end
88
+
89
+
90
+ count, rank = 0, nil
91
+
92
+ loop {
93
+ html_response = Net::HTTP.get_response(URI.parse("#{request_url}#{count}")).body
94
+ html_results = Nokogiri.parse(html_response).css(results_selector)
95
+ @var = html_results.detect{ |result| result.css("#productbox") }
96
+
97
+ if @var == "#productbox"
98
+ #if html_results.index(html_results.detect{ |result| result.css(cite_selector).text.match Regexp.new(options[:url]) })
99
+
100
+ rank = html_results.index(html_results.detect{ |result| result.css(cite_selector).text.match Regexp.new(options[:url]) })
101
+
102
+ if count > options[:limit]
103
+ break
104
+ elsif rank
105
+ rank += count - 1
106
+ break
107
+ end
108
+ count += options[:res_per_page]
109
+
110
+ else
111
+ rank = html_results.index(html_results.detect{ |result| result.css(cite_selector).text.match Regexp.new(options[:url]) })
112
+
113
+ if count > options[:limit]
114
+ break
115
+ elsif rank
116
+ rank += count
117
+ break
118
+ end
119
+ count += options[:res_per_page]
120
+ end#If HTML
121
+ }
122
+ rank ? rank.next : nil
123
+ end
124
+
125
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keyword_rankr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Amit Koranne
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: modification of keyword_ranking. Which displays, the Google Yahoo Bing
15
+ UK INDIA CANADA FRANCE Result for the Given Keyword(s)
16
+ email: aakoranne@rediff.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/keyword_rankr.rb
22
+ homepage: http://rubygems.org/gems/keyword_rankr
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.24
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Get Keyword Ranking for Google Yahoo Bing UK INDIA CANADA FRANCEA!
46
+ test_files: []