[youtube] Fix extraction.
[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/(?:refid/[^/]+/prefid/)?(?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             # https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2019240/
46             'url': 'http://player.piksel.com/v/refid/nhkworld/prefid/nw_vod_v_en_2019_240_20190823233000_02_1566873477',
47             'only_matching': True,
48         }
49     ]
50
51     @staticmethod
52     def _extract_url(webpage):
53         mobj = re.search(
54             r'<iframe[^>]+src=["\'](?P<url>(?:https?:)?//player\.piksel\.com/v/[a-z0-9]+)',
55             webpage)
56         if mobj:
57             return mobj.group('url')
58
59     def _real_extract(self, url):
60         display_id = self._match_id(url)
61         webpage = self._download_webpage(url, display_id)
62         video_id = self._search_regex(
63             r'data-de-program-uuid=[\'"]([a-z0-9]+)',
64             webpage, 'program uuid', default=display_id)
65         app_token = self._search_regex([
66             r'clientAPI\s*:\s*"([^"]+)"',
67             r'data-de-api-key\s*=\s*"([^"]+)"'
68         ], webpage, 'app token')
69         response = self._download_json(
70             'http://player.piksel.com/ws/ws_program/api/%s/mode/json/apiv/5' % app_token,
71             video_id, query={
72                 'v': video_id
73             })['response']
74         failure = response.get('failure')
75         if failure:
76             raise ExtractorError(response['failure']['reason'], expected=True)
77         video_data = response['WsProgramResponse']['program']['asset']
78         title = video_data['title']
79
80         formats = []
81
82         m3u8_url = dict_get(video_data, [
83             'm3u8iPadURL',
84             'ipadM3u8Url',
85             'm3u8AndroidURL',
86             'm3u8iPhoneURL',
87             'iphoneM3u8Url'])
88         if m3u8_url:
89             formats.extend(self._extract_m3u8_formats(
90                 m3u8_url, video_id, 'mp4', 'm3u8_native',
91                 m3u8_id='hls', fatal=False))
92
93         asset_type = dict_get(video_data, ['assetType', 'asset_type'])
94         for asset_file in video_data.get('assetFiles', []):
95             # TODO: extract rtmp formats
96             http_url = asset_file.get('http_url')
97             if not http_url:
98                 continue
99             tbr = None
100             vbr = int_or_none(asset_file.get('videoBitrate'), 1024)
101             abr = int_or_none(asset_file.get('audioBitrate'), 1024)
102             if asset_type == 'video':
103                 tbr = vbr + abr
104             elif asset_type == 'audio':
105                 tbr = abr
106
107             format_id = ['http']
108             if tbr:
109                 format_id.append(compat_str(tbr))
110
111             formats.append({
112                 'format_id': '-'.join(format_id),
113                 'url': unescapeHTML(http_url),
114                 'vbr': vbr,
115                 'abr': abr,
116                 'width': int_or_none(asset_file.get('videoWidth')),
117                 'height': int_or_none(asset_file.get('videoHeight')),
118                 'filesize': int_or_none(asset_file.get('filesize')),
119                 'tbr': tbr,
120             })
121         self._sort_formats(formats)
122
123         subtitles = {}
124         for caption in video_data.get('captions', []):
125             caption_url = caption.get('url')
126             if caption_url:
127                 subtitles.setdefault(caption.get('locale', 'en'), []).append({
128                     'url': caption_url})
129
130         return {
131             'id': video_id,
132             'title': title,
133             'description': video_data.get('description'),
134             'thumbnail': video_data.get('thumbnailUrl'),
135             'timestamp': parse_iso8601(video_data.get('dateadd')),
136             'formats': formats,
137             'subtitles': subtitles,
138         }