seotools 0.1.0

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.
@@ -0,0 +1,4 @@
1
+ == 0.1.0 2007-12-02
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 FIXME full name
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ lib/seotools.rb
9
+ lib/seotools/engine.rb
10
+ lib/seotools/engine/base.rb
11
+ lib/seotools/engine/google.rb
12
+ lib/seotools/engine/google/pagerank.rb
13
+ lib/seotools/engine/yahoo.rb
14
+ lib/seotools/site.rb
15
+ lib/seotools/version.rb
16
+ log/debug.log
17
+ script/destroy
18
+ script/generate
19
+ script/makemanifest.rb
20
+ script/txt2html
21
+ setup.rb
22
+ tasks/deployment.rake
23
+ tasks/environment.rake
24
+ tasks/website.rake
25
+ test/test_helper.rb
26
+ test/test_seotools.rb
27
+ website/index.html
28
+ website/index.txt
29
+ website/javascripts/rounded_corners_lite.inc.js
30
+ website/stylesheets/screen.css
31
+ website/template.rhtml
@@ -0,0 +1,32 @@
1
+ = SeoTools
2
+
3
+ SEOに役立つ機能の詰め合わせ、の予定です。
4
+ 趣味(と実益)のために作っているものなので、
5
+ 「欲しいなー」と思った機能を実装していきます。
6
+
7
+ 要望、バグ報告等はお気軽に。
8
+ (でも対応できるとは限らないよ)
9
+
10
+ == 使い方
11
+
12
+ # gemでいれてたら必要
13
+ # require 'rubygems'
14
+ require 'seotools'
15
+ site = SeoTools('http://example.com')
16
+ # 以下、:googleを:yahooに変えると、YSTの結果を取得できます。
17
+ site.get_rank(:google, keyword) # => Google順位
18
+ site.get_page_rank # => ページランク(Google)
19
+ site.get_number_of_index_results(:google) # => Googleインデックス数
20
+ # 別名もあります。
21
+ site.rank(:google, keyword)
22
+ site.page_rank
23
+ site.number_of_index_results(:google)
24
+ # 使用する検索エンジンの名前のメソッドもあります。(別名も同様に)
25
+ site.get_rank_with_google(keyword)
26
+ site.get_number_of_index_results_with_google(keyword)
27
+
28
+ == 作成者
29
+ Yuichi Takeuchi
30
+
31
+ == ライセンス
32
+ Ruby License
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,72 @@
1
+ require 'seotools/version'
2
+
3
+ AUTHOR = 'Yuichi Takeuchi / taslam' # can also be an array of Authors
4
+ EMAIL = "my_name_is_takeyu@hotmail.com"
5
+ DESCRIPTION = "SEOに便利な機能の詰め合わせ(予定)"
6
+ GEM_NAME = 'seotools' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'seotools' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "unknown"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
+ VERS = Seotools::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'seotools documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.author = AUTHOR
52
+ p.description = DESCRIPTION
53
+ p.email = EMAIL
54
+ p.summary = DESCRIPTION
55
+ p.url = HOMEPATH
56
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
57
+ p.test_globs = ["test/**/test_*.rb"]
58
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
59
+
60
+ # == Optional
61
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
62
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
63
+ p.extra_deps = [['hpricot', '>=0.6']]
64
+
65
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
66
+
67
+ end
68
+
69
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
70
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
71
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
72
+ hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,17 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16
+
17
+ require 'seotools'
@@ -0,0 +1,15 @@
1
+ module SeoTools
2
+ def self.new(*args)
3
+ SeoTools::Site.new(*args)
4
+ end
5
+ end
6
+
7
+ module Kernel
8
+ # alias for SeoTools.new.
9
+ def SeoTools(*args) # :doc:
10
+ SeoTools.new(*args)
11
+ end
12
+ module_function :SeoTools
13
+ end
14
+
15
+ require 'seotools/site'
@@ -0,0 +1,20 @@
1
+ module SeoTools::Engine
2
+ def self.engines
3
+ self.constants.collect { |constant|
4
+ self.const_get(constant).is_a?(Class) && constant != "Base" ? constant.downcase.to_sym : nil
5
+ }.compact
6
+ end
7
+
8
+ def self.get_rank(engine, uri, keyword, options)
9
+ self.const_get(engine.to_s.capitalize).get_rank(uri, keyword, options)
10
+ end
11
+
12
+ def self.get_number_of_index_results(engine, uri)
13
+ self.const_get(engine.to_s.capitalize).get_number_of_index_results(uri)
14
+ end
15
+
16
+ end
17
+
18
+ Dir.glob(File.join(File.dirname(__FILE__), 'engine') + '/*.rb') do |name|
19
+ require name
20
+ end
@@ -0,0 +1,44 @@
1
+ require 'cgi'
2
+ require 'open-uri'
3
+ require 'hpricot'
4
+ #
5
+ # 抽象クラス
6
+ # 継承して各検索エンジンの解析クラスをつくる
7
+ #
8
+ class SeoTools::Engine::Base
9
+ def self.uri
10
+ "http://[%keyword%]"
11
+ end
12
+
13
+ def self.build_query(keyword, page = 1, uri = nil)
14
+ uri ||= self.uri
15
+ uri.sub(Regexp.new(Regexp.quote('[%keyword%]')), self.build_keyword(keyword))
16
+ end
17
+
18
+ def self.build_keyword(keyword)
19
+ CGI.escape(keyword.split(/\s/).join('+'))
20
+ end
21
+
22
+ def self.request(keyword, page = 1, uri = nil, &block)
23
+ open(self.build_query(keyword, page, uri)) do |f|
24
+ yield Hpricot(f.read)
25
+ end
26
+ end
27
+
28
+ def self.get_rank(uri, keyword, options = { })
29
+ options = {
30
+ :limit => 100
31
+ }.merge(options)
32
+
33
+ self.search(uri, keyword, options)
34
+ end
35
+
36
+ def self.get_number_of_index_results(uri)
37
+ 0
38
+ end
39
+
40
+ private
41
+ def self.search(uri, keyword, options)
42
+ end
43
+
44
+ end
@@ -0,0 +1,41 @@
1
+ require 'seotools/engine/base'
2
+
3
+ class SeoTools::Engine::Google < SeoTools::Engine::Base
4
+ def self.uri
5
+ "http://www.google.co.jp/search?lr=lang_ja&ie=utf-8&oe=utf-8&q=[%keyword%]&offeset=[%offset%]&num=100"
6
+ end
7
+
8
+ def self.build_query(keyword, page = 1, uri = nil)
9
+ query = super
10
+ offset = (page - 1) * 100
11
+ query.sub(Regexp.new(Regexp.quote('[%offset%]')), offset.to_s)
12
+ end
13
+
14
+ private
15
+
16
+ def self.search(uri, keyword, options)
17
+ rank = 1
18
+ 10.times do |i|
19
+ self.request(keyword, i + 1) do |doc|
20
+ doc.search('//h2[@class="r"]/a').each do |a|
21
+ return rank if Regexp.new("^#{Regexp.quote(uri)}/?") =~ a.get_attribute('href')
22
+ rank += 1
23
+ return nil if rank > options[:limit]
24
+ end
25
+ end
26
+ end
27
+ nil
28
+ end
29
+
30
+ def self.get_number_of_index_results(uri)
31
+ self.request("site:#{uri}") do |doc|
32
+ doc.search('//table').each do |table|
33
+ return $1.gsub(/,/, '').to_i if table.to_html =~ /検索結果 <b>((\d|\,)+)<\/b>/
34
+ end
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ require 'seotools/engine/google/pagerank'
41
+
@@ -0,0 +1,76 @@
1
+ # GooglePageRank
2
+ # http://blogoscoped.com/archive/2004_06_27_index.html#108834386239051706
3
+ require 'open-uri'
4
+ class SeoTools::Engine::Google
5
+ module PageRank
6
+
7
+ GOOGLE_MAGICK = 0xE6359A60
8
+
9
+ def get_page_rank(uri)
10
+ ch = sprintf("6%u", self.get_check_sum(uri))
11
+ 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)
12
+ open(query_uri) do |f|
13
+ body = f.read
14
+ # Rank_1:1:3
15
+ return $1.to_i if body =~ /Rank_1:1:(\d)/
16
+ end
17
+ nil
18
+ # rescue
19
+ # nil
20
+ end
21
+ alias :page_rank :get_page_rank
22
+
23
+ def get_check_sum(uri)
24
+ a = b = 0x9E3779B9
25
+ c = GOOGLE_MAGICK
26
+ k = 0
27
+ iuri = "info:#{uri}"
28
+ len = iuri.length
29
+ while len >= k + 12
30
+ a = (a + chars_to_i(iuri, k))
31
+ b = (b + chars_to_i(iuri, k+4))
32
+ c = (c + chars_to_i(iuri, k+8))
33
+ a, b, c = mix(a, b, c)
34
+ k = k + 12
35
+ end
36
+ a = (a + chars_to_i(iuri, k))
37
+ b = (b + chars_to_i(iuri, k + 4))
38
+ c = (c + (chars_to_i(iuri, k + 8) << 8) + len)
39
+ a, b, c = mix(a, b, c)
40
+ return c
41
+ end
42
+ alias :check_sum :get_check_sum
43
+
44
+ def chars_to_i(str, k)
45
+ i = 0
46
+ substr = str.slice(k..str.length)
47
+ return 0 unless substr
48
+ substr.unpack('C4').compact.each_with_index do |value, index|
49
+ i += value << (index * 8)
50
+ end
51
+ return i
52
+ end
53
+
54
+ MODULO = 0x100000000
55
+ def mix(a, b, c)
56
+ a, b, c = a % MODULO, b % MODULO, c % MODULO
57
+ a = (((a + (MODULO - b) + (MODULO - c)) % MODULO) ^ zero_fill(c, 13)) % MODULO
58
+ b = (((b + (MODULO - c) + (MODULO - a)) % MODULO) ^ (a << 8)) % MODULO
59
+ c = (((c + (MODULO - a) + (MODULO - b)) % MODULO) ^ zero_fill(b, 13)) % MODULO
60
+ a = (((a + (MODULO - b) + (MODULO - c)) % MODULO) ^ zero_fill(c, 12)) % MODULO
61
+ b = (((b + (MODULO - c) + (MODULO - a)) % MODULO) ^ (a << 16)) % MODULO
62
+ c = (((c + (MODULO - a) + (MODULO - b)) % MODULO) ^ zero_fill(b, 5)) % MODULO
63
+ a = (((a + (MODULO - b) + (MODULO - c)) % MODULO) ^ zero_fill(c, 3)) % MODULO
64
+ b = (((b + (MODULO - c) + (MODULO - a)) % MODULO) ^ (a << 10)) % MODULO
65
+ c = (((c + (MODULO - a) + (MODULO - b)) % MODULO) ^ zero_fill(b, 15)) % MODULO
66
+ return [a, b, c]
67
+ end
68
+
69
+ def zero_fill(a, b)
70
+ (a >> b) % MODULO
71
+ end
72
+
73
+ end
74
+
75
+ extend PageRank
76
+ end
@@ -0,0 +1,38 @@
1
+ require 'seotools/engine/base'
2
+
3
+ class SeoTools::Engine::Yahoo < SeoTools::Engine::Base
4
+ def self.uri
5
+ "http://search.yahoo.co.jp/search?p=[%keyword%]&ei=UTF-8&n=100&b=[%offset%]"
6
+ end
7
+
8
+ def self.build_query(keyword, page = 1, uri = nil)
9
+ query = super
10
+ offset = (page - 1) * 100
11
+ query.sub(Regexp.new(Regexp.quote('[%offset%]')), offset.to_s)
12
+ end
13
+
14
+ private
15
+
16
+ def self.get_number_of_index_results(uri)
17
+ self.request(uri, 1, 'http://siteexplorer.search.yahoo.com/advsearch?ei=UTF-8&p=[%keyword%]&bwm=p&bwms=p&searchbwm=Explore+URL') do |doc|
18
+ doc.search('//div[@id="yschinfo"]').each do |div|
19
+ return $1.gsub(/,/, '').to_i if div.to_html =~ /<strong>1 - 100<\/strong> of about <strong>((\d|,)+)/
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.search(uri, keyword, options)
25
+ rank = 1
26
+ 10.times do |i|
27
+ self.request(keyword, i + 1) do |doc|
28
+ doc.search('//div[@class="ttl"]/a[@class="yschttl"]').each do |a|
29
+ return rank if Regexp.new("#{Regexp.quote(uri.sub(/:/, '%3A'))}") =~ a.get_attribute('href')
30
+ rank += 1
31
+ return nil if rank > options[:limit]
32
+ end
33
+ end
34
+ end
35
+ nil
36
+ end
37
+
38
+ end
@@ -0,0 +1,66 @@
1
+ require 'uri'
2
+ require 'seotools/engine'
3
+
4
+ class SeoTools::Site
5
+ # サイトのURI
6
+ attr_reader :uri
7
+
8
+ def initialize(*args)
9
+ case args[0]
10
+ when String
11
+ @uri = args[0]
12
+ when URI
13
+ @uri = args[0].to_s
14
+ else
15
+ raise ArgumentError.new("引数はStringかURIのインスタンスを指定してください。")
16
+ end
17
+ end
18
+
19
+ def to_uri
20
+ URI(self.uri)
21
+ end
22
+
23
+ #
24
+ # 検索順位を取得する
25
+ # 引数
26
+ # - keyword 半角スペースで複数指定可能
27
+ # - engine 検索する検索エンジンの識別子をシンボルで指定する
28
+ # - options 順位取得のオプション
29
+ #
30
+ def get_rank(engine, keyword, options = { })
31
+ SeoTools::Engine.get_rank(engine, self.uri, keyword, options)
32
+ end
33
+ alias :rank :get_rank
34
+
35
+ #
36
+ # ページランクを取得する
37
+ #
38
+ def get_page_rank
39
+ SeoTools::Engine::Google.get_page_rank(self.uri)
40
+ end
41
+ alias :page_rank :get_page_rank
42
+
43
+ #
44
+ # インデックス数を取得する
45
+ #
46
+ def get_number_of_index_results(engine)
47
+ SeoTools::Engine.get_number_of_index_results(engine, self.uri)
48
+ end
49
+ alias :number_of_index_results :get_number_of_index_results
50
+
51
+ end
52
+
53
+ SeoTools::Engine.engines.each do |engine|
54
+ SeoTools::Site.class_eval <<-EVAL
55
+ def get_rank_with_#{engine}(keyword, options = { })
56
+ get_rank(#{engine.inspect}, keyword, options)
57
+ end
58
+ alias :rank_with_#{engine} :get_rank_with_#{engine}
59
+
60
+ def get_number_of_index_results_with_#{engine}
61
+ get_number_of_index_results(#{engine.inspect})
62
+ end
63
+ alias :number_of_index_results_with_#{engine} :get_number_of_index_results_with_#{engine}
64
+
65
+ EVAL
66
+ end