diff --git a/mumbledj/download_audio.py b/mumbledj/download_audio.py index 607edf2..7ea6d4d 100644 --- a/mumbledj/download_audio.py +++ b/mumbledj/download_audio.py @@ -30,9 +30,9 @@ except: while isfile('song.ogg.temp'): 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) remove('song.ogg') diff --git a/mumbledj/mumbledj.lua b/mumbledj/mumbledj.lua index 6787244..a79967d 100644 --- a/mumbledj/mumbledj.lua +++ b/mumbledj/mumbledj.lua @@ -6,6 +6,7 @@ local config = require("config") 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 = {} function piepan.onConnect() @@ -21,11 +22,11 @@ function piepan.onMessage(message) end if string.sub(message.text, 0, 1) == config.COMMAND_PREFIX then - parseCommand(message) + parse_command(message) end end -function parseCommand(message) +function parse_command(message) local command = "" local argument = "" if string.find(message.text, " ") then @@ -36,7 +37,7 @@ function parseCommand(message) end 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 config.OUTPUT then @@ -48,7 +49,7 @@ function parseCommand(message) if piepan.Audio.isPlaying() then message.user:send(config.MUSIC_PLAYING_MSG) else - piepan.me.channel:play("song-converted.ogg", nextSong) + piepan.me.channel:play("song-converted.ogg", SongQueue.get_next_song) end end @@ -56,7 +57,7 @@ function parseCommand(message) message.user:send(config.NO_PERMISSION_MSG) end 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 config.OUTPUT then @@ -73,13 +74,12 @@ function parseCommand(message) message.user:send(config.NO_PERMISSION_MSG) end 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 config.OUTPUT then 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 - print(debug.traceback()) + if not song_queue.add_song(argument, message.user.name) then message.user:send(config.INVALID_URL_MSG) end end @@ -87,7 +87,7 @@ function parseCommand(message) message.user:send(config.NO_PERMISSION_MSG) end 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 config.OUTPUT then @@ -99,7 +99,7 @@ function parseCommand(message) message.user:send(config.NO_PERMISSION_MSG) end 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 config.OUTPUT then @@ -116,7 +116,7 @@ function parseCommand(message) end end 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 config.OUTPUT then @@ -129,7 +129,7 @@ function parseCommand(message) message.user:send(config.NO_PERMISSION_MSG) end 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 config.OUTPUT then @@ -139,8 +139,6 @@ function parseCommand(message) else message.user:send(config.NO_PERMISSION_MSG) end - elseif command == "test" then - piepan.me.channel:play("song-converted.ogg", config.VOLUME, nextSong) else message.user:send("The command you have entered is not valid.") end @@ -169,7 +167,7 @@ function skip(username) local skip_ratio = skipper_count / user_count if skip_ratio > config.SKIP_RATIO then piepan.me.channel:send("The number of votes required for a skip has been met. Skipping song!") - nextSong() + next_song() else piepan.me.channel:send("" .. username .. " has voted to skip this song.") end @@ -192,18 +190,19 @@ end function kill() os.remove("song.ogg") os.remove("song-converted.ogg") + os.remove(".video_fail") os.exit(0) end -function checkPermissions(ADMIN_COMMAND, username) +function check_permissions(ADMIN_COMMAND, username) if config.ENABLE_ADMINS and ADMIN_COMMAND then - return isAdmin(username) + return is_admin(username) end return true end -function isAdmin(username) +function is_admin(username) for _,user in pairs(config.ADMINS) do if user == username then return true @@ -213,8 +212,14 @@ function isAdmin(username) return false end -function nextSong() +function next_song() 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 function file_exists(file) diff --git a/mumbledj/song_queue.lua b/mumbledj/song_queue.lua index 9c26f6b..f7e602a 100644 --- a/mumbledj/song_queue.lua +++ b/mumbledj/song_queue.lua @@ -13,7 +13,7 @@ local song_queue = deque.new() SongQueue = {} -function SongQueue.addSong(url, username) +function SongQueue.add_song(url, username) local patterns = { "https?://www%.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 local video_id = string.match(url, pattern) if video_id ~= nil and string.len(video_id) < 20 then - print("YouTube URL is valid!") - getYoutubeInfo(video_id, username) + return get_youtube_info(video_id, username) + else + return false end end end -function getYoutubeInfo(id, username) +function get_youtube_info(id, username) if id == nil then - return + return false end local cmd = [[ 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 thumbnail = jshon:read() if name == nil or duration == nil then - return + return false end - print("Finished getting info.") - youtubeInfoCompleted({ + return youtube_info_completed({ id = id, title = name, duration = string.format("%d:%02d", duration / 60, duration % 60), @@ -57,37 +57,54 @@ function getYoutubeInfo(id, username) }) end -function youtubeInfoCompleted(info) +function youtube_info_completed(info) if info == nil then return false end song_queue:push_right(info) - if song_queue:length() == 1 then - os.execute("python download_audio.py " .. info.id .. " " .. config.VOLUME) - while not file_exists("song-converted.ogg") do - os.execute("sleep " .. tonumber(2)) - end - piepan.me.channel:play("song-converted.ogg", nextSong) - end + local message = string.format(config.SONG_ADDED_HTML, info.username, info.title) + piepan.me.channel:send(message) - 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) - else - local message = string.format(config.SONG_ADDED_HTML, info.username, info.title) - piepan.me.channel:send(message) + if not piepan.Audio.isPlaying() then + return SongQueue.get_next_song() end return true 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 -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() end