Added "show schedule", "show detail", "playback" function of "Internet Radio Station <Onsen>"

This commit is contained in:
takumi091111
2017-10-14 22:37:51 +09:00
parent 4969084731
commit 0f1a48c893
3 changed files with 157 additions and 0 deletions

40
lib/commands/music.rb Normal file
View File

@ -0,0 +1,40 @@
module Bot
module DiscordCommands
module MusicCommands
extend Discordrb::Commands::CommandContainer
command(:connect) do |event|
if channel = event.user.voice_channel
BOT.voice_connect(channel)
nil
else
event.respond 'ボイスチャンネルに参加してください'
end
end
command(:disconnect) do |event|
unless event.bot.voices.empty?
event.voice.destroy
nil
else
event.respond 'ボイスチャンネルに接続されていません'
end
end
command(:pause) do |event|
event.voice.pause
event.respond '一時停止'
end
command(:stop) do |event|
event.voice.stop_playing
event.respond '再生停止'
end
command(:skip) do |event, sec|
event.voice.skip(sec.to_f)
nil
end
end
end
end

106
lib/commands/webradio.rb Normal file
View File

@ -0,0 +1,106 @@
module Bot
module DiscordCommands
module RadioCommands
extend Discordrb::Commands::CommandContainer
require 'nokogiri'
require 'open-uri'
ONSEN_TOP = 'http://www.onsen.ag'
ONSEN_SCHEDULE_API = 'http://www.onsen.ag/api/shownMovie/shownMovie.json'
ONSEN_INFO_API = 'http://www.onsen.ag/data/api/getMovieInfo/'
command(:onsenList, usage: 'onsenList <曜日>', description: '音泉の番組リストを表示') do |event, wday|
# 曜日を判定
if wday
wday = match_to_wday(wday)
else
# wdayが空の場合はtodayを設定
wday = match_to_wday('today')
end
# 判定後に空の場合は抜ける
next unless wday
# 指定した曜日のリストを取得
doc = Nokogiri::HTML(open(ONSEN_TOP))
list = doc.css('.listWrap .clr li')
result = list.select {|item| item.get_attribute('data-week') == wday}
event.channel.send_embed do |embed|
embed.color = CONFIG[:onsen][:color]
embed.timestamp = Time.now
embed.author = Discordrb::Webhooks::EmbedAuthor.new(
name: "音泉 番組表(#{wday.to_jpn_wday})",
url: "http://www.onsen.ag",
icon_url: "https://lh3.googleusercontent.com/Cr5L2jLfye8-FSFPzXEPA_2EQtGEed-4IwEOY-_4DMePG3zUwQD9O0ZwBpYYBrnx6R0l=w50"
)
result.each do |item|
id = item.get_attribute('id')
title = item.css('h4').text.gsub(" ", "\s")
update = item.get_attribute('data-update')
navigator = item.css('.navigator').text
embed.add_field(
name: "[#{id}] #{title}",
value: "更新日: #{update}\nパーソナリティ: #{navigator}"
)
end
end
end
command(:onsenInfo, usage: 'onsenInfo <番組ID>', description: '音泉の番組情報を表示', min_args: 1) do |event, id|
uri = URI.parse(ONSEN_INFO_API + id)
json = JSON.load(Net::HTTP.get(uri).to_json)
title = json["title"].gsub(" ", "\s")
id = json["url"]
personality = json["personality"].split('/').map(&:strip!)
thumbnail = json["thumbnailPath"]
schedule = json["schedule"]
update = json["update"]
count = json["count"].to_i
event.channel.send_embed do |embed|
embed.color = CONFIG[:onsen][:color]
embed.image = Discordrb::Webhooks::EmbedImage.new(
url: "#{ONSEN_TOP}#{thumbnail}"
)
embed.author = Discordrb::Webhooks::EmbedAuthor.new(
name: "音泉 番組詳細",
url: "http://www.onsen.ag/program/#{id}",
icon_url: "https://lh3.googleusercontent.com/Cr5L2jLfye8-FSFPzXEPA_2EQtGEed-4IwEOY-_4DMePG3zUwQD9O0ZwBpYYBrnx6R0l=w50"
)
embed.add_field(name: "番組名", value: title)
embed.add_field(name: "配信日時", value: schedule, inline: true)
embed.add_field(name: "最新配信日", value: update, inline: true)
embed.add_field(name: "配信回数", value: "#{count}", inline: true)
embed.add_field(name: "パーソナリティ", value: personality.join("\n"))
end
end
command(:onsenPlay, usage: 'onsenPlay <番組ID>', description: '音泉の番組を再生', min_args: 1) do |event, id|
uri = URI.parse(ONSEN_INFO_API + id)
json = JSON.load(Net::HTTP.get(uri).to_json)
moviePath = json["moviePath"]["pc"]
title = json["title"].gsub(" ", "\s")
BOT.voice_connect(event.user.voice_channel)
event.respond "ダウンロード中..."
File.open("./radio/#{title}.mp3", 'wb') do |saved_file|
open(moviePath, 'rb') do |read_file|
saved_file.write(read_file.read)
end
end
event.respond "#{title}を再生開始"
event.voice.play_file("./radio/#{title}.mp3")
event.respond "再生終了"
event.voice.destroy
nil
end
end
end
end

View File

@ -1,6 +1,7 @@
require 'net/http'
require 'uri'
require 'json'
require 'time'
def get_json(location, limit = 10)
raise ArgumentError, 'too many HTTP redirects' if limit == 0
@ -37,4 +38,14 @@ def post_json(location, json)
req["Content-Type"] = "application/json"
req.body = json
return https.request(req)
end
# 正規表現でマッチした曜日を3文字の英語に変換
def match_to_wday(str)
str.downcase!
if str =~ /^今日|^today/
return Time.now.strftime("%a").downcase
end
i = %w(^日|^sun ^月|^mon ^火|^tue ^水|^wed ^木|^thu ^金|^fri ^土|^sat).index{|r| str =~ /#{r}/}
return %w(sun mon tue wed thu fri sat)[i] if i
end