]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bliptv.py
a26001bb37401d8f0b92dcc5a4ef130be31b3e5e
[youtubedl] / youtube_dl / extractor / bliptv.py
1 from __future__ import unicode_literals
2
3 import datetime
4 import re
5
6 from .common import InfoExtractor
7 from .subtitles import SubtitlesInfoExtractor
8 from ..utils import (
9 compat_str,
10 compat_urllib_request,
11
12 unescapeHTML,
13 )
14
15
16 class BlipTVIE(SubtitlesInfoExtractor):
17 """Information extractor for blip.tv"""
18
19 _VALID_URL = r'https?://(?:\w+\.)?blip\.tv/((.+/)|(play/)|(api\.swf#))(?P<presumptive_id>.+)$'
20
21 _TESTS = [{
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 'upload_date': '20111205',
28 'description': 'md5:9bc31f227219cde65e47eeec8d2dc596',
29 'uploader': 'Comic Book Resources - CBR TV',
30 'title': 'CBR EXCLUSIVE: "Gotham City Imposters" Bats VS Jokerz Short 3',
31 }
32 }, {
33 # https://github.com/rg3/youtube-dl/pull/2274
34 'note': 'Video with subtitles',
35 'url': 'http://blip.tv/play/h6Uag5OEVgI.html',
36 'md5': '309f9d25b820b086ca163ffac8031806',
37 'info_dict': {
38 'id': '6586561',
39 'ext': 'mp4',
40 'uploader': 'Red vs. Blue',
41 'description': 'One-Zero-One',
42 'upload_date': '20130614',
43 'title': 'Red vs. Blue Season 11 Episode 1',
44 }
45 }]
46
47 def _real_extract(self, url):
48 mobj = re.match(self._VALID_URL, url)
49 presumptive_id = mobj.group('presumptive_id')
50
51 # See https://github.com/rg3/youtube-dl/issues/857
52 embed_mobj = re.match(r'https?://(?:\w+\.)?blip\.tv/(?:play/|api\.swf#)([a-zA-Z0-9]+)', url)
53 if embed_mobj:
54 info_url = 'http://blip.tv/play/%s.x?p=1' % embed_mobj.group(1)
55 info_page = self._download_webpage(info_url, embed_mobj.group(1))
56 video_id = self._search_regex(
57 r'data-episode-id="([0-9]+)', info_page, 'video_id')
58 return self.url_result('http://blip.tv/a/a-' + video_id, 'BlipTV')
59
60 cchar = '&' if '?' in url else '?'
61 json_url = url + cchar + 'skin=json&version=2&no_wrap=1'
62 request = compat_urllib_request.Request(json_url)
63 request.add_header('User-Agent', 'iTunes/10.6.1')
64
65 json_data = self._download_json(request, video_id=presumptive_id)
66
67 if 'Post' in json_data:
68 data = json_data['Post']
69 else:
70 data = json_data
71
72 video_id = compat_str(data['item_id'])
73 upload_date = datetime.datetime.strptime(data['datestamp'], '%m-%d-%y %H:%M%p').strftime('%Y%m%d')
74 subtitles = {}
75 formats = []
76 if 'additionalMedia' in data:
77 for f in data['additionalMedia']:
78 if f.get('file_type_srt') == 1:
79 LANGS = {
80 'english': 'en',
81 }
82 lang = f['role'].rpartition('-')[-1].strip().lower()
83 langcode = LANGS.get(lang, lang)
84 subtitles[langcode] = f['url']
85 continue
86 if not int(f['media_width']): # filter m3u8
87 continue
88 formats.append({
89 'url': f['url'],
90 'format_id': f['role'],
91 'width': int(f['media_width']),
92 'height': int(f['media_height']),
93 })
94 else:
95 formats.append({
96 'url': data['media']['url'],
97 'width': int(data['media']['width']),
98 'height': int(data['media']['height']),
99 })
100 self._sort_formats(formats)
101
102 # subtitles
103 video_subtitles = self.extract_subtitles(video_id, subtitles)
104 if self._downloader.params.get('listsubtitles', False):
105 self._list_available_subtitles(video_id, subtitles)
106 return
107
108 return {
109 'id': video_id,
110 'uploader': data['display_name'],
111 'upload_date': upload_date,
112 'title': data['title'],
113 'thumbnail': data['thumbnailUrl'],
114 'description': data['description'],
115 'user_agent': 'iTunes/10.6.1',
116 'formats': formats,
117 'subtitles': video_subtitles,
118 }
119
120 def _download_subtitle_url(self, sub_lang, url):
121 # For some weird reason, blip.tv serves a video instead of subtitles
122 # when we request with a common UA
123 req = compat_urllib_request.Request(url)
124 req.add_header('Youtubedl-user-agent', 'youtube-dl')
125 return self._download_webpage(req, None, note=False)
126
127
128 class BlipTVUserIE(InfoExtractor):
129 _VALID_URL = r'(?:(?:(?:https?://)?(?:\w+\.)?blip\.tv/)|bliptvuser:)([^/]+)/*$'
130 _PAGE_SIZE = 12
131 IE_NAME = 'blip.tv:user'
132
133 def _real_extract(self, url):
134 mobj = re.match(self._VALID_URL, url)
135 username = mobj.group(1)
136
137 page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
138
139 page = self._download_webpage(url, username, 'Downloading user page')
140 mobj = re.search(r'data-users-id="([^"]+)"', page)
141 page_base = page_base % mobj.group(1)
142
143 # Download video ids using BlipTV Ajax calls. Result size per
144 # query is limited (currently to 12 videos) so we need to query
145 # page by page until there are no video ids - it means we got
146 # all of them.
147
148 video_ids = []
149 pagenum = 1
150
151 while True:
152 url = page_base + "&page=" + str(pagenum)
153 page = self._download_webpage(
154 url, username, 'Downloading video ids from page %d' % pagenum)
155
156 # Extract video identifiers
157 ids_in_page = []
158
159 for mobj in re.finditer(r'href="/([^"]+)"', page):
160 if mobj.group(1) not in ids_in_page:
161 ids_in_page.append(unescapeHTML(mobj.group(1)))
162
163 video_ids.extend(ids_in_page)
164
165 # A little optimization - if current page is not
166 # "full", ie. does not contain PAGE_SIZE video ids then
167 # we can assume that this page is the last one - there
168 # are no more ids on further pages - no need to query
169 # again.
170
171 if len(ids_in_page) < self._PAGE_SIZE:
172 break
173
174 pagenum += 1
175
176 urls = ['http://blip.tv/%s' % video_id for video_id in video_ids]
177 url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
178 return [self.playlist_result(url_entries, playlist_title=username)]