Skip to content

Instantly share code, notes, and snippets.

@entropie
Last active May 13, 2024 15:34
Show Gist options
  • Save entropie/d265e94136b9777cc6b3190189b30050 to your computer and use it in GitHub Desktop.
Save entropie/d265e94136b9777cc6b3190189b30050 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# this scripts acts as a wrapper for a little daemon that reads a fifo;
#
# If called without allready running it starts a background daemon and
# downloads the argument, then proceeds to watch the fifo for more.
#
# We esnure that the script is only running once and only one dl at a time.
FIFO = "/home/ha/.ytfifo"
# SCRIPT_AUDIO = "yt-dlp -P /home/media/audiobooks/Incoming --extract-audio --audio-format mp3 --audio-quality 0"
# SCRIPT_VIDEO = "yt-dlp -P /home/media/movie/Incoming"
SCRIPT_AUDIO = "/home/mit/bin/ytaudiodl"
SCRIPT_VIDEO = "/home/mit/bin/ytdl"
LOGFILE = "/home/ha/log/fifo.log"
unless File.exist?(FIFO)
File.mkfifo(FIFO)
end
Process.setproctitle("ytfifo")
first_file = nil
scmd = ARGV.join
if `pgrep -f ytfifo`.split("\n").size > 1
puts "daemon seems to be awake"
if ARGV.size > 0
puts "sending argument '#{scmd}'"
File.open(FIFO, "w+"){ |fp| fp.puts(scmd) }
end
exit
else
$stderr.puts "not running; casting daemon"
Process.daemon
first_file = scmd
end
def dl(what)
what.strip!
puts "DL: #{what}"
if not what or not what =~ /youtu\.?be/ or not what =~ /https/
puts "skip: #{what}"
return
end
# prepend V for video download - default
what = "V---#{what}" if what.scan(/---/).size == 0
script =
if what.split("---").first == "V"
SCRIPT_VIDEO
else
SCRIPT_AUDIO
end
what = what.split("---").last
cmd = "echo -e \"ytfifo.rb DL($(date)) #{what}\" >> #{LOGFILE} && #{script} #{what} >> #{LOGFILE} 2>&1"
puts "running: #{cmd}"
`#{cmd}`
end
fifo = File.open(FIFO, 'r+')
$stdout.sync = true
loop do
sleep 1
line = first_file || fifo.gets
first_file = nil
next if not line or line.empty?
puts dl(line)
end
fifo.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment