Class: Amazon2irc::AmazonMechanize

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon2irc.rb

Class Method Summary collapse

Class Method Details

.scan(keyword) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/amazon2irc.rb', line 121

def self.scan keyword
  begin
    items = []
    agent = Mechanize.new
    agent.max_history = nil # unlimited history
    html = agent.get("https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dprime-day&field-keywords=#{keyword}")
    
    loop do
        doc = Nokogiri::HTML::Document.parse(html.body)

        doc.xpath('//*[@class="s-item-container"]').each do |item|
          last_title=''
          offer_link=''
          item.css('a').each do |a|
            last_title=a['title'] unless a['title'].to_s.length == 0
            offer_link = a['href'] unless (a.to_s.length == 0 || (a['href'].include?("offer")) || (a['href'].include?("Reviews"))  || (a['href'].include?("void(0)")) || (a['href'].include?("Promotions")) )
          end
          items.push("#{last_title} : #{offer_link}") if last_title.downcase.include? keyword.downcase
        end

        next_page=false
        html.links.each do |l|
          next_page=true if l.text.include? 'Next Page'
          sleep 5 if l.text.include? 'Next Page'
          html = l.click if ((l.text.include? 'Next Page') && (l.href.to_s.length > 10))
          next  if l.text.include? 'Next Page'
        end
      return items if next_page == false
    end
  rescue => error
    puts "Error! #{error}"
    return items
  end
end