6 from .common import InfoExtractor
12 compat_urllib_parse_urlparse,
13 compat_urllib_request,
20 class BlipTVIE(InfoExtractor):
21 """Information extractor for blip.tv"""
23 _VALID_URL = r'^(?:https?://)?(?:www\.)?blip\.tv/((.+/)|(play/)|(api\.swf#))(.+)$'
24 _URL_EXT = r'^.*\.([a-z0-9]+)$'
27 u'url': u'http://blip.tv/cbr/cbr-exclusive-gotham-city-imposters-bats-vs-jokerz-short-3-5796352',
28 u'file': u'5779306.m4v',
29 u'md5': u'80baf1ec5c3d2019037c1c707d676b9f',
31 u"upload_date": u"20111205",
32 u"description": u"md5:9bc31f227219cde65e47eeec8d2dc596",
33 u"uploader": u"Comic Book Resources - CBR TV",
34 u"title": u"CBR EXCLUSIVE: \"Gotham City Imposters\" Bats VS Jokerz Short 3"
38 def report_direct_download(self, title):
39 """Report information extraction."""
40 self.to_screen(u'%s: Direct download detected' % title)
42 def _real_extract(self, url):
43 mobj = re.match(self._VALID_URL, url)
45 raise ExtractorError(u'Invalid URL: %s' % url)
47 # See https://github.com/rg3/youtube-dl/issues/857
48 api_mobj = re.match(r'http://a\.blip\.tv/api\.swf#(?P<video_id>[\d\w]+)', url)
49 if api_mobj is not None:
50 url = 'http://blip.tv/play/g_%s' % api_mobj.group('video_id')
51 urlp = compat_urllib_parse_urlparse(url)
52 if urlp.path.startswith('/play/'):
53 response = self._request_webpage(url, None, False)
54 redirecturl = response.geturl()
55 rurlp = compat_urllib_parse_urlparse(redirecturl)
56 file_id = compat_parse_qs(rurlp.fragment)['file'][0].rpartition('/')[2]
57 url = 'http://blip.tv/a/a-' + file_id
58 return self._real_extract(url)
64 json_url = url + cchar + 'skin=json&version=2&no_wrap=1'
65 request = compat_urllib_request.Request(json_url)
66 request.add_header('User-Agent', 'iTunes/10.6.1')
67 self.report_extraction(mobj.group(1))
68 urlh = self._request_webpage(request, None, False,
69 u'unable to download video info webpage')
72 json_code_bytes = urlh.read()
73 json_code = json_code_bytes.decode('utf-8')
74 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
75 raise ExtractorError(u'Unable to read video info webpage: %s' % compat_str(err))
78 json_data = json.loads(json_code)
79 if 'Post' in json_data:
80 data = json_data['Post']
84 upload_date = datetime.datetime.strptime(data['datestamp'], '%m-%d-%y %H:%M%p').strftime('%Y%m%d')
85 if 'additionalMedia' in data:
86 formats = sorted(data['additionalMedia'], key=lambda f: int(f['media_height']))
87 best_format = formats[-1]
88 video_url = best_format['url']
90 video_url = data['media']['url']
91 umobj = re.match(self._URL_EXT, video_url)
93 raise ValueError('Can not determine filename extension')
97 'id': compat_str(data['item_id']),
99 'uploader': data['display_name'],
100 'upload_date': upload_date,
101 'title': data['title'],
103 'format': data['media']['mimeType'],
104 'thumbnail': data['thumbnailUrl'],
105 'description': data['description'],
106 'player_url': data['embedUrl'],
107 'user_agent': 'iTunes/10.6.1',
109 except (ValueError, KeyError) as err:
110 raise ExtractorError(u'Unable to parse video information: %s' % repr(err))
113 class BlipTVUserIE(InfoExtractor):
114 """Information Extractor for blip.tv users."""
116 _VALID_URL = r'(?:(?:(?:https?://)?(?:\w+\.)?blip\.tv/)|bliptvuser:)([^/]+)/*$'
118 IE_NAME = u'blip.tv:user'
120 def _real_extract(self, url):
122 mobj = re.match(self._VALID_URL, url)
124 raise ExtractorError(u'Invalid URL: %s' % url)
126 username = mobj.group(1)
128 page_base = 'http://m.blip.tv/pr/show_get_full_episode_list?users_id=%s&lite=0&esi=1'
130 page = self._download_webpage(url, username, u'Downloading user page')
131 mobj = re.search(r'data-users-id="([^"]+)"', page)
132 page_base = page_base % mobj.group(1)
135 # Download video ids using BlipTV Ajax calls. Result size per
136 # query is limited (currently to 12 videos) so we need to query
137 # page by page until there are no video ids - it means we got
144 url = page_base + "&page=" + str(pagenum)
145 page = self._download_webpage(url, username,
146 u'Downloading video ids from page %d' % pagenum)
148 # Extract video identifiers
151 for mobj in re.finditer(r'href="/([^"]+)"', page):
152 if mobj.group(1) not in ids_in_page:
153 ids_in_page.append(unescapeHTML(mobj.group(1)))
155 video_ids.extend(ids_in_page)
157 # A little optimization - if current page is not
158 # "full", ie. does not contain PAGE_SIZE video ids then
159 # we can assume that this page is the last one - there
160 # are no more ids on further pages - no need to query
163 if len(ids_in_page) < self._PAGE_SIZE:
168 urls = [u'http://blip.tv/%s' % video_id for video_id in video_ids]
169 url_entries = [self.url_result(vurl, 'BlipTV') for vurl in urls]
170 return [self.playlist_result(url_entries, playlist_title = username)]