Last active
May 19, 2018 17:08
-
-
Save Mossuru777/e62a96b67c8e7632ccbcdfd3b16b9835 to your computer and use it in GitHub Desktop.
Chinachu γ で録画終了後に、JKCommentGetterを利用してニコニコ実況のコメントを取得する (recordedCommandに指定することを想定)
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
#!/usr/bin/env ruby | |
# JKCommentGetter (https://github.com/ACUVE/JKCommentGetter) の | |
# JKComment.rbをこのスクリプトと同一のディレクトリに置いて下さい | |
### Settings ### | |
EMAIL = nil # EMAIL = '[email protected]' | |
PASSWORD = nil # PASSWORD = 'hogefuga' | |
### Settings End ### | |
require 'json' | |
require 'net/http' | |
require 'time' | |
require 'uri' | |
require_relative 'JKComment' | |
include JKComment | |
# ref http://soranikakaruhashi.blog.fc2.com/blog-entry-71.html | |
SID2JK = { | |
1024 => 'jk1', # NHK総合・東京 | |
1032 => 'jk2', # NHKEテレ東京 | |
1040 => 'jk4', # 日本テレビ | |
1048 => 'jk6', # TBS | |
1056 => 'jk8', # フジテレビジョン | |
1064 => 'jk5', # テレビ朝日 | |
1072 => 'jk7', # テレビ東京 | |
23608 => 'jk9', # TOKYO MX | |
24632 => 'jk11', # tvk | |
27704 => 'jk12', # チバテレビ | |
29752 => 'jk10', # テレ玉 | |
101 => 'jk101', # NHKBS1 | |
103 => 'jk103', # NHKBSプレミアム | |
141 => 'jk141', # BS日テレ | |
151 => 'jk151', # BS朝日 | |
161 => 'jk161', # BS-TBS | |
171 => 'jk171', # BSジャパン | |
181 => 'jk181', # BSフジ | |
211 => 'jk211', # BS11イレブン | |
222 => 'jk222', # BS12トゥエルビ | |
258 => 'jk258', # ディーライフ | |
} | |
# ref https://github.com/Chinachu/Chinachu/blob/a69d5e99b75ddd770146e65d6171704be28ec01a/app-operator.js#L63 | |
BEFORE_MARGIN_SEC = 15 | |
def main | |
# Parse Arguments | |
rec_filepath, info = ARGV | |
info = JSON.parse(info) | |
# Decide NicoJK Channel | |
nicojk_ch = nil | |
begin | |
sid = info['channel']['sid'] | |
nicojk_ch = SID2JK.fetch(sid) | |
rescue KeyError | |
logging 'NicoJK channel not found: ', info['channel']['name'], ?\n | |
exit 0 | |
end | |
# Fetch Cookie | |
cookie = getNicoJKCookie(EMAIL, PASSWORD).join('=') << ';' | |
# Fetch Start/End Time | |
program_start_time = Time.at(info['start'] / 1000) | |
program_end_time = Time.at(info['end'] / 1000) | |
# Decide Comment Filepath | |
rec_dirname = File.dirname(rec_filepath) | |
rec_basename = File.basename(rec_filepath, '.m2ts') | |
comment_filepath = rec_dirname + '/' + rec_basename + '.xml' | |
# Output Comments | |
outputCommentXML(nicojk_ch, cookie, program_start_time, program_end_time, rec_filepath, comment_filepath) | |
end | |
def getNicoJKCookie(email, password) | |
uri = URI.parse('https://account.nicovideo.jp/api/v1/login?site=nicojikkyo_web&mail_or_tel=1') | |
request = Net::HTTP::Post.new(uri) | |
request.set_form_data( | |
'mail_tel' => email, | |
'password' => password, | |
) | |
req_options = { | |
use_ssl: uri.scheme == 'https', | |
} | |
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| | |
http.request(request) | |
end | |
cookie = response.get_fields('Set-Cookie').find do |c| | |
c.start_with?('user_session=user_session') | |
end | |
if cookie.nil? then | |
raise 'Cookie (user_session) not found' | |
end | |
cookie.split(/; */).each do |c| | |
kv = c.split('=') | |
if kv[0] == 'user_session' then | |
return kv | |
end | |
end | |
end | |
def outputCommentXML(nicojk_ch, cookie, program_start_time, program_end_time, rec_filepath, comment_filepath) | |
argv_start_time = program_start_time | |
argv_end_time = program_end_time | |
start_time = program_start_time - BEFORE_MARGIN_SEC | |
end_time = program_end_time | |
retrynum = 3 | |
check_range = 60 | |
logging nicojk_ch, ' を ', start_time, ' から ', end_time, 'まで取得します', ?\n | |
# コメント取得処理 | |
cm = CommentGetter.new(cookie, retrynum, logging: method(:logging)) | |
chat = cm.getChatElementsRange(nicojk_ch, start_time, end_time, false) | |
# 一つもコメントを得られなかった場合、ファイル出力せず抜ける | |
if chat.empty? | |
logging 'コメントが一つも得られませんでした。出力せずに終了します。', ?\n | |
return | |
end | |
# XMLファイル出力処理 | |
logging comment_filepath, ' へ出力します', ?\n | |
File.open(comment_filepath, 'w') do |comment_filehandle| | |
arg = { | |
jknum: nicojk_ch, | |
start_time: start_time, | |
end_time: end_time, | |
argv_start_time: argv_start_time, | |
argv_end_time: argv_end_time, | |
time_header: false, | |
comment: nil | |
} | |
printChatArrayXML(comment_filehandle, chat, arg) | |
end | |
# XMLファイルの最終アクセス日時と更新日時を | |
# 録画ファイルの更新日時に合わせることを試みる | |
begin | |
rec_mtime = File.stat(rec_filepath).mtime | |
File.utime(rec_mtime, rec_mtime, comment_filepath) | |
rescue Exception | |
end | |
end | |
def logging(*str) | |
$stderr.print *str | |
end | |
if __FILE__ == $0 | |
if EMAIL.nil? || EMAIL.empty? || PASSWORD.nil? || PASSWORD.empty? then | |
logging 'メールアドレスとパスワードをセットして下さい', ?\n | |
else | |
main | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment