]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bliptv.py
Merge tag 'upstream/2015.06.04.1'
[youtubedl] / youtube_dl / extractor / bliptv.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6
7 from ..compat import (
8 compat_str,
9 compat_urllib_request,
10 compat_urlparse,
11 )
12 from ..utils import (
13 clean_html,
14 int_or_none,
15 parse_iso8601,
16 unescapeHTML,
17 )
18
19
20 class BlipTVIE(InfoExtractor):
21 _VALID_URL = r'https?://(?:\w+\.)?blip\.tv/(?:(?:.+-|rss/flash/)(?P<id>\d+)|((?:play/|api\.swf#)(?P<lookup_id>[\da-zA-Z+_]+)))'
22
23 _TESTS = [
24 {
25 'url': 'http://blip.tv/cbr/cbr-exclusive-gotham-city-imposters-bats-vs-jokerz-short-3-5796352',
26 'md5': 'c6934ad0b6acf2bd920720ec888eb812',
27 'info_dict': {
28 'id': '5779306',
29 'ext': 'mov',
30 'title': 'CBR EXCLUSIVE: "Gotham City Imposters" Bats VS Jokerz Short 3',
31 'description': 'md5:9bc31f227219cde65e47eeec8d2dc596',
32 'timestamp': 1323138843,
33 'upload_date': '20111206',
34 'uploader': 'cbr',
35 'uploader_id': '679425',
36 'duration': 81,
37 }
38 },
39 {
40 # https://github.com/rg3/youtube-dl/pull/2274
41 'note': 'Video with subtitles',
42 'url': 'http://blip.tv/play/h6Uag5OEVgI.html',
43 'md5': '309f9d25b820b086ca163ffac8031806',
44 'info_dict': {
45 'id': '6586561',
46 'ext': 'mp4',
47 'title': 'Red vs. Blue Season 11 Episode 1',
48 'description': 'One-Zero-One',
49 'timestamp': 1371261608,
50 'upload_date': '20130615',
51 'uploader': 'redvsblue',
52 'uploader_id': '792887',
53 'duration': 279,
54 }
55 },
56 {
57 # https://bugzilla.redhat.com/show_bug.cgi?id=967465
58 'url': 'http://a.blip.tv/api.swf#h6Uag5KbVwI',
59 'md5': '314e87b1ebe7a48fcbfdd51b791ce5a6',
60 'info_dict': {
61 'id': '6573122',
62 'ext': 'mov',
63 'upload_date': '20130520',
64 'description': 'Two hapless space marines argue over what to do when they realize they have an astronomically huge problem on their hands.',
65 'title': 'Red vs. Blue Season 11 Trailer',
66 'timestamp': 1369029609,
67 'uploader': 'redvsblue',
68 'uploader_id': '792887',
69 }
70 },
71 {
72 'url': 'http://blip.tv/play/gbk766dkj4Yn',
73 'md5': 'fe0a33f022d49399a241e84a8ea8b8e3',
74 'info_dict': {
75 'id': '1749452',
76 'ext': 'mp4',
77 'upload_date': '20090208',
78 'description': 'Witness the first appearance of the Nostalgia Critic character, as Doug reviews the movie Transformers.',
79 'title': 'Nostalgia Critic: Transformers',
80 'timestamp': 1234068723,
81 'uploader': 'NostalgiaCritic',
82 'uploader_id': '246467',
83 }
84 },
85 {
86 # https://github.com/rg3/youtube-dl/pull/4404
87 'note': 'Audio only',
88 'url': 'http://blip.tv/hilarios-productions/weekly-manga-recap-kingdom-7119982',
89 'md5': '76c0a56f24e769ceaab21fbb6416a351',
90 'info_dict': {
91 'id': '7103299',
92 'ext': 'flv',
93 'title': 'Weekly Manga Recap: Kingdom',
94 'description': 'And then Shin breaks the enemy line, and he&apos;s all like HWAH! And then he slices a guy and it&apos;s all like FWASHING! And... it&apos;s really hard to describe the best parts of this series without breaking down into sound effects, okay?',
95 'timestamp': 1417660321,
96 'upload_date': '20141204',
97 'uploader': 'The Rollo T',
98 'uploader_id': '407429',
99 'duration': 7251,
100 'vcodec': 'none',
101 }
102 },
103 ]
104
105 @staticmethod
106 def _extract_url(webpage):
107 mobj = re.search(r'<meta\s[^>]*https?://api\.blip\.tv/\w+/redirect/\w+/(\d+)', webpage)
108 if mobj:
109 return 'http://blip.tv/a/a-' + mobj.group(1)
110 mobj = re.search(r'<(?:iframe|embed|object)\s[^>]*(https?://(?:\w+\.)?blip\.tv/(?:play/|api\.swf#)[a-zA-Z0-9_]+)', webpage)
111 if mobj:
112 return mobj.group(1)
113
114 def _real_extract(self, url):
115 mobj = re.match(self._VALID_URL, url)
116 lookup_id = mobj.group('lookup_id')
117
118 # See https://github.com/rg3/youtube-dl/issues/857 and
119 # https://github.com/rg3/youtube-dl/issues/4197
120 if lookup_id:
121 urlh = self._request_webpage(
122 'http://blip.tv/play/%s' % lookup_id, lookup_id, 'Resolving lookup id')
123 url = compat_urlparse.urlparse(urlh.geturl())
124 qs = compat_urlparse.parse_qs(url.query)
125 mobj = re.match(self._VALID_URL, qs['file'][0])
126
127 video_id = mobj.group('id')
128
129 rss = self._download_xml('http://blip.tv/rss/flash/%s' % video_id, video_id, 'Downloading video RSS')
130
131 def blip(s):
132 return '{http://blip.tv/dtd/blip/1.0}%s' % s
133
134 def media(s):
135 return '{http://search.yahoo.com/mrss/}%s' % s
136
137 def itunes(s):
138 return '{http://www.itunes.com/dtds/podcast-1.0.dtd}%s' % s
139
140 item = rss.find('channel/item')
141
142 video_id = item.find(blip('item_id')).text
143 title = item.find('./title').text
144 description = clean_html(compat_str(item.find(blip('puredescription')).text))
145 timestamp = parse_iso8601(item.find(blip('datestamp')).text)
146 uploader = item.find(blip('user')).text
147 uploader_id = item.find(blip('userid')).text
148 duration = int(item.find(blip('runtime')).text)
149 media_thumbnail = item.find(media('thumbnail'))
150 thumbnail = media_thumbnail.get('url') if media_thumbnail is not None else item.find(itunes('image')).text
151 categories = [category.text for category in item.findall('category')]
152
153 formats = []
154 subtitles_urls = {}
155
156 media_group = item.find(media('group'))
157 for media_content in media_group.findall(media('content')):
158 url = media_content.get('url')
159 role = media_content.get(blip('role'))
160 msg = self._download_webpage(
161 url + '?showplayer=20140425131715&referrer=http://blip.tv&mask=7&skin=flashvars&view=url',
162 video_id, 'Resolving URL for %s' % role)
163 real_url = compat_urlparse.parse_qs(msg.strip())['message'][0]
164
165 media_type = media_content.get('type')
166 if media_type == 'text/srt' or url.endswith('.srt'):
167 LANGS = {
168 'english': 'en',
169 }
170 lang = role.rpartition('-')[-1].strip().lower()
171 langcode = LANGS.get(lang, lang)
172 subtitles_urls[langcode] = url
173 elif media_type.startswith('video/'):
174 formats.append({
175 'url': real_url,
176 'format_id': role,
177 'format_note': media_type,
178 'vcodec': media_content.get(blip('vcodec')) or 'none',
179 'acodec': media_content.get(blip('acodec')),
180 'filesize': media_content.get('filesize'),
181 'width': int_or_none(media_content.get('width')),
182 'height': int_or_none(media_content.get('height')),
183 })
184 self._check_formats(formats, video_id)
185 self._sort_formats(formats)
186
187 subtitles = self.extract_subtitles(video_id, subtitles_urls)
188
189 return {
190 'id': video_id,
191 'title': title,
192 'description': description,
193 'timestamp': timestamp,
194 'uploader': uploader,
195 'uploader_id': uploader_id,
196 'duration': duration,
197 'thumbnail': thumbnail,
198 'categories': categories,
199 'formats': formats,
200 'subtitles': subtitles,
201 }
202
203 def _get_subtitles(self, video_id, subtitles_urls):
204 subtitles = {}
205 for lang, url in subtitles_urls.items():
206 # For some weird reason, blip.tv serves a video instead of subtitles
207 # when we request with a common UA
208 req = compat_urllib_request.Request(url)
209 req.add_header('User-Agent', 'youtube-dl')
210 subtitles[lang] = [{
211 # The extension is 'srt' but it's actually an 'ass' file
212 'ext': 'ass',
213 'data': self._download_webpage(req, None, note=False),
214 }]
215 return subtitles
216
217
218 class BlipTVUserIE(InfoExtractor):
219 _VALID_URL = r'(?:(?:https?://(?:\w+\.)?blip\.tv/)|bliptvuser:)(?!api\.swf)([^/]+)/*$'
220 _PAGE_SIZE = 12
221 IE_NAME = 'blip.tv:user'
222 _TEST = {
223 'url': 'http://blip.tv/actone',
224 'info_dict': {
225 'id': 'actone',
226 'title': 'Act One: The Series',
227 },
228 'playlist_count': 5,
229 }
230
231 def _real_extract(self, url):
232 mobj = re.match(self._VALID_URL, url)
233 username = mobj.group(1)
234
235 page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
236
237 page = self._download_webpage(url, username, 'Downloading user page')
238 mobj = re.search(r'data-users-id="([^"]+)"', page)
239 page_base = page_base % mobj.group(1)
240 title = self._og_search_title(page)
241
242 # Download video ids using BlipTV Ajax calls. Result size per
243 # query is limited (currently to 12 videos) so we need to query
244 # page by page until there are no video ids - it means we got
245 # all of them.
246
247 video_ids = []
248 pagenum = 1
249
250 while True:
251 url = page_base + "&page=" + str(pagenum)
252 page = self._download_webpage(
253 url, username, 'Downloading video ids from page %d' % pagenum)
254
255 # Extract video identifiers
256 ids_in_page = []
257
258 for mobj in re.finditer(r'href="/([^"]+)"', page):
259 if mobj.group(1) not in ids_in_page:
260 ids_in_page.append(unescapeHTML(mobj.group(1)))
261
262 video_ids.extend(ids_in_page)
263
264 # A little optimization - if current page is not
265 # "full", ie. does not contain PAGE_SIZE video ids then
266 # we can assume that this page is the last one - there
267 # are no more ids on further pages - no need to query
268 # again.
269
270 if len(ids_in_page) < self._PAGE_SIZE:
271 break
272
273 pagenum += 1
274
275 urls = ['http://blip.tv/%s' % video_id for video_id in video_ids]
276 url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
277 return self.playlist_result(
278 url_entries, playlist_title=title, playlist_id=username)