stevetunes/util.rb
2021-12-22 14:40:08 -05:00

57 lines
1.3 KiB
Ruby

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