From b02bac2926a64fc6677be25d7a8bbb6a71b2b413 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 7 Oct 2008 22:22:17 +0200 Subject: [PATCH] use new gdata api to get video key --- oxweb/youtube.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/oxweb/youtube.py b/oxweb/youtube.py index 2b3d602..4595498 100644 --- a/oxweb/youtube.py +++ b/oxweb/youtube.py @@ -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">(.*?)' % v['id']).strip().replace('', ' ').replace('', '') 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() -