[Podcasts] fix linking to podcasts w/o title
[mygpo.git] / mygpo / data / youtube.py
blob2efbcf721926e20a1102f842b015dcbc65b1f75f
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
4 # This file is part of my.gpodder.org.
6 # my.gpodder.org is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or (at your
9 # option) any later version.
11 # my.gpodder.org is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
14 # License for more details.
16 # You should have received a copy of the GNU Affero General Public License
17 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
20 # taken from gPodder :)
22 import re
23 import urllib
25 def is_video_link(url):
26 return (get_youtube_id(url) is not None)
28 def get_youtube_id(url):
29 if url is None:
30 return None
32 r = re.compile('http://(?:[a-z]+\.)?youtube\.com/v/(.*)\.swf', re.IGNORECASE).match(url)
33 if r is not None:
34 return r.group(1)
36 r = re.compile('http://(?:[a-z]+\.)?youtube\.com/watch\?v=([^&]*)', re.IGNORECASE).match(url)
37 if r is not None:
38 return r.group(1)
40 return None
43 def get_real_cover(url):
44 rs = [re.compile('http://www\\.youtube\\.com/rss/user/([^/]+)/videos\\.rss', re.IGNORECASE),
45 re.compile('http://www\\.youtube\\.com/profile_videos\\?user=([^\&]+)', re.IGNORECASE)]
47 for r in rs:
48 m = r.match(url)
49 if m is None:
50 continue
51 username = m.group(1)
52 api_url = 'http://gdata.youtube.com/feeds/api/users/%s?v=2' % username
53 data = urllib.urlopen(api_url).read()
54 match = re.search('<media:thumbnail url=[\'"]([^\'"]+)[\'"]/>', data)
55 if match is not None:
56 return match.group(1)
58 return None