#!/bin/sh

# This fetches a video url and cookie from youtube using youtube-dl,
# then plays the actual video by streaming with mplayer (this has the
# advantage over piping youtube-dl that mplayer can actually seek and
# stuff). Thanks to mod_eerf from #raspberrypi for pointing this method
# out.

# Apparently youtube-dl barfs if the cookie file pre-exists.
# Using mktemp anyway to randomize the name. This is _not secure_
# unless you use per-user tmp directories with proper permissions.
# Which I do, so meh.

for a in "$@" ; do
	COOKIEJAR="`mktemp -u --tmpdir youtube-cookie.XXXXXXXXXX`"
	mplayer -cookies -cookies-file "$COOKIEJAR" "$(youtube-dl -g --cookies="$COOKIEJAR" "$a")"
	rm "$COOKIEJAR"
done
