|
# -*- coding: utf-8 -*- |
|
# imastodonにモバマス/デレステイベントの終了時間を送信する |
|
# |
|
# usage: push_imastodon_imascg.rb |
|
require 'rubygems' |
|
require 'bundler' |
|
Bundler.require(:default) |
|
Dotenv.load |
|
|
|
# const |
|
IMAS_CG_URL = 'http://seesaawiki.jp/imascg/' |
|
SLSTAGE_URL = 'https://imascg-slstage-wiki.gamerch.com/' |
|
THEATER_URL = 'https://imasml-theater-wiki.gamerch.com/' |
|
MSTAGE_URL = 'https://wikiwiki.jp/sidemstage/' |
|
UA = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36' #Chrome |
|
|
|
def find_imascg_events(agent) |
|
result = [] |
|
type = 'シンデレラガールズ' |
|
page = agent.get(IMAS_CG_URL) |
|
elements = page.at('.user-area').search('.wiki-section-2') |
|
elements.each do |elem| |
|
elem.search('b').each do |b_e| |
|
if b_e.nil? or !['開催期間', 'イベント期間'].include?(b_e.inner_text) |
|
next |
|
end |
|
title = elem.at('h4').inner_text |
|
if md = title.match(/「(.*)」/) |
|
title = md[1] |
|
end |
|
term = b_e.next_sibling.inner_text.strip.split('〜') |
|
if term.size < 2 |
|
next |
|
end |
|
result.push({:type => type, :title => title, :start => extend_time_parse(term[0]), :end => term[1] ? extend_time_parse(term[1]) : nil}) |
|
end |
|
end |
|
result |
|
end |
|
|
|
def find_slstage_events(agent) |
|
result = [] |
|
type = 'スターライトステージ' |
|
page = agent.get(SLSTAGE_URL) |
|
elements = page.at('#js_async_main_column_text').search('.ui_anchor_container') |
|
elements.each do |elem| |
|
h4_elem = elem.at('h4') |
|
if !h4_elem.nil? |
|
ul_elem = elem.next_element.next_element.next_element |
|
if !ul_elem.nil? and !ul_elem.at('li').nil? |
|
ul_elem.search('li').each do |li| |
|
if li.nil? or !li.inner_text.include?('開催期間') |
|
next |
|
end |
|
title = h4_elem.inner_text |
|
term = li.inner_text.strip.split("\t")[2].split('~') |
|
if term.size < 2 |
|
next |
|
end |
|
result.push({:type => type, :title => title, :start => extend_time_parse(term[0]), :end => term[1] ? extend_time_parse(term[1]) : nil}) |
|
break |
|
end |
|
end |
|
end |
|
end |
|
result |
|
end |
|
|
|
def find_theater_events(agent) |
|
result = [] |
|
type = 'シアターデイズ' |
|
page = agent.get(THEATER_URL) |
|
elements = page.at('#js_async_main_column_text').search('.ui_anchor_container') |
|
elements.each do |elem| |
|
h4_elem = elem.at('h4') |
|
if !h4_elem.nil? |
|
ul_elem = elem.next_element.next_element.next_element |
|
if !ul_elem.nil? and !ul_elem.at('li').nil? |
|
ul_elem.search('li').each do |li| |
|
if li.nil? or !li.inner_text.include?('開催期間') |
|
next |
|
end |
|
title = h4_elem.inner_text |
|
term = li.inner_text.strip.split(':')[1].split('~') |
|
if term.size < 2 |
|
next |
|
end |
|
result.push({:type => type, :title => title, :start => extend_time_parse(term[0]), :end => term[1] ? extend_time_parse(term[1]) : nil}) |
|
end |
|
end |
|
end |
|
end |
|
result |
|
end |
|
|
|
def find_mstage_events(agent) |
|
result = [] |
|
type = 'LIVE ON ST@GE!' |
|
page = agent.get(MSTAGE_URL) |
|
elements = page.at('#body').search('h3') |
|
elements.each do |elem| |
|
title_elem = elem.at('a').attribute('title') |
|
ul_elem = elem.next.next.next.next |
|
if ul_elem and !ul_elem.at('li').nil? |
|
ul_elem.search('li').each do |li| |
|
if li.nil? or (!li.inner_text.include?('開催期間') and !li.inner_text.include?('開催日時')) |
|
next |
|
end |
|
title = title_elem.inner_text.sub('開催!', '') |
|
term = li.inner_text.split('~') |
|
if term.size < 2 |
|
next |
|
end |
|
result.push({:type => type, :title => title, :start => extend_time_parse(term[0]), :end => term[1] ? extend_time_parse(term[1]) : nil}) |
|
break |
|
end |
|
end |
|
end |
|
result |
|
end |
|
|
|
def extend_time_parse(str) |
|
tmp = str.gsub(/([0-9]+)年([0-9]+)月([0-9]+)日(.*)/, '\1/\2/\3\4') |
|
begin |
|
return Time.parse(tmp) |
|
rescue ArgumentError => e |
|
p e |
|
return nil |
|
end |
|
end |
|
|
|
def get_summary_message(list) |
|
now = Time.now |
|
send_message = '' |
|
list.each do |row| |
|
if row[:end] and now.yday == row[:end].yday |
|
send_message = send_message + row[:type] + '、' + row[:title] + ' ' + row[:end].strftime('%m月%d日 %H時%M分') + "まで\n" |
|
end |
|
end |
|
{:status => send_message, :spoiler_text => '本日終了予定のイベントは以下になります。'} |
|
end |
|
|
|
def get_announcement_message(list) |
|
# 2時間前から告知するよう判定 |
|
now = Time.now |
|
after = now + 60 * 60 * 2.5 |
|
before = now + 60 * 60 * 0.5 |
|
send_message = '' |
|
list.each do |row| |
|
if row[:end] and before < row[:end] and after > row[:end] |
|
send_message = send_message + row[:type] + '、' + row[:title] + 'の終了日時は' + row[:end].strftime('%m月%d日 %H時%M分') + "です。\n" |
|
end |
|
end |
|
{:status => send_message.empty? ? '' : "#{send_message}お忘れにならないようにお気を付けくださいね!", :spoiler_text => ''} |
|
end |
|
|
|
def get_miraicha_message(list) |
|
# ドリンクの飲み忘れ警告 |
|
events = [ |
|
'シンデレラロード', |
|
'シンデレラキャラバン', |
|
'LIVE Groove', |
|
'LIVE Parade', |
|
'LIVE PARTY', |
|
'ミリコレ', |
|
'THEATER SHOW TIME', |
|
'MILLION LIVE WORKING', |
|
'UNI-ON@IR', |
|
] |
|
now = Time.now |
|
send_message = {:status => '', :spoiler_text => ''} |
|
list.each do |row| |
|
if events.any? {|e| row[:title].include?(e) } |
|
if row[:start] < now and row[:end] and row[:end].year >= now.year and row[:end].yday >= now.yday |
|
p row |
|
return {:status => "(っ'ヮ'c)ウゥッヒョオアアァアアアァイェイL('ω')┘三└('ω')」イェイイェイL('ω')┘三└('ω')」イェイはよ当日ドリンク飲め言うたろイェイL('ω')┘三└('ω')」イェイ", :spoiler_text => '山崎はるかさんよりメッセージがあります'} |
|
end |
|
end |
|
end |
|
send_message |
|
end |
|
|
|
def get_message(list) |
|
now = Time.now |
|
if now.min >= 30 |
|
messages = get_miraicha_message(list) |
|
elsif now.hour == 0 or now.hour == 12 |
|
messages = get_summary_message(list) |
|
else |
|
messages = get_announcement_message(list) |
|
end |
|
messages |
|
end |
|
|
|
def send_message(agent, messages) |
|
if messages.empty? == false and messages[:status].empty? == false |
|
p messages |
|
body = {'status': messages[:status], 'visibility': 'public'} |
|
if messages[:spoiler_text].empty? == false |
|
body['spoiler_text'] = messages[:spoiler_text] |
|
end |
|
# toot |
|
agent.request_headers = {'Authorization' => 'Bearer ' + ENV['CHIHIRO_BEARER']} |
|
res = agent.post(ENV['MASTODON_HOST'] + 'api/v1/statuses', body) |
|
p res.code |
|
end |
|
end |
|
|
|
## main |
|
# mechanize |
|
agent = Mechanize.new |
|
agent.user_agent = UA |
|
agent.request_headers = {'accept-language' => 'ja, ja-JP', 'accept-encoding' => 'utf-8'} |
|
|
|
events = [] |
|
# imas_cg |
|
events.concat(find_imascg_events(agent)) |
|
# slstage |
|
events.concat(find_slstage_events(agent)) |
|
# theater_days |
|
events.concat(find_theater_events(agent)) |
|
# mstage |
|
events.concat(find_mstage_events(agent)) |
|
|
|
p events |
|
|
|
send_message(agent, get_message(events)) |