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 -*-
# vi:si:et:sw=4:sts=4:ts=4
from urllib import quote
import httplib
import xml.etree.ElementTree as ET
import re
@ -9,25 +10,44 @@ from oxlib.cache import getUrl, getUrlUnicode
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
data = getUrl(url)
xml = ET.fromstring(data)
youtubeKey = xml.find('t').text
return youtubeKey
def getVideoUrl(youtubeId, format='mp4'):
youtubeKey = getVideoKey(youtubeId)
if format == 'mp4':
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:
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
'''
def getMovieInfo(youtubeId):
url = "http://gdata.youtube.com/feeds/api/videos/%s " % youtubeId
data = getUrl(url)
fd = feedparser.parse(data)
return getInfoFromAtom(fd.entries[0])
'''
def getInfoFromAtom(entry):
info = dict()
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:
v['video_link'] = "%s/%s" % (video_url_base, v['id'])
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['thumbnail'] = video[1]
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()