Skip to content

Instantly share code, notes, and snippets.

@koduki
Created October 9, 2010 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koduki/618331 to your computer and use it in GitHub Desktop.
Save koduki/618331 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'mechanize'
class AmazonOrder
def initialize
@agent = WWW::Mechanize.new
@agent.user_agent_alias = 'Windows IE 7'
end
def login id, password
page = @agent.get('https://www.amazon.co.jp/gp/sign-in.html')
page.forms[1].fields_with(:name => 'email').first.value = id
page.forms[1].fields_with(:name => 'password').first.value = password
page = page.forms[1].click_button
end
def get_items
page = @agent.get 'https://www.amazon.co.jp/gp/css/history/orders/view.html'
years = page.search('#orderFilter option @value').select{|x| x.value =~ /year-/}.map{|x| x.value.split('-')[1]}
years.reduce([]){|r, year| r + get_items_by(year) }.uniq{|x| x[:isbn10]}
end
def get_items_by year
sleep(1)
a_url = "https://www.amazon.co.jp/gp/css/history/orders/view.html?opt=ab&groupID=0&orderFilter=year-#{year}&x=20&y=4"
page = @agent.get a_url
len = (page.at('/html/body/table[3]/tbody/tr/td/div/b[2]').text.to_f / 10).ceil
(1..len).reduce([]) do |r, i|
page = @agent.get a_url + "&startAtIndex=#{i*10}"
page.encoding = 'sjis'
r + to_items(page)
end
end
def parse page
next_btn = page.at('/html/body/table[3]/tbody/tr/td[2]/a/@href')
to_items(page) + if next_btn
parse page.links.select{|link| link.href == next_btn.text}.first.click
else
[]
end
end
def to_items page
page.search('li.item').map do |item|
{
name:item.at('a').text,
isbn10:item.at('a @href').value.split('/')[5]
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment