Proceeding from client to server, the first component is a Firefox bookmarklet for sending the URL of the current page to the MythTV box:
javascript:(function(){window.open('http://mythtvbox.mydomain.com:12345/'+window.location);})()On the MythTV box I have a Python script listening for incoming HTTP requests on port 12345. The script uses twisted.web, so
apt-get python-twisted-web (on Debian-based distros) is needed.#!/usr/bin/python
from twisted.web import server, resource
from twisted.internet import reactor
class DownloadRequestHandler(resource.Resource):
isLeaf = True
def render_GET(self, request):
try:
video_id = request.args['v'][0]
except:
msg = "Can't parse URL %r" % request.path
return msg
file('/home/akaihola/.download-queue/youtube.com/%s' % video_id, 'w').close()
return 'Downloading YouTube %s' % video_id
site = server.Site(DownloadRequestHandler())
reactor.listenTCP(12345, site)
reactor.run()
At this point, whenever I select the "Download YouTube" bookmarklet while viewing a YouTube video, a file named after the ID of the video magically appears on the MythTV box.
For actual downloading of the content I use the youtube-dl script. The following bash script downloads content as long as files corresponding to video IDs exist, deletes those files after downloading, and waits for new files to appear. Waiting for new files is implemented efficiently using the inotifywait tool (
apt-get inotify-tools on Debian-based distros). Only a small slice of my bandwidth is allowed for downloads during daytime, and in the night it can freely saturate the connection.#!/bin/bash
QUEUEDIR=/home/akaihola/.download-queue/youtube.com
TEMPLATE="/var/lib/mythtv/videos/%(stitle)s-%(id)s.%(ext)s"
cd $QUEUEDIR
while true; do
while [ "`ls`" ]; do
VIDEO=`ls -1|head -1`
RATE=$( [ `date +%H` -gt 6 ] && echo "-r 30k" )
~/bin/youtube-dl -b $RATE -o $TEMPLATE http://youtube.com/watch?v=$VIDEO
rm $VIDEO
done
inotifywait -e create $QUEUEDIR
done
Both scripts are running as services in the background on the MythTV server ready to queue and download videos. The videos appear in the "Watch Videos" menu of the MythTV box ready for enjoyment.
Voilà!* It works! Improvements appreciated.
*or "Viola!" as I've seen many native English speakers happily saluting their favorite string instrument after achieving a desired goal
Update: Insufficient bash experience. Wouldn't work for >1 files in the queue. Fixed now, but
[ "`ls`" ] feels like a bit awkward way to test if a directory is not empty.