Class: Crown::Amazon::EntryList
- Inherits:
-
Object
- Object
- Crown::Amazon::EntryList
- Defined in:
- lib/crown/amazon/entrylist.rb
Overview
——————————————————————- #
EntryList
指定した URI の a タグ href 属性から Amazon の ASIN と思われる
情報を抽出し,Amazon の商品情報を取得するクラス.
——————————————————————- #
Instance Attribute Summary collapse
-
#interval ⇒ Object
————————————————————— # accessors ————————————————————— #.
Class Method Summary collapse
-
.get(uri, options = {}, &block) ⇒ Object
————————————————————— # EntryList.get ————————————————————— #.
Instance Method Summary collapse
-
#asin(uri, options = {}) ⇒ Object
————————————————————— # asin ————————————————————— #.
-
#get(uri, options = {}) ⇒ Object
————————————————————— # get ————————————————————— #.
-
#initialize ⇒ EntryList
constructor
————————————————————— # initialize ————————————————————— #.
Constructor Details
#initialize ⇒ EntryList
————————————————————— #
initialize
————————————————————— #
57 58 59 |
# File 'lib/crown/amazon/entrylist.rb', line 57 def initialize() @interval = 2 end |
Instance Attribute Details
#interval ⇒ Object
————————————————————— #
accessors
————————————————————— #
52 53 54 |
# File 'lib/crown/amazon/entrylist.rb', line 52 def interval @interval end |
Class Method Details
Instance Method Details
#asin(uri, options = {}) ⇒ Object
————————————————————— #
asin
————————————————————— #
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/crown/amazon/entrylist.rb', line 71 def asin(uri, = {}) parser = URI.parse(uri.strip) path = parser.path path += '?' + parser.query if (parser.query != nil) proxy_addr = nil proxy_port = nil if (.class == Hash) proxy_addr = [:proxy_address] if (.has_key?(:proxy_address)) proxy_port = [:proxy_port] if (.has_key?(:proxy_port)) end result = Array.new Crown::HTTPWrapper.start(parser.host, parser.port, proxy_addr, proxy_port) { |session| response = session.get(path) return [] if (response == nil || response.code.to_i != 200) html = Nokogiri::HTML.parse(response.body) html.search('a').each { |node| next if (node['href'] == nil) begin parser = URI.parse(node['href'].strip) rescue URI::InvalidURIError parser = URI.parse(URI.encode(node['href'].strip)) end next if (parser == nil || parser.host == nil || parser.path == nil) if (parser.host.match(/^(?:www\.)?amazon\.(?:com|ca|co\.uk|de|co\.jp|jp|fr|cn)$/) != nil) asin = guess_asin(parser.path, parser.query) if (asin != nil && !result.include?(asin)) yield asin if (block_given?) result.push(asin) end end } } end |
#get(uri, options = {}) ⇒ Object
————————————————————— #
get
————————————————————— #
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/crown/amazon/entrylist.rb', line 113 def get(uri, = {}) result = Array.new asin(uri, ) { |asin| entry = ::Amazon::Ecs.item_lookup(asin, ) if (entry != nil && entry.items.length > 0) yield entry.first_item if (block_given?) result.push(entry) end sleep(@interval) } return result end |