More progress towards fully working queue. Second song doesn't play for some reason.

This commit is contained in:
Matthieu Grieger 2014-09-18 10:10:23 -07:00
parent b3b9e2e1dc
commit 6b6d1a7a7e
3 changed files with 68 additions and 46 deletions

View file

@ -30,9 +30,9 @@ except:
while isfile('song.ogg.temp'): while isfile('song.ogg.temp'):
sleep(1) sleep(1)
system('ffmpeg -i song.ogg -ar 48000 -ac 1 -loglevel quiet -vol ' + str(volume) + ' song-converted.ogg -y') system('ffmpeg -i song.ogg -ar 48000 -ac 1 -vol ' + str(volume) + ' song-converted.ogg -y')
while not isfile("song-converted.ogg"): while not isfile('song-converted.ogg') and not isfile(".video_fail"):
sleep(1) sleep(1)
remove('song.ogg') remove('song.ogg')

View file

@ -6,6 +6,7 @@
local config = require("config") local config = require("config")
local song_queue = require("song_queue") local song_queue = require("song_queue")
-- NOTE: Skippers will need to be fixed since all the song-related stuff is being moved to song_queue.lua
local skippers = {} local skippers = {}
function piepan.onConnect() function piepan.onConnect()
@ -21,11 +22,11 @@ function piepan.onMessage(message)
end end
if string.sub(message.text, 0, 1) == config.COMMAND_PREFIX then if string.sub(message.text, 0, 1) == config.COMMAND_PREFIX then
parseCommand(message) parse_command(message)
end end
end end
function parseCommand(message) function parse_command(message)
local command = "" local command = ""
local argument = "" local argument = ""
if string.find(message.text, " ") then if string.find(message.text, " ") then
@ -36,7 +37,7 @@ function parseCommand(message)
end end
if command == "play" then if command == "play" then
local has_permission = checkPermissions(config.ADMIN_PLAY, message.user.name) local has_permission = check_permissions(config.ADMIN_PLAY, message.user.name)
if has_permission then if has_permission then
if config.OUTPUT then if config.OUTPUT then
@ -48,7 +49,7 @@ function parseCommand(message)
if piepan.Audio.isPlaying() then if piepan.Audio.isPlaying() then
message.user:send(config.MUSIC_PLAYING_MSG) message.user:send(config.MUSIC_PLAYING_MSG)
else else
piepan.me.channel:play("song-converted.ogg", nextSong) piepan.me.channel:play("song-converted.ogg", SongQueue.get_next_song)
end end
end end
@ -56,7 +57,7 @@ function parseCommand(message)
message.user:send(config.NO_PERMISSION_MSG) message.user:send(config.NO_PERMISSION_MSG)
end end
elseif command == "pause" then elseif command == "pause" then
local has_permission = checkPermissions(config.ADMIN_PAUSE, message.user.name) local has_permission = check_permissions(config.ADMIN_PAUSE, message.user.name)
if has_permission then if has_permission then
if config.OUTPUT then if config.OUTPUT then
@ -73,13 +74,12 @@ function parseCommand(message)
message.user:send(config.NO_PERMISSION_MSG) message.user:send(config.NO_PERMISSION_MSG)
end end
elseif command == "add" then elseif command == "add" then
local has_permission = checkPermissions(config.ADMIN_ADD, message.user.name) local has_permission = check_permissions(config.ADMIN_ADD, message.user.name)
if has_permission then if has_permission then
if config.OUTPUT then if config.OUTPUT then
print(message.user.name .. " has told the bot to add the following URL to the queue: " .. argument .. ".") print(message.user.name .. " has told the bot to add the following URL to the queue: " .. argument .. ".")
if not song_queue.addSong(argument, message.user.name) then if not song_queue.add_song(argument, message.user.name) then
print(debug.traceback())
message.user:send(config.INVALID_URL_MSG) message.user:send(config.INVALID_URL_MSG)
end end
end end
@ -87,7 +87,7 @@ function parseCommand(message)
message.user:send(config.NO_PERMISSION_MSG) message.user:send(config.NO_PERMISSION_MSG)
end end
elseif command == "skip" then elseif command == "skip" then
local has_permission = checkPermissions(config.ADMIN_SKIP, message.user.name) local has_permission = check_permissions(config.ADMIN_SKIP, message.user.name)
if has_permission then if has_permission then
if config.OUTPUT then if config.OUTPUT then
@ -99,7 +99,7 @@ function parseCommand(message)
message.user:send(config.NO_PERMISSION_MSG) message.user:send(config.NO_PERMISSION_MSG)
end end
elseif command == "volume" then elseif command == "volume" then
local has_permission = checkPermissions(config.ADMIN_VOLUME, message.user.name) local has_permission = check_permissions(config.ADMIN_VOLUME, message.user.name)
if has_permission then if has_permission then
if config.OUTPUT then if config.OUTPUT then
@ -116,7 +116,7 @@ function parseCommand(message)
end end
end end
elseif command == "move" then elseif command == "move" then
local has_permission = checkPermissions(config.ADMIN_MOVE, message.user.name) local has_permission = check_permissions(config.ADMIN_MOVE, message.user.name)
if has_permission then if has_permission then
if config.OUTPUT then if config.OUTPUT then
@ -129,7 +129,7 @@ function parseCommand(message)
message.user:send(config.NO_PERMISSION_MSG) message.user:send(config.NO_PERMISSION_MSG)
end end
elseif command == "kill" then elseif command == "kill" then
local has_permission = checkPermissions(config.ADMIN_KILL, message.user.name) local has_permission = check_permissions(config.ADMIN_KILL, message.user.name)
if has_permission then if has_permission then
if config.OUTPUT then if config.OUTPUT then
@ -139,8 +139,6 @@ function parseCommand(message)
else else
message.user:send(config.NO_PERMISSION_MSG) message.user:send(config.NO_PERMISSION_MSG)
end end
elseif command == "test" then
piepan.me.channel:play("song-converted.ogg", config.VOLUME, nextSong)
else else
message.user:send("The command you have entered is not valid.") message.user:send("The command you have entered is not valid.")
end end
@ -169,7 +167,7 @@ function skip(username)
local skip_ratio = skipper_count / user_count local skip_ratio = skipper_count / user_count
if skip_ratio > config.SKIP_RATIO then if skip_ratio > config.SKIP_RATIO then
piepan.me.channel:send("The number of votes required for a skip has been met. Skipping song!") piepan.me.channel:send("The number of votes required for a skip has been met. Skipping song!")
nextSong() next_song()
else else
piepan.me.channel:send("<b>" .. username .. "</b> has voted to skip this song.") piepan.me.channel:send("<b>" .. username .. "</b> has voted to skip this song.")
end end
@ -192,18 +190,19 @@ end
function kill() function kill()
os.remove("song.ogg") os.remove("song.ogg")
os.remove("song-converted.ogg") os.remove("song-converted.ogg")
os.remove(".video_fail")
os.exit(0) os.exit(0)
end end
function checkPermissions(ADMIN_COMMAND, username) function check_permissions(ADMIN_COMMAND, username)
if config.ENABLE_ADMINS and ADMIN_COMMAND then if config.ENABLE_ADMINS and ADMIN_COMMAND then
return isAdmin(username) return is_admin(username)
end end
return true return true
end end
function isAdmin(username) function is_admin(username)
for _,user in pairs(config.ADMINS) do for _,user in pairs(config.ADMINS) do
if user == username then if user == username then
return true return true
@ -213,8 +212,14 @@ function isAdmin(username)
return false return false
end end
function nextSong() function next_song()
skippers = {} skippers = {}
if song_queue:get_length() ~= 0 then
local success = song_queue:get_next_song()
if not success then
piepan.me.channel:send("An error occurred while preparing the next track. Skipping...")
end
end
end end
function file_exists(file) function file_exists(file)

View file

@ -13,7 +13,7 @@ local song_queue = deque.new()
SongQueue = {} SongQueue = {}
function SongQueue.addSong(url, username) function SongQueue.add_song(url, username)
local patterns = { local patterns = {
"https?://www%.youtube%.com/watch%?v=([%d%a_%-]+)", "https?://www%.youtube%.com/watch%?v=([%d%a_%-]+)",
"https?://youtube%.com/watch%?v=([%d%a_%-]+)", "https?://youtube%.com/watch%?v=([%d%a_%-]+)",
@ -25,15 +25,16 @@ function SongQueue.addSong(url, username)
for _,pattern in ipairs(patterns) do for _,pattern in ipairs(patterns) do
local video_id = string.match(url, pattern) local video_id = string.match(url, pattern)
if video_id ~= nil and string.len(video_id) < 20 then if video_id ~= nil and string.len(video_id) < 20 then
print("YouTube URL is valid!") return get_youtube_info(video_id, username)
getYoutubeInfo(video_id, username) else
return false
end end
end end
end end
function getYoutubeInfo(id, username) function get_youtube_info(id, username)
if id == nil then if id == nil then
return return false
end end
local cmd = [[ local cmd = [[
wget -q -O - 'http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc' | wget -q -O - 'http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc' |
@ -44,11 +45,10 @@ function getYoutubeInfo(id, username)
local duration = jshon:read() local duration = jshon:read()
local thumbnail = jshon:read() local thumbnail = jshon:read()
if name == nil or duration == nil then if name == nil or duration == nil then
return return false
end end
print("Finished getting info.") return youtube_info_completed({
youtubeInfoCompleted({
id = id, id = id,
title = name, title = name,
duration = string.format("%d:%02d", duration / 60, duration % 60), duration = string.format("%d:%02d", duration / 60, duration % 60),
@ -57,37 +57,54 @@ function getYoutubeInfo(id, username)
}) })
end end
function youtubeInfoCompleted(info) function youtube_info_completed(info)
if info == nil then if info == nil then
return false return false
end end
song_queue:push_right(info) song_queue:push_right(info)
if song_queue:length() == 1 then local message = string.format(config.SONG_ADDED_HTML, info.username, info.title)
os.execute("python download_audio.py " .. info.id .. " " .. config.VOLUME) piepan.me.channel:send(message)
while not file_exists("song-converted.ogg") do
os.execute("sleep " .. tonumber(2))
end
piepan.me.channel:play("song-converted.ogg", nextSong)
end
if piepan.Audio:isPlaying() then if not piepan.Audio.isPlaying() then
local message = string.format(config.NOW_PLAYING_HTML, info.thumbnail, info.id, info.title, info.duration, info.username) return SongQueue.get_next_song()
piepan.me.channel:send(message)
else
local message = string.format(config.SONG_ADDED_HTML, info.username, info.title)
piepan.me.channel:send(message)
end end
return true return true
end end
function SongQueue.getNextSong(url) function SongQueue.get_next_song()
if file_exists("song-converted.ogg") then
os.remove("song-converted.ogg")
end
if song_queue:length() ~= 0 then
local next_song = song_queue:pop_left()
return start_song(next_song)
end
end end
function SongQueue.getLength() function start_song(info)
os.execute("python download_audio.py " .. info.id .. " " .. config.VOLUME)
while not file_exists("song-converted.ogg") do
os.execute("sleep " .. tonumber(2))
end
if not file_exists(".video_fail") then
piepan.me.channel:play("song-converted.ogg", SongQueue.get_next_song)
else
return false
end
if piepan.Audio:isPlaying() then
local message = string.format(config.NOW_PLAYING_HTML, info.thumbnail, info.id, info.title, info.duration, info.username)
piepan.me.channel:send(message)
return true
end
return false
end
function SongQueue.get_length()
return song_queue:length() return song_queue:length()
end end