use new gdata api to get video key

This commit is contained in:
j 2008-10-07 22:22:17 +02:00
parent 5a5b0f085a
commit b02bac2926

View file

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from urllib import quote from urllib import quote
import httplib
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import re import re
@ -9,25 +10,44 @@ from oxlib.cache import getUrl, getUrlUnicode
from oxlib import findString, findRe from oxlib import findString, findRe
def getVideoUrl(youtubeId, format='mp4'): def getVideoKey(youtubeId):
try:
conn = httplib.HTTPConnection ("www.youtube.com")
conn.request ("HEAD", '/v/' + youtubeId)
response = conn.getresponse ()
conn.close ()
except:
return False
if response.status >= 300 and response.status < 400:
location = response.getheader("location")
return re.match(".*[?&]t=([^&]+)", location).groups()[0]
else:
return False
def getVideoKeyLegacyAPI(videoId):
url = 'http://www.youtube.com/api2_rest?method=youtube.videos.get_video_token&video_id=' + youtubeId url = 'http://www.youtube.com/api2_rest?method=youtube.videos.get_video_token&video_id=' + youtubeId
data = getUrl(url) data = getUrl(url)
xml = ET.fromstring(data) xml = ET.fromstring(data)
youtubeKey = xml.find('t').text youtubeKey = xml.find('t').text
return youtubeKey
def getVideoUrl(youtubeId, format='mp4'):
youtubeKey = getVideoKey(youtubeId)
if format == 'mp4': if format == 'mp4':
fmt=18 fmt=18
url = "http://youtube.com/get_video.php?video_id=%s&t=%s&fmt=%s"%(youtubeId, youtubeKey, fmt) url = "http://youtube.com/get_video.php?video_id=%s&t=%s&fmt=%s" % (youtubeId, youtubeKey, fmt)
else: else:
url = "http://youtube.com/get_video.php?video_id=%s&t=%s"%(youtubeId, youtubeKey) url = "http://youtube.com/get_video.php?video_id=%s&t=%s" % (youtubeId, youtubeKey)
return url return url
'''
def getMovieInfo(youtubeId): def getMovieInfo(youtubeId):
url = "http://gdata.youtube.com/feeds/api/videos/%s " % youtubeId url = "http://gdata.youtube.com/feeds/api/videos/%s " % youtubeId
data = getUrl(url) data = getUrl(url)
fd = feedparser.parse(data) fd = feedparser.parse(data)
return getInfoFromAtom(fd.entries[0]) return getInfoFromAtom(fd.entries[0])
'''
def getInfoFromAtom(entry): def getInfoFromAtom(entry):
info = dict() info = dict()
info['title'] = entry['title'] info['title'] = entry['title']
@ -75,7 +95,7 @@ def find(query, max_results=10, offset=1, orderBy='relevance', video_url_base=No
if video_url_base: if video_url_base:
v['video_link'] = "%s/%s" % (video_url_base, v['id']) v['video_link'] = "%s/%s" % (video_url_base, v['id'])
else: else:
v['video_url'] = get_video_url(v['id']) v['video_url'] = getVideoUrl(v['id'])
v['description'] = findRe(data, 'BeginvidDesc%s">(.*?)</span>' % v['id']).strip().replace('<b>', ' ').replace('</b>', '') v['description'] = findRe(data, 'BeginvidDesc%s">(.*?)</span>' % v['id']).strip().replace('<b>', ' ').replace('</b>', '')
v['thumbnail'] = video[1] v['thumbnail'] = video[1]
videos[vid] = v videos[vid] = v
@ -83,4 +103,3 @@ def find(query, max_results=10, offset=1, orderBy='relevance', video_url_base=No
return videos.values() return videos.values()
return videos.values() return videos.values()