#!/bin/sh ############################################################################### ## ## ## File: getyoutube ## ## Author: Wolfgang S. Rupprecht ## ## Created: Wed Jul 4 14:44:59 PDT 2007 ## ## Contents: quick shell script to grab and play youtube videos ## ## ## ## Copyright (c) 2007 Wolfgang S. Rupprecht. ## ## Released under GPL version 3 ## ## ## ## $Id: getyoutube,v 1.24 2007/11/17 07:37:47 wolfgang Exp $ ############################################################################### # usage: # getyoutube http://www.youtube.com/watch?v=DCQ7dmGuAU8 [...] # getyoutube http://www.youtube.com/view_play_list?p=472DE700A3CC70A4 [...] # getyoutube http://www.youtube.com/results?search_query=openmoko [...] # getyoutube http://www.youtube.com/profile_videos?user=openmoko [...] # # The "&" in the youtube URLS and anything past them is optional. # Freel free to not cut and paste it and save yourself the trouble of # having to quote the lines (since the &'s have meaning to the shell.) # # We can deal with a few types of youtube html files. # /watch?v= (individual video page) # /view_play_list?p= (playlist of the above files) # /results?search_query= (search results) # /profile_videos?user= (a user's video list) # # Requires wget(1) and mplayer(1). Fedora users may need to install # the optional mplayer codecs from the livna repository. set -eu TMP=/tmp LOCK=/tmp/mplayer-lock GETVIDEO="http://youtube.com/get_video.php?" WATCHURL="http://youtube.com/watch?v=" testflag='' mkdir -p $TMP/YouTube cd $TMP/YouTube playone () { vcache="$(echo $1 | sed -n -e 's:.*/watch?v=\([^\&]*\).*:v-\1:p')" if [ -z "$vcache" ] then echo "That isn't a \"/watch?v=XXXX\" type url. Quitting." exit 1 fi if [ ! -f "$vcache.html" ] then # echo "wget $1 -O $vcache.html" wget $1 -O "$vcache.html~" mv $vcache.html~ $vcache.html fi gvext="$(grep -a fullscreen $vcache.html | cut -d? -f2 | cut -d\" -f1)" if [ -z "$gvext" ] then if [ -n "${COOKIES-}" ] then rm $vcache.html echo "Couldn't get filename. Retrying with cookies..." wget --load-cookies $COOKIES $1 -O "$vcache.html~" mv $vcache.html~ $vcache.html gvext="$(grep -a player2.swf $vcache.html | cut -d? -f2 | cut -d\" -f1)" if [ -z "$gvext" ] then echo "Retried with cookies. Still couldn't find video's filename in the html file." mv -f $vcache.html $vcache.html~ exit 1 fi else mv -f $vcache.html $vcache.html~ echo "Couldn't find video's filename in the html file. Is this a flagged video?" echo "Try setting COOKIES to a valid cookie file containing the login cookies and the is_adult cookie." exit 1 fi fi gvext="$GETVIDEO$gvext" # # grab the title and clean it up a bit. title=$(grep -a 'meta name="title"' $vcache.html | sed -n -e 's/.*content="\(.*\)".*/\1/p' | sed -e 's/[^a-z0-9A-Z]/_/g' -e 's/___*/_/g' -e 's/_$//' -e 's/^_//') if [ -z "$title" ] then echo "get_video: '$gvext'" echo "Couldn't find video's title in the html file." exit 1 fi # echo "$1 -> $title.flv" if [ ! -f "$title.flv" ] then wget "$gvext" -O "$title.flv~" if [ -s "$title.flv~" ] then mv "$title.flv~" "$title.flv" else rm "$title.flv~" fi fi if [ -f $LOCK ] then echo "Another mplayer is running, waiting" while [ -f $LOCK ] do sleep 5 done fi if [ -z "$testflag" ] then touch $LOCK mplayer -fs "$title.flv" rm $LOCK fi echo "cd $PWD; mplayer -fs \"$title.flv\"" # uncomment to not permanently cache the v= html file. # rm $vcache.html } # called with URL and TMPFILENAME playmulti() { if [ -z "$2" ] then echo "$1 filename parse failure" exit 1 fi if [ ! -f "$2.html" ] then wget $1 -O "$2.html~" mv $2.html~ $2.html fi # grab these lines avoiding dups with "Openmoko 1 vurllist=$(grep '/watch?v=' $2.html | grep -v '