class Amazon2irc::AmazonMechanize

Public Class Methods

scan(keyword) click to toggle source
# 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