Last active
August 29, 2015 14:10
-
-
Save Bakudankun/68f5d63d6724e1fe72c5 to your computer and use it in GitHub Desktop.
「大合奏!バンドブラザーズP」の日刊バンブラ通信をTwitterにつぶやくbot
This file contains 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
#encoding: utf-8 | |
require 'twitter' | |
require 'nokogiri' | |
require 'open-uri' | |
def tweet(client, str) # 文字列を140字以下に分割しつつ呟く | |
if str.size <= 0 then | |
return | |
elsif str.size <= 140 then | |
client.update(str.strip) | |
else | |
bufs = str[0 .. (str.size/2 - 1)].rpartition(/[。!?」』[:space:]]/) + str[(str.size/2) .. str.size].partition(/[。!?」』[:space:]]/) | |
if ( bufs[2].size > (bufs[3] + bufs[4]).size ) then | |
tweet(client, bufs[0] + bufs[1] + bufs[2] + bufs[3] + bufs[4]) | |
tweet(client, bufs[5]) | |
else | |
tweet(client, bufs[0] + bufs[1]) | |
tweet(client, bufs[2] + bufs[3] + bufs[4] + bufs[5]) | |
end | |
end | |
end | |
def tweetRecommends(client, newsPage, newsURL) # 本日おすすめの曲をツイートする | |
recommends = "本日おすすめの曲:\n" | |
newsPage.css("div.boxRecommend > div > table").each do |song| | |
title = song.css("a").inner_html | |
href = newsURL + song.css("a").first["href"].partition(";").first | |
author = song.css("td")[1].inner_html | |
oneSong = title + " (" + author + ") " + href + "\n" | |
if (recommends + oneSong).gsub(/ http[^\n]*\n/, " abcdefghijklmnopqrstuv\n").strip.size > 140 then | |
client.update(recommends.strip) | |
recommends = "本日おすすめの曲:\n" | |
end | |
recommends = recommends + oneSong | |
end | |
if recommends.size > 10 then | |
client.update(recommends.strip) | |
end | |
end | |
newsURL = "http://bandbros-p.nintendo.co.jp" | |
newsPage = Nokogiri::HTML(open(newsURL)) | |
latestNews = newsPage.css("p.txtComTop").first | |
latestInfo = newsPage.css("dd.boxEventInfo").first | |
client = Twitter::REST::Client.new do |config| | |
config.consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx' | |
config.consumer_secret = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' | |
config.access_token = '0000000000-zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' | |
config.access_token_secret = 'wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww' | |
end | |
# バンブラ通信 | |
if latestNews != nil then | |
if File.exist?("lastNews.txt") then | |
lastNews = File.read("lastNews.txt", :encoding => Encoding::UTF_8) | |
if lastNews != latestNews.inner_html.strip then | |
tweet(client, latestNews.inner_html.strip) | |
tweetRecommends(client, newsPage, newsURL) | |
File.write("lastNews.txt", latestNews.inner_html.strip) | |
end | |
else | |
tweet(client, latestNews.inner_html.strip) | |
tweetRecommends(client, newsPage, newsURL) | |
File.write("lastNews.txt", latestNews.inner_html.strip) | |
end | |
end | |
# お知らせ | |
if latestInfo != nil then | |
if File.exist?("lastInfo.txt") then | |
lastInfo = File.read("lastInfo.txt", :encoding => Encoding::UTF_8) | |
if lastInfo != latestInfo.inner_html.strip then | |
tweet(client, "【お知らせ】" + latestInfo.inner_html.strip) | |
File.write("lastInfo.txt", latestInfo.inner_html.strip) | |
end | |
else | |
tweet(client, "【お知らせ】" + latestInfo.inner_html.strip) | |
File.write("lastInfo.txt", latestInfo.inner_html.strip) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
予め専用クライアントを dev.twitter.com に登録し、取得したConsumer Key等々を中盤あたりに記入。
TwitterとNokogiriのgemが必要なのでインストールしておく。
起動するとすぐに投稿してそのまま終了するので、cronなどを利用して1日1回起動させるようにする。
作業ディレクトリに保存される
lastInfo.txt
とlastNews.txt
は2度同じツイートをしないための物なので消さないように。