Support for YouTube in CoverDownloader.
[gpodder.git] / src / gpodder / resolver.py
blobe51402f8a206c8eac1795deed14d497e8f97faeb
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2008 Thomas Perl and the gPodder Team
6 # gPodder is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # resolver.py -- YouTube and related magic
20 # Justin Forest <justin.forest@gmail.com> 2008-10-13
22 # TODO:
24 # * Channel covers.
25 # * Support for Vimeo, maybe blip.tv and others.
27 import re
28 import urllib2
29 from gpodder.liblogger import log
30 from gpodder.util import proxy_request
32 rr = {}
34 def get_real_download_url(url, proxy=None):
35 if 'youtube-episode' not in rr:
36 rr['youtube-episode'] = re.compile('http://(?:[a-z]+\.)?youtube\.com/v/.*\.swf', re.IGNORECASE)
38 if rr['youtube-episode'].match(url):
39 req = proxy_request(url, proxy)
41 if 'location' in req.msg:
42 id, tag = (None, None)
44 for part in req.msg['location'].split('&'):
45 if part.startswith('video_id='):
46 id = part[9:]
47 elif part.startswith('t='):
48 tag = part[2:]
50 if id is not None and tag is not None:
51 next = 'http://www.youtube.com/get_video?video_id='+ id +'&t='+ tag +'&fmt=18'
52 log('YouTube link resolved: %s => %s', url, next)
53 return next
55 return url
57 def get_real_channel_url(url):
58 r = re.compile('http://(?:[a-z]+\.)?youtube\.com/user/([a-z0-9]+)', re.IGNORECASE)
59 m = r.match(url)
61 if m is not None:
62 next = 'http://www.youtube.com/rss/user/'+ m.group(1) +'/videos.rss'
63 log('YouTube link resolved: %s => %s', url, next)
64 return next
66 r = re.compile('http://(?:[a-z]+\.)?youtube\.com/profile?user=([a-z0-9]+)', re.IGNORECASE)
67 m = r.match(url)
69 if m is not None:
70 next = 'http://www.youtube.com/rss/user/'+ m.group(1) +'/videos.rss'
71 log('YouTube link resolved: %s => %s', url, next)
72 return next
74 return url
76 def get_real_cover(url):
77 log('Cover: %s', url)
79 r = re.compile('http://www\.youtube\.com/rss/user/([a-z0-9]+)/videos\.rss', re.IGNORECASE)
80 m = r.match(url)
82 if m is not None:
83 data = urllib2.urlopen('http://www.youtube.com/user/'+ m.group(1)).read()
84 data = data[data.find('id="user-profile-image"'):]
85 data = data[data.find('src="') + 5:]
87 next = data[:data.find('"')]
89 if next.strip() == '':
90 return None
92 log('YouTube userpic for %s is: %s', url, next)
93 return next
95 return None