Class: Crown::Amazon::EntryList

Inherits:
Object
  • Object
show all
Defined in:
lib/crown/amazon/entrylist.rb

Overview

——————————————————————- #

EntryList

指定した URI  a タグ href 属性から Amazon  ASIN と思われる
情報を抽出し,Amazon の商品情報を取得するクラス.

——————————————————————- #

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntryList

————————————————————— #

initialize

————————————————————— #



57
58
59
# File 'lib/crown/amazon/entrylist.rb', line 57

def initialize()
    @interval = 2
end

Instance Attribute Details

#intervalObject

————————————————————— #

accessors

————————————————————— #



52
53
54
# File 'lib/crown/amazon/entrylist.rb', line 52

def interval
  @interval
end

Class Method Details

.get(uri, options = {}, &block) ⇒ Object

————————————————————— #

EntryList.get

————————————————————— #



64
65
66
# File 'lib/crown/amazon/entrylist.rb', line 64

def EntryList.get(uri, options = {}, &block)
    return Crown::Amazon::EntryList.new.get(uri, options, &block)
end

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, options = {})
    parser = URI.parse(uri.strip)
    path = parser.path
    path += '?' + parser.query if (parser.query != nil)
    
    proxy_addr = nil
    proxy_port = nil
    if (options.class == Hash)
        proxy_addr = options[:proxy_address] if (options.has_key?(:proxy_address))
        proxy_port = options[:proxy_port] if (options.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, options = {})
    result = Array.new
    asin(uri, options) { |asin|
        entry = ::Amazon::Ecs.item_lookup(asin, options)
        if (entry != nil && entry.items.length > 0)
            yield entry.first_item if (block_given?)
            result.push(entry)
        end
        sleep(@interval)
    }
    
    return result
end