initial commit

This commit is contained in:
josch 2014-06-26 14:28:46 +02:00
commit c262c5ce96
2 changed files with 185 additions and 0 deletions

74
youtube-dl.py Normal file
View file

@ -0,0 +1,74 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "J. Schauer <josch@pyneo.org>"
__version__ = "prototype"
__copyright__ = "Copyright (c) 2010 J. Schauer"
__license__ = "AGPLv3"
from imp import load_source
youtube = load_source('youtube', '/usr/bin/youtube-dl')
for elm in ['YoutubeIE', 'MetacafeIE', 'DailymotionIE', 'YoutubePlaylistIE',
'YoutubeUserIE', 'YoutubeSearchIE', 'GoogleIE', 'GoogleSearchIE',
'PhotobucketIE', 'YahooIE', 'YahooSearchIE', 'GenericIE',
'FileDownloader', 'PostProcessor']:
globals()[elm] = getattr(youtube, elm)
del youtube
FileDownloader.process_info = lambda self, info_dict : setattr(self, 'info_dict', info_dict)
import urllib2
from urllib import splittype, splithost
def getytdlurl(ytid):
youtube_ie = YoutubeIE()
metacafe_ie = MetacafeIE(youtube_ie)
dailymotion_ie = DailymotionIE()
youtube_pl_ie = YoutubePlaylistIE(youtube_ie)
youtube_user_ie = YoutubeUserIE(youtube_ie)
youtube_search_ie = YoutubeSearchIE(youtube_ie)
google_ie = GoogleIE()
google_search_ie = GoogleSearchIE(google_ie)
photobucket_ie = PhotobucketIE()
yahoo_ie = YahooIE()
yahoo_search_ie = YahooSearchIE(yahoo_ie)
generic_ie = GenericIE()
fd = FileDownloader({
'username': None,
'password': None,
'format': None,
'quiet': True,
'simulate': True,
'ignoreerrors': False
})
fd.add_info_extractor(youtube_search_ie)
fd.add_info_extractor(youtube_pl_ie)
fd.add_info_extractor(youtube_user_ie)
fd.add_info_extractor(metacafe_ie)
fd.add_info_extractor(dailymotion_ie)
fd.add_info_extractor(youtube_ie)
fd.add_info_extractor(google_ie)
fd.add_info_extractor(google_search_ie)
fd.add_info_extractor(photobucket_ie)
fd.add_info_extractor(yahoo_ie)
fd.add_info_extractor(yahoo_search_ie)
retcode = fd.download((
ytid,
))
if retcode:
return
u = fd.info_dict['url'].encode('UTF-8', 'xmlcharrefreplace') # title, url, thumbnail, description
opener = urllib2.build_opener()
f = opener.open(urllib2.Request(u))
_, rest = splittype(f.url)
host, path = splithost(rest)
return (host, path,
fd.info_dict['stitle'].encode('UTF-8', 'xmlcharrefreplace'),
fd.info_dict['id'].encode('UTF-8', 'xmlcharrefreplace'),
fd.info_dict['ext'].encode('UTF-8', 'xmlcharrefreplace'))
if __name__ == '__main__':
getytdlurl('gR4OM5tmzno')

111
youtube-dl.sh Executable file
View file

@ -0,0 +1,111 @@
#!/bin/sh -e
read request
while /bin/true; do
read header
[ "$header" = "`printf '\r'`" ] && break
done
code="${request#GET /}"
code="${code% HTTP/*}"
urldecode() { echo -n $1 | sed 's/%\([0-9A-F]\{2\}\)/\\\\\\\x\1/gI' | xargs printf; }
qualityisgreater() {
if [ "$1" = "hd1080" ] && [ "$2" != "hd1080" ]; then
return 0
elif [ "$2" = "hd1080" ]; then
return 1
elif [ $1 = "hd720" ] && [ "$2" != "hd720" ]; then
return 0
elif [ "$2" = "hd720" ]; then
return 1
elif [ $1 = "large" ] && [ "$2" != "large" ]; then
return 0
elif [ "$2" = "large" ]; then
return 1
elif [ $1 = "medium" ] && [ "$2" != "medium" ]; then
return 0
elif [ "$2" = "medium" ]; then
return 1
fi
return 1
}
cookiejar=`mktemp`
baseurl="http://www.youtube.com/get_video_info?video_id=$code&el=detailpage&ps=default&eurl=&gl=US&hl=en"
data=`curl --silent --cookie-jar "$cookiejar" "$baseurl"`
highestquality=small
highesturl=""
highesttype=video/x-flv
title=""
for part in `echo $data | tr '&' ' '`; do
key=`echo $part | cut -d"=" -f1`
value=`echo $part | cut -d"=" -f2`
if [ "$value" != "" ]; then
value=`urldecode "$value"`
fi
case "$key" in
"url_encoded_fmt_stream_map")
for format in `echo $value | tr ',' ' '`; do
for part in `echo $format | tr '&' ' '`; do
key=`echo $part | cut -d"=" -f1`
value=`echo $part | cut -d"=" -f2`
if [ "$value" != "" ]; then
value=`urldecode "$value"`
fi
case "$key" in
"url")
url=$value;;
"quality")
quality=$value;;
"fallback_host")
fallback_host=$value;;
"type")
type=$value;;
"itag")
itag=$value;;
esac
done
if [ $quality = $highestquality ]; then
if [ $type = "video/webm" ]; then
highesttype=$type
highesturl=$url
highestquality=$quality
elif [ $type = "video/mp4" ] && [ $highesttype != "video/webm" ]; then
highesttype=$type
highesturl=$url
highestquality=$quality
fi
elif qualityisgreater $quality $highestquality; then
highesttype=$type
highesturl=$url
highestquality=$quality
fi
done ;;
"title") title="$value" ;;
esac
done
if [ "$highesturl" = "" ]; then
echo "fail $code" >&2
echo HTTP/1.1 200 OK
echo Content-Type: text/plain
echo
echo "error :("
else
url=`curl --silent --head --output /dev/null --write-out %{redirect_url} --cookie "$cookiejar" "$highesturl"`
while [ "$url" != "" ]; do
highesturl=$url
url=`curl --silent --head --output /dev/null --write-out %{redirect_url} --cookie "$cookiejar" "$highesturl"`
done
echo "success $code" >&2
curl --silent --include --cookie "$cookiejar" "$highesturl"
fi
rm $cookiejar