Created
October 9, 2010 10:39
-
-
Save koduki/618093 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'mechanize' | |
class RakutenOrder | |
def initialize | |
@agent = WWW::Mechanize.new | |
@agent.user_agent_alias = 'Windows IE 7' | |
end | |
def login id, password | |
@agent = WWW::Mechanize.new | |
@agent.user_agent_alias = 'Windows IE 7' | |
page = @agent.get('https://grp02.id.rakuten.co.jp/rms/nid/login') | |
page.forms[1].fields_with(:name => 'u').first.value = id | |
page.forms[1].fields_with(:name => 'p').first.value = password | |
page = page.forms[1].click_button | |
end | |
def get_items | |
page = @agent.get 'https://order.my.rakuten.co.jp/' | |
xpath = "//select[@name='display_span']/option/@value" | |
years = page.search(xpath).map{|x| x.value.to_i }.select{|x| x != 0 } | |
years.reduce([]){|items, year| items + get_items_by(year)} | |
end | |
def get_items_by year | |
page = @agent.get("https://order.my.rakuten.co.jp/?page=myorder&act=window&page_num=1&display_span=#{year.to_s}&display_month=all&search_item=&search_shop=&page=myorder&page_num=") | |
parse page | |
end | |
def parse page | |
link = page.links.select{|link| link.text =~ /次の\d+件/}.first | |
to_items(page) + ((link) ? parse(link.click) : []) | |
end | |
def to_items page | |
def date d | |
ds = d.split(/\D/).map{|x| x.to_i} | |
Date.new(*ds) | |
end | |
def money value | |
value.gsub(/\D/,'').to_i | |
end | |
page.search('div.itemBlock').map do |item| | |
{ | |
name:item.at('.itemName').text, | |
date:date(item.at('.day').text), | |
value:money(item.search('dd')[1].text), | |
shop:item.search('dd')[2].text, | |
link:item.at('a @href').value, | |
} | |
end | |
end | |
def get_books | |
def isbn13_to_10 isbn13 | |
isbn10 = isbn13[3..11] | |
check_digit = 0 | |
isbn10.split(//).each_with_index do |chr, idx| | |
check_digit += chr.to_i * (10 - idx) | |
end | |
check_digit = 11 - (check_digit % 11) | |
check_digit = "X" if check_digit == 10 | |
check_digit = 0 if check_digit == 11 | |
"#{isbn10}#{check_digit}" | |
end | |
items = get_items | |
items.select{|x| x[:shop] == '楽天ブックス'}.map{ |item| | |
begin | |
page = @agent.get(item[:link]) | |
xpath = '/html/body/div/div[2]/div/div/div/div/div/div[2]/ul/li/dl/dd' | |
item[:isbn] = page.search(xpath).last.text | |
sleep 1 | |
rescue | |
puts "error item:#{item[:name]}" | |
end | |
}.select{|book| book[:isbn] =~ /\d+/}.map{|book| | |
book[:isbn10] = isbn13_to_10(book[:isbn]) | |
book | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment