crown 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/ChangeLog +5 -1
  2. data/VERSION +1 -1
  3. data/crown.gemspec +2 -1
  4. data/lib/crown/amazon.rb +138 -0
  5. metadata +4 -3
data/ChangeLog CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.0.7 / 2011-04-03
2
+
3
+ * bugfix
4
+
1
5
  == 0.0.6 / 2011-04-03
2
6
 
3
7
  * add Amazon module
@@ -8,7 +12,7 @@
8
12
 
9
13
  == 0.0.4 / 2011-02-19
10
14
 
11
- * bug fix
15
+ * bugfix
12
16
  * add Livedoor Reader module
13
17
 
14
18
  == 0.0.3 / 2011-02-19
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{crown}
8
- s.version = "0.0.6"
8
+ s.version = "0.0.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["clown"]
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
34
34
  "example/sbmcount.rb",
35
35
  "example/twcount.rb",
36
36
  "lib/crown.rb",
37
+ "lib/crown/amazon.rb",
37
38
  "lib/crown/backtype.rb",
38
39
  "lib/crown/buzzurl.rb",
39
40
  "lib/crown/delicious.rb",
@@ -0,0 +1,138 @@
1
+ # -*- coding: utf-8 -*-
2
+ # --------------------------------------------------------------------------- #
3
+ #
4
+ # twitter.rb
5
+ #
6
+ # Copyright (c) 2008 - 2011, clown.
7
+ #
8
+ # Redistribution and use in source and binary forms, with or without
9
+ # modification, are permitted provided that the following conditions
10
+ # are met:
11
+ #
12
+ # - Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ # - Redistributions in binary form must reproduce the above copyright
15
+ # notice, this list of conditions and the following disclaimer in the
16
+ # documentation and/or other materials provided with the distribution.
17
+ # - No names of its contributors may be used to endorse or promote
18
+ # products derived from this software without specific prior written
19
+ # permission.
20
+ #
21
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27
+ # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+ #
33
+ # --------------------------------------------------------------------------- #
34
+ module Crown
35
+ module Amazon
36
+ require 'uri'
37
+ require 'net/http'
38
+ require 'crown/http'
39
+ require 'rubygems'
40
+ require 'amazon/ecs'
41
+ require 'nokogiri'
42
+ Net::HTTP.version_1_2
43
+
44
+ # ------------------------------------------------------------------- #
45
+ #
46
+ # AsinExtractor
47
+ #
48
+ # 指定した URI の a タグ href 属性から Amazon の ASIN と思われる
49
+ # 情報を抽出するクラス.
50
+ #
51
+ # ------------------------------------------------------------------- #
52
+ class AsinExtractor
53
+ def initialize(proxy_host = nil, proxy_port = nil)
54
+ @proxy_host = proxy_host
55
+ @proxy_port = proxy_port
56
+ end
57
+
58
+ # --------------------------------------------------------------- #
59
+ # get
60
+ # --------------------------------------------------------------- #
61
+ def get(uri)
62
+ result = Array.new
63
+
64
+ parser = URI.parse(uri.strip)
65
+ path = parser.path
66
+ path += parser.query if (parser.query != nil)
67
+
68
+ session = Net::HTTP.new(parser.host, parser.port, @proxy_host, @proxy_port)
69
+ response = Crown::HTTP.get(session, path)
70
+ return result if (response == nil || response.code.to_i != 200)
71
+
72
+ html = Nokogiri::HTML.parse(response.body)
73
+ html.search("a").each { |node|
74
+ next if (node['href'] == nil)
75
+
76
+ begin
77
+ parser = URI.parse(node['href'].strip)
78
+ rescue URI::InvalidURIError
79
+ parser = URI.parse(URI.encode(node['href'].strip))
80
+ end
81
+ next if (parser == nil || parser.host == nil || parser.path == nil)
82
+
83
+ if (parser.host.match(/^(?:www\.)?amazon\.(?:com|ca|co\.uk|de|co\.jp|jp|fr|cn)$/) != nil)
84
+ asin = get_asin(parser.path, parser.query)
85
+ result.push(asin) if (asin != nil)
86
+ end
87
+ }
88
+
89
+ return result
90
+ end
91
+
92
+ private
93
+ # --------------------------------------------------------------- #
94
+ #
95
+ # get_asin
96
+ #
97
+ # パスおよびクエリーから ASIN を抽出する.推測方法は,
98
+ # /[B0123489][A-Z0-9]{9}/ にマッチする文字列を探すと言う方法を
99
+ # 採用している.
100
+ #
101
+ # --------------------------------------------------------------- #
102
+ def get_asin(path, query)
103
+ path.split('/').each { |item|
104
+ asin = item.match(/[B0123489][A-Z0-9]{9}/)
105
+ return asin[0] if (asin != nil)
106
+ }
107
+
108
+ if (query != nil)
109
+ asin = query.match(/[B0123489][A-Z0-9]{9}/)
110
+ return asin[0] if (asin != nil)
111
+ end
112
+
113
+ return nil
114
+ end
115
+ end
116
+
117
+ # ------------------------------------------------------------------- #
118
+ #
119
+ # each
120
+ #
121
+ # AsinExtractor クラスを使用して指定された URL に存在する
122
+ # ASIN コードを抽出し,Amazon::Ecs を用いてそれらの情報を
123
+ # 取得する
124
+ #
125
+ # ------------------------------------------------------------------- #
126
+ def each(uri, options = {})
127
+ AsinExtractor.new.get(uri).each { |asin|
128
+ result = ::Amazon::Ecs.item_lookup(asin, options)
129
+ yield(result.first_item) if (result != nil && result.items.length > 0)
130
+ }
131
+ end
132
+
133
+ # ------------------------------------------------------------------- #
134
+ # module functions
135
+ # ------------------------------------------------------------------- #
136
+ module_function :each
137
+ end # Amazon
138
+ end # Crown
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crown
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - clown
@@ -100,6 +100,7 @@ files:
100
100
  - example/sbmcount.rb
101
101
  - example/twcount.rb
102
102
  - lib/crown.rb
103
+ - lib/crown/amazon.rb
103
104
  - lib/crown/backtype.rb
104
105
  - lib/crown/buzzurl.rb
105
106
  - lib/crown/delicious.rb