public
Last active

  • Download Gist
rakuten_point_calender.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
# -*- 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.