現在のマイリスの全体再生数をTwitterに投げる
定期実行
遅延処理(getthumbinfoをディレイなしにリクエストしまくってる)
前回との変化分を表示
鯖に上げて誰でも使えるように
マイリスのRSSを取る
page_source = open('http://www.nicovideo.jp/mylist/10180194?rss=2.0', 'r:UTF-8', &:read)
動画のIDの配列を取る
video_ids = page_source.scan(%r!(sm\d+)</link>!).flatten
ひとつの動画のgetthumbinfoにリクエストする
video_page_source = open('http://ext.nicovideo.jp/api/getthumbinfo/sm31097259', 'r:UTF-8', &:read)
再生数を取る
video_page_source.scan(%r!<(?:view_counter|comment_num|mylist_counter)>(\d+)</\w+>!).flatten[0]
require 'open-uri'
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "ENV['CONSUMER_KEY']"
config.consumer_secret = "ENV['CONSUMER_SECRET']"
config.access_token = "ENV['ACCESS_TOKEN']"
config.access_token_secret = "ENV['ACCESS_SECRET']"
end
class Integer
def formatted
self.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end
end
def message(play_count)
return "わたしの投稿動画の現在の合計再生数は #{play_count.formatted} です http://www.nicovideo.jp/mylist/10180194 #nicoplaycountsharetool https://gist.github.com/keisei1092/d642a991bd538e80beaabf4721aff51c"
end
video_ids = open('http://www.nicovideo.jp/mylist/10180194?rss=2.0', 'r:UTF-8', &:read)
.scan(%r!(sm\d+)</link>!)
.flatten
play_count = video_ids.inject(0) { |sum, video_id|
thumb_info = open("http://ext.nicovideo.jp/api/getthumbinfo/#{video_id}", 'r:UTF-8', &:read)
sum = sum + thumb_info
.scan(%r!<(?:view_counter|comment_num|mylist_counter)>(\d+)</\w+>!)
.flatten[0]
.to_i
}
message = message(play_count)
client.update(message)
イコールの後に適切な値をいれてください
CONSUMER_KEY= CONSUMER_SECRET= OAUTH_TOKEN= OAUTH_SECRET= ruby 20170507_nico_playcount_share_tool.rb