diff --git a/main.rb b/main.rb index 91d20dd..c2f49df 100644 --- a/main.rb +++ b/main.rb @@ -3,63 +3,10 @@ require 'video_info' require 'uri' require 'optparse' require 'shellwords' +require './mpc_shim' +require './util' -def sanity_check_video(url) - if !url.include?("youtube") - return false - end - video = VideoInfo.new(url) - title = video.title - title.slice! " - YouTube Music" - if video.duration > 1200 - puts "skipping #{url} since it's over 20 minutes" - return false - elsif File.exists?("tracks/#{title}.mp3") || File.exists?("spool/#{title}.mp3") - puts "#{url} already downloaded, not downloading again" - return false - end - return true -end - - - -def parse_songs_from_string(message) - urls = URI.extract(message) - if urls.length != 0 - puts "Found #{urls}; downloading" - urls.each do |url| - if sanity_check_video(url) - res = `youtube-dl --ignore-errors --output \"spool/%(title)s.%(ext)s\" --extract-audio --audio-format mp3 #{url}` - if res - puts "Downloaded #{url}" - else - puts "error downloading #{url}" - end - end - end - end - nil -end - -def update_mpd() - new_songs = Dir["spool/*.mp3"] - if new_songs.length <= 0 - return true - end - puts "telling mpd to refresh" - `mv spool/*.mp3 tracks/` - `mpc update` - new_songs.each do |song| - s = song.split("/")[1] - res = system("mpc add #{Shellwords.shellescape(s)}") - if res - puts "added #{s} to mpd" - else - puts "error adding #{s} to mpd" - end - end -end - +mpc = MpcShim.new options = {} OptionParser.new do |opt| opt.on("-t", "--token DISCORD_BOT_TOKEN") {|o| options[:bot_token] = o} @@ -73,7 +20,7 @@ end puts "creating spool and tracks directories" `mkdir -p spool` -`"mkdir -p tracks` +`mkdir -p tracks` if File.exists?("/usr/bin/mpd") if !File.exists?("/usr/bin/mpc") @@ -82,48 +29,45 @@ if File.exists?("/usr/bin/mpd") end puts "starting mpd if not already running" `systemctl start mpd` unless `ps aux | grep mpd` != "" - `mpc clear` - `mpc add /` - `mpc shuffle` - `mpc repeat` + mpc.reset() end bot = Discordrb::Commands::CommandBot.new token: options[:bot_token], prefix: '!radio ' bot.command :start do |event| - `mpc play` + mpc.play event.respond("starting radio") end bot.command :stop do |event| - `mpc stop` + mpc.stop event.respond("stoping radio") end bot.command :reload do |event| event.respond("reloading queue") - `mpc stop` - `mpc clear` - `mpc update` - `mpc add /` + mpc.stop + mpc.clear + mpc.update + mpc.add event.respond("queue reloaded") nil end bot.command :pause do |event| event.respond("pausing playback") - `mpc toggle` + mpc.toggle nil end bot.command :skip do |event| event.respond("skipping") - `mpc next` + mpc.next() nil end bot.command :shuffle do |event| - `mpc shuffle` + mpc.shuffle() event.respond("shuffled queue") nil end @@ -135,14 +79,14 @@ bot.command :initialize do |event| messages.each do |msg| parse_songs_from_string(msg.content) end - update_mpd() + update_mpd(mpc) event.respond("done loading") end bot.message(in: options[:channel_id],contains: "youtube.com") do |event| parse_songs_from_string(event.content) - update_mpd() + update_mpd(mpc) end bot.run diff --git a/mpc_shim.rb b/mpc_shim.rb new file mode 100644 index 0000000..92b47fc --- /dev/null +++ b/mpc_shim.rb @@ -0,0 +1,50 @@ +class MpcShim + def update() + `mpc update` + end + + def clear() + `mpc clear` + end + + def play() + `mpc play` + end + + def stop() + `mpc stop` + end + + def next() + `mpc next` + end + + def reset() + `mpc clear` + `mpc add /` + `mpc shuffle` + `mpc repeat` + end + + def update() + `mpc update` + end + + def toggle() + `mpc toggle` + end + + def add(filename = nil) + if filename == nil + `mpc add /` + else + `mpc add #{filename}` + end + return $?.success? + end + + def initialize() + true + end + +end diff --git a/util.rb b/util.rb new file mode 100644 index 0000000..9dd55cb --- /dev/null +++ b/util.rb @@ -0,0 +1,56 @@ +def sanity_check_video(url) + if !url.include?("youtube") + return false + end + video = VideoInfo.new(url) + title = video.title + title.slice! " - YouTube Music" + if video.duration > 1200 + puts "skipping #{url} since it's over 20 minutes" + return false + elsif File.exists?("tracks/#{title}.mp3") || File.exists?("spool/#{title}.mp3") + puts "#{url} already downloaded, not downloading again" + return false + end + return true +end + + + +def parse_songs_from_string(message) + urls = URI.extract(message) + if urls.length != 0 + puts "Found #{urls}; downloading" + urls.each do |url| + if sanity_check_video(url) + res = `youtube-dl --ignore-errors --output \"spool/%(title)s.%(ext)s\" --extract-audio --audio-format mp3 #{url}` + if res + puts "Downloaded #{url}" + else + puts "error downloading #{url}" + end + end + end + end + nil +end + +def update_mpd(mpc) + new_songs = Dir["spool/*.mp3"] + if new_songs.length <= 0 + return true + end + puts "telling mpd to refresh" + `mv spool/*.mp3 tracks/` + mpc.update + new_songs.each do |song| + s = song.split("/")[1] + res = mpc.add(Shellwords.shellescape(s)) + if res + puts "added #{s} to mpd" + else + puts "error adding #{s} to mpd" + end + end +end +