From bd16e95a395701e44060fdfa1a65fd59e7c86b5e Mon Sep 17 00:00:00 2001 From: Justin Forest Date: Thu, 6 Nov 2008 23:49:03 +0300 Subject: [PATCH] Updated YouTube downloader YouTube changed their server side logic and the old way of resolving enclosure URLs stopped working. The new method is taken from clive. --- src/gpodder/resolver.py | 30 +++++++++--------------------- src/gpodder/util.py | 6 +++--- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/gpodder/resolver.py b/src/gpodder/resolver.py index fb99d6fb..7b56f36d 100644 --- a/src/gpodder/resolver.py +++ b/src/gpodder/resolver.py @@ -33,28 +33,16 @@ from xml.sax import saxutils from gpodder.liblogger import log from gpodder.util import proxy_request -rr = {} - def get_real_download_url(url, proxy=None): - if 'youtube-episode' not in rr: - rr['youtube-episode'] = re.compile('http://(?:[a-z]+\.)?youtube\.com/v/.*\.swf', re.IGNORECASE) - - if rr['youtube-episode'].match(url): - req = proxy_request(url, proxy) - - if 'location' in req.msg: - id, tag = (None, None) - - for part in req.msg['location'].split('&'): - if part.startswith('video_id='): - id = part[9:] - elif part.startswith('t='): - tag = part[2:] - - if id is not None and tag is not None: - next = 'http://www.youtube.com/get_video?video_id='+ id +'&t='+ tag +'&fmt=18' - log('YouTube link resolved: %s => %s', url, next) - return next + r1 = re.compile('http://(?:[a-z]+\.)?youtube\.com/v/(.*)\.swf', re.IGNORECASE).match(url) + if r1 is not None: + page = proxy_request('http://www.youtube.com/watch?v=' + r1.group(1), proxy, method='GET').read() + + r2 = re.compile('.*"t"\:\s+"([^"]+)".*').search(page) + if r2: + next = 'http://www.youtube.com/get_video?video_id=' + r1.group(1) + '&t=' + r2.group(1) + '&fmt=18' + log('YouTube link resolved: %s => %s', url, next) + return next return url diff --git a/src/gpodder/util.py b/src/gpodder/util.py index f346108b..65c3dced 100644 --- a/src/gpodder/util.py +++ b/src/gpodder/util.py @@ -845,16 +845,16 @@ def format_seconds_to_hour_min_sec(seconds): else: return result[0] -def proxy_request(url, proxy=None): +def proxy_request(url, proxy=None, method='HEAD'): if proxy is None or proxy.strip() == '': (scheme, netloc, path, parms, qry, fragid) = urlparse.urlparse(url) conn = httplib.HTTPConnection(netloc) start = len(scheme) + len('://') + len(netloc) - conn.request('HEAD', url[start:]) + conn.request(method, url[start:]) else: (scheme, netloc, path, parms, qry, fragid) = urlparse.urlparse(proxy) conn = httplib.HTTPConnection(netloc) - conn.request('HEAD', url) + conn.request(method, url) return conn.getresponse() -- 2.11.4.GIT