]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/bliptv.py
[ebaumsworld] Modernize
[youtube-dl.git] / youtube_dl / extractor / bliptv.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from .subtitles import SubtitlesInfoExtractor
7 from ..utils import (
8     compat_urllib_request,
9     unescapeHTML,
10     parse_iso8601,
11     compat_urlparse,
12     clean_html,
13     compat_str,
14 )
15
16
17 class BlipTVIE(SubtitlesInfoExtractor):
18     _VALID_URL = r'https?://(?:\w+\.)?blip\.tv/(?:(?:.+-|rss/flash/)(?P<id>\d+)|((?:play/|api\.swf#)(?P<lookup_id>[\da-zA-Z+_TESTS]+)))'
19
20     _TESTS = [
21         {
22             'url': 'http://blip.tv/cbr/cbr-exclusive-gotham-city-imposters-bats-vs-jokerz-short-3-5796352',
23             'md5': 'c6934ad0b6acf2bd920720ec888eb812',
24             'info_dict': {
25                 'id': '5779306',
26                 'ext': 'mov',
27                 'title': 'CBR EXCLUSIVE: "Gotham City Imposters" Bats VS Jokerz Short 3',
28                 'description': 'md5:9bc31f227219cde65e47eeec8d2dc596',
29                 'timestamp': 1323138843,
30                 'upload_date': '20111206',
31                 'uploader': 'cbr',
32                 'uploader_id': '679425',
33                 'duration': 81,
34             }
35         },
36         {
37             # https://github.com/rg3/youtube-dl/pull/2274
38             'note': 'Video with subtitles',
39             'url': 'http://blip.tv/play/h6Uag5OEVgI.html',
40             'md5': '309f9d25b820b086ca163ffac8031806',
41             'info_dict': {
42                 'id': '6586561',
43                 'ext': 'mp4',
44                 'title': 'Red vs. Blue Season 11 Episode 1',
45                 'description': 'One-Zero-One',
46                 'timestamp': 1371261608,
47                 'upload_date': '20130615',
48                 'uploader': 'redvsblue',
49                 'uploader_id': '792887',
50                 'duration': 279,
51             }
52         },
53         {
54             # https://bugzilla.redhat.com/show_bug.cgi?id=967465
55             'url': 'http://a.blip.tv/api.swf#h6Uag5KbVwI',
56             'md5': '314e87b1ebe7a48fcbfdd51b791ce5a6',
57             'info_dict': {
58                 'id': '6573122',
59                 'ext': 'mov',
60                 'upload_date': '20130520',
61                 'description': 'Two hapless space marines argue over what to do when they realize they have an astronomically huge problem on their hands.',
62                 'title': 'Red vs. Blue Season 11 Trailer',
63                 'timestamp': 1369029609,
64                 'uploader': 'redvsblue',
65                 'uploader_id': '792887',
66             }
67         }
68     ]
69
70     def _real_extract(self, url):
71         mobj = re.match(self._VALID_URL, url)
72         lookup_id = mobj.group('lookup_id')
73
74         # See https://github.com/rg3/youtube-dl/issues/857
75         if lookup_id:
76             info_page = self._download_webpage(
77                 'http://blip.tv/play/%s.x?p=1' % lookup_id, lookup_id, 'Resolving lookup id')
78             video_id = self._search_regex(r'data-episode-id="([0-9]+)', info_page, 'video_id')
79         else:
80             video_id = mobj.group('id')
81
82         rss = self._download_xml('http://blip.tv/rss/flash/%s' % video_id, video_id, 'Downloading video RSS')
83
84         def blip(s):
85             return '{http://blip.tv/dtd/blip/1.0}%s' % s
86
87         def media(s):
88             return '{http://search.yahoo.com/mrss/}%s' % s
89
90         def itunes(s):
91             return '{http://www.itunes.com/dtds/podcast-1.0.dtd}%s' % s
92
93         item = rss.find('channel/item')
94
95         video_id = item.find(blip('item_id')).text
96         title = item.find('./title').text
97         description = clean_html(compat_str(item.find(blip('puredescription')).text))
98         timestamp = parse_iso8601(item.find(blip('datestamp')).text)
99         uploader = item.find(blip('user')).text
100         uploader_id = item.find(blip('userid')).text
101         duration = int(item.find(blip('runtime')).text)
102         media_thumbnail = item.find(media('thumbnail'))
103         thumbnail = media_thumbnail.get('url') if media_thumbnail is not None else item.find(itunes('image')).text
104         categories = [category.text for category in item.findall('category')]
105
106         formats = []
107         subtitles = {}
108
109         media_group = item.find(media('group'))
110         for media_content in media_group.findall(media('content')):
111             url = media_content.get('url')
112             role = media_content.get(blip('role'))
113             msg = self._download_webpage(
114                 url + '?showplayer=20140425131715&referrer=http://blip.tv&mask=7&skin=flashvars&view=url',
115                 video_id, 'Resolving URL for %s' % role)
116             real_url = compat_urlparse.parse_qs(msg)['message'][0]
117
118             media_type = media_content.get('type')
119             if media_type == 'text/srt' or url.endswith('.srt'):
120                 LANGS = {
121                     'english': 'en',
122                 }
123                 lang = role.rpartition('-')[-1].strip().lower()
124                 langcode = LANGS.get(lang, lang)
125                 subtitles[langcode] = url
126             elif media_type.startswith('video/'):
127                 formats.append({
128                     'url': real_url,
129                     'format_id': role,
130                     'format_note': media_type,
131                     'vcodec': media_content.get(blip('vcodec')),
132                     'acodec': media_content.get(blip('acodec')),
133                     'filesize': media_content.get('filesize'),
134                     'width': int(media_content.get('width')),
135                     'height': int(media_content.get('height')),
136                 })
137         self._sort_formats(formats)
138
139         # subtitles
140         video_subtitles = self.extract_subtitles(video_id, subtitles)
141         if self._downloader.params.get('listsubtitles', False):
142             self._list_available_subtitles(video_id, subtitles)
143             return
144
145         return {
146             'id': video_id,
147             'title': title,
148             'description': description,
149             'timestamp': timestamp,
150             'uploader': uploader,
151             'uploader_id': uploader_id,
152             'duration': duration,
153             'thumbnail': thumbnail,
154             'categories': categories,
155             'formats': formats,
156             'subtitles': video_subtitles,
157         }
158
159     def _download_subtitle_url(self, sub_lang, url):
160         # For some weird reason, blip.tv serves a video instead of subtitles
161         # when we request with a common UA
162         req = compat_urllib_request.Request(url)
163         req.add_header('Youtubedl-user-agent', 'youtube-dl')
164         return self._download_webpage(req, None, note=False)
165
166
167 class BlipTVUserIE(InfoExtractor):
168     _VALID_URL = r'(?:(?:(?:https?://)?(?:\w+\.)?blip\.tv/)|bliptvuser:)(?!api\.swf)([^/]+)/*$'
169     _PAGE_SIZE = 12
170     IE_NAME = 'blip.tv:user'
171
172     def _real_extract(self, url):
173         mobj = re.match(self._VALID_URL, url)
174         username = mobj.group(1)
175
176         page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
177
178         page = self._download_webpage(url, username, 'Downloading user page')
179         mobj = re.search(r'data-users-id="([^"]+)"', page)
180         page_base = page_base % mobj.group(1)
181
182         # Download video ids using BlipTV Ajax calls. Result size per
183         # query is limited (currently to 12 videos) so we need to query
184         # page by page until there are no video ids - it means we got
185         # all of them.
186
187         video_ids = []
188         pagenum = 1
189
190         while True:
191             url = page_base + "&page=" + str(pagenum)
192             page = self._download_webpage(
193                 url, username, 'Downloading video ids from page %d' % pagenum)
194
195             # Extract video identifiers
196             ids_in_page = []
197
198             for mobj in re.finditer(r'href="/([^"]+)"', page):
199                 if mobj.group(1) not in ids_in_page:
200                     ids_in_page.append(unescapeHTML(mobj.group(1)))
201
202             video_ids.extend(ids_in_page)
203
204             # A little optimization - if current page is not
205             # "full", ie. does not contain PAGE_SIZE video ids then
206             # we can assume that this page is the last one - there
207             # are no more ids on further pages - no need to query
208             # again.
209
210             if len(ids_in_page) < self._PAGE_SIZE:
211                 break
212
213             pagenum += 1
214
215         urls = ['http://blip.tv/%s' % video_id for video_id in video_ids]
216         url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
217         return [self.playlist_result(url_entries, playlist_title=username)]