[youtube] Quick extraction tempfix (closes #22367, closes #22163)
[youtube-dl] / youtube_dl / extractor / piksel.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     ExtractorError,
10     dict_get,
11     int_or_none,
12     unescapeHTML,
13     parse_iso8601,
14 )
15
16
17 class PikselIE(InfoExtractor):
18     _VALID_URL = r'https?://player\.piksel\.com/v/(?P<id>[a-z0-9]+)'
19     _TESTS = [
20         {
21             'url': 'http://player.piksel.com/v/ums2867l',
22             'md5': '34e34c8d89dc2559976a6079db531e85',
23             'info_dict': {
24                 'id': 'ums2867l',
25                 'ext': 'mp4',
26                 'title': 'GX-005 with Caption',
27                 'timestamp': 1481335659,
28                 'upload_date': '20161210'
29             }
30         },
31         {
32             # Original source: http://www.uscourts.gov/cameras-courts/state-washington-vs-donald-j-trump-et-al
33             'url': 'https://player.piksel.com/v/v80kqp41',
34             'md5': '753ddcd8cc8e4fa2dda4b7be0e77744d',
35             'info_dict': {
36                 'id': 'v80kqp41',
37                 'ext': 'mp4',
38                 'title': 'WAW- State of Washington vs. Donald J. Trump, et al',
39                 'description': 'State of Washington vs. Donald J. Trump, et al, Case Number 17-CV-00141-JLR, TRO Hearing, Civil Rights Case, 02/3/2017, 1:00 PM (PST), Seattle Federal Courthouse, Seattle, WA, Judge James L. Robart presiding.',
40                 'timestamp': 1486171129,
41                 'upload_date': '20170204'
42             }
43         }
44     ]
45
46     @staticmethod
47     def _extract_url(webpage):
48         mobj = re.search(
49             r'<iframe[^>]+src=["\'](?P<url>(?:https?:)?//player\.piksel\.com/v/[a-z0-9]+)',
50             webpage)
51         if mobj:
52             return mobj.group('url')
53
54     def _real_extract(self, url):
55         video_id = self._match_id(url)
56         webpage = self._download_webpage(url, video_id)
57         app_token = self._search_regex([
58             r'clientAPI\s*:\s*"([^"]+)"',
59             r'data-de-api-key\s*=\s*"([^"]+)"'
60         ], webpage, 'app token')
61         response = self._download_json(
62             'http://player.piksel.com/ws/ws_program/api/%s/mode/json/apiv/5' % app_token,
63             video_id, query={
64                 'v': video_id
65             })['response']
66         failure = response.get('failure')
67         if failure:
68             raise ExtractorError(response['failure']['reason'], expected=True)
69         video_data = response['WsProgramResponse']['program']['asset']
70         title = video_data['title']
71
72         formats = []
73
74         m3u8_url = dict_get(video_data, [
75             'm3u8iPadURL',
76             'ipadM3u8Url',
77             'm3u8AndroidURL',
78             'm3u8iPhoneURL',
79             'iphoneM3u8Url'])
80         if m3u8_url:
81             formats.extend(self._extract_m3u8_formats(
82                 m3u8_url, video_id, 'mp4', 'm3u8_native',
83                 m3u8_id='hls', fatal=False))
84
85         asset_type = dict_get(video_data, ['assetType', 'asset_type'])
86         for asset_file in video_data.get('assetFiles', []):
87             # TODO: extract rtmp formats
88             http_url = asset_file.get('http_url')
89             if not http_url:
90                 continue
91             tbr = None
92             vbr = int_or_none(asset_file.get('videoBitrate'), 1024)
93             abr = int_or_none(asset_file.get('audioBitrate'), 1024)
94             if asset_type == 'video':
95                 tbr = vbr + abr
96             elif asset_type == 'audio':
97                 tbr = abr
98
99             format_id = ['http']
100             if tbr:
101                 format_id.append(compat_str(tbr))
102
103             formats.append({
104                 'format_id': '-'.join(format_id),
105                 'url': unescapeHTML(http_url),
106                 'vbr': vbr,
107                 'abr': abr,
108                 'width': int_or_none(asset_file.get('videoWidth')),
109                 'height': int_or_none(asset_file.get('videoHeight')),
110                 'filesize': int_or_none(asset_file.get('filesize')),
111                 'tbr': tbr,
112             })
113         self._sort_formats(formats)
114
115         subtitles = {}
116         for caption in video_data.get('captions', []):
117             caption_url = caption.get('url')
118             if caption_url:
119                 subtitles.setdefault(caption.get('locale', 'en'), []).append({
120                     'url': caption_url})
121
122         return {
123             'id': video_id,
124             'title': title,
125             'description': video_data.get('description'),
126             'thumbnail': video_data.get('thumbnailUrl'),
127             'timestamp': parse_iso8601(video_data.get('dateadd')),
128             'formats': formats,
129             'subtitles': subtitles,
130         }