Created
November 15, 2010 17:27
-
-
Save koduki/700636 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' | |
require 'icalendar' | |
class RakutenPointCalender | |
def initialize | |
@agent = WWW::Mechanize.new | |
@agent.user_agent_alias = 'Windows IE 7' | |
@cal = Icalendar::Calendar.new | |
# タイムゾーン (VTIMEZONE) を作成 | |
@cal.timezone do | |
tzid 'Asia/Tokyo' | |
standard do | |
tzoffsetfrom '+0900' | |
tzoffsetto '+0900' | |
dtstart '19700101T000000' | |
tzname 'JST' | |
end | |
end | |
end | |
def login id, password | |
page = @agent.get('https://grp02.id.rakuten.co.jp/rms/nid/vc?__event=login&service_id=i47&return_url=Top/TopDisplay/') | |
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 parse_date td | |
tmp = td.children[1].children.map{|x| x.text} | |
ds = ([tmp[0]] + tmp[2].split(/\D/)).map{|x| x.to_i} | |
Date.new(*ds) | |
end | |
def get_items | |
page = @agent.get 'https://point.rakuten.co.jp/PointHistory/PointHistoryDisplay/' | |
trs = page.search('//html/body/div/div[2]/div/div[3]/table[2]/tbody/tr')[2..-1] | |
items = trs.map do |item| | |
{ | |
date:parse_date(item.at('td[1]')), | |
service:item.at('td[2]').text.strip, | |
contents:item.at('td[3]').text.strip.gsub(/\t/, '').gsub(/\n/, ' ') , | |
point:item.at('td[4]').text.strip.to_i, | |
usedPoint:item.at('td[5]').text.strip.to_i, | |
status:item.at('td[6]').text.strip, | |
period:item.at('td[7]').text.strip, | |
} | |
end | |
end | |
def select_campaign items | |
date = lambda do |d| | |
ds = d.split(/\D/).map{|x| x.to_i} | |
Date.new(*ds) | |
end | |
items.select{|item| item[:service] == 'キャンペーン' && item[:period] != ''} | |
.map{|item| item[:period] = date[item[:period].sub('まで', '').sub('23:59', '')];item} | |
end | |
def add_event dstart, dend, title, desc | |
@cal.event do | |
dtstart dstart | |
dtend dend | |
summary title | |
description desc | |
end | |
end | |
def to_ical | |
select_campaign(get_items).map do |campaign| | |
add_event campaign[:date], campaign[:period], 'ポイント期限:' + campaign[:period].to_s + 'まで', campaign[:contents] + " " + campaign[:point].to_s + " ポイント" | |
end | |
@cal.to_ical | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment