]>
Commit | Line | Data |
---|---|---|
bca4e930 PH |
1 | from __future__ import unicode_literals |
2 | ||
f5884801 | 3 | import datetime |
f5884801 | 4 | import re |
f5884801 PH |
5 | |
6 | from .common import InfoExtractor | |
b4bcffef | 7 | from .subtitles import SubtitlesInfoExtractor |
f5884801 | 8 | from ..utils import ( |
f5884801 | 9 | compat_str, |
f5884801 PH |
10 | compat_urllib_request, |
11 | ||
f5884801 PH |
12 | unescapeHTML, |
13 | ) | |
14 | ||
15 | ||
b4bcffef | 16 | class BlipTVIE(SubtitlesInfoExtractor): |
f5884801 PH |
17 | """Information extractor for blip.tv""" |
18 | ||
b4bcffef | 19 | _VALID_URL = r'https?://(?:\w+\.)?blip\.tv/((.+/)|(play/)|(api\.swf#))(?P<presumptive_id>.+)$' |
aff24732 | 20 | |
b4bcffef | 21 | _TESTS = [{ |
bca4e930 | 22 | 'url': 'http://blip.tv/cbr/cbr-exclusive-gotham-city-imposters-bats-vs-jokerz-short-3-5796352', |
cefcb9fd | 23 | 'md5': 'c6934ad0b6acf2bd920720ec888eb812', |
bca4e930 | 24 | 'info_dict': { |
b4bcffef PH |
25 | 'id': '5779306', |
26 | 'ext': 'mov', | |
bca4e930 PH |
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', | |
6f5ac90c | 31 | } |
b4bcffef PH |
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 | }] | |
f5884801 PH |
46 | |
47 | def _real_extract(self, url): | |
48 | mobj = re.match(self._VALID_URL, url) | |
b4bcffef | 49 | presumptive_id = mobj.group('presumptive_id') |
f5884801 PH |
50 | |
51 | # See https://github.com/rg3/youtube-dl/issues/857 | |
b4bcffef | 52 | embed_mobj = re.match(r'https?://(?:\w+\.)?blip\.tv/(?:play/|api\.swf#)([a-zA-Z0-9]+)', url) |
c0f9969b | 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)) | |
b4bcffef PH |
56 | video_id = self._search_regex( |
57 | r'data-episode-id="([0-9]+)', info_page, 'video_id') | |
aff24732 | 58 | return self.url_result('http://blip.tv/a/a-' + video_id, 'BlipTV') |
b4bcffef PH |
59 | |
60 | cchar = '&' if '?' in url else '?' | |
f5884801 PH |
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') | |
8e93b9b9 | 64 | |
b4bcffef PH |
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 | |
531147dd | 88 | formats.append({ |
b4bcffef PH |
89 | 'url': f['url'], |
90 | 'format_id': f['role'], | |
91 | 'width': int(f['media_width']), | |
92 | 'height': int(f['media_height']), | |
531147dd | 93 | }) |
b4bcffef PH |
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 | } | |
466617f5 | 119 | |
b4bcffef PH |
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) | |
f5884801 PH |
126 | |
127 | ||
128 | class BlipTVUserIE(InfoExtractor): | |
f5884801 PH |
129 | _VALID_URL = r'(?:(?:(?:https?://)?(?:\w+\.)?blip\.tv/)|bliptvuser:)([^/]+)/*$' |
130 | _PAGE_SIZE = 12 | |
bca4e930 | 131 | IE_NAME = 'blip.tv:user' |
f5884801 PH |
132 | |
133 | def _real_extract(self, url): | |
f5884801 | 134 | mobj = re.match(self._VALID_URL, url) |
f5884801 PH |
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 | ||
bca4e930 | 139 | page = self._download_webpage(url, username, 'Downloading user page') |
f5884801 PH |
140 | mobj = re.search(r'data-users-id="([^"]+)"', page) |
141 | page_base = page_base % mobj.group(1) | |
142 | ||
f5884801 PH |
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) | |
b4bcffef PH |
153 | page = self._download_webpage( |
154 | url, username, 'Downloading video ids from page %d' % pagenum) | |
f5884801 PH |
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 | ||
bca4e930 | 176 | urls = ['http://blip.tv/%s' % video_id for video_id in video_ids] |
20c3893f | 177 | url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls] |
b4bcffef | 178 | return [self.playlist_result(url_entries, playlist_title=username)] |