[youtube] Skip unsupported adaptive stream type (#18804)
[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/nv60p12f',
22             'md5': 'd9c17bbe9c3386344f9cfd32fad8d235',
23             'info_dict': {
24                 'id': 'nv60p12f',
25                 'ext': 'mp4',
26                 'title': 'فن الحياة  - الحلقة 1',
27                 'description': 'احدث برامج الداعية الاسلامي " مصطفي حسني " فى رمضان 2016علي النهار نور',
28                 'timestamp': 1465231790,
29                 'upload_date': '20160606',
30             }
31         },
32         {
33             # Original source: http://www.uscourts.gov/cameras-courts/state-washington-vs-donald-j-trump-et-al
34             'url': 'https://player.piksel.com/v/v80kqp41',
35             'md5': '753ddcd8cc8e4fa2dda4b7be0e77744d',
36             'info_dict': {
37                 'id': 'v80kqp41',
38                 'ext': 'mp4',
39                 'title': 'WAW- State of Washington vs. Donald J. Trump, et al',
40                 '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.',
41                 'timestamp': 1486171129,
42                 'upload_date': '20170204',
43             }
44         }
45     ]
46
47     @staticmethod
48     def _extract_url(webpage):
49         mobj = re.search(
50             r'<iframe[^>]+src=["\'](?P<url>(?:https?:)?//player\.piksel\.com/v/[a-z0-9]+)',
51             webpage)
52         if mobj:
53             return mobj.group('url')
54
55     def _real_extract(self, url):
56         video_id = self._match_id(url)
57         webpage = self._download_webpage(url, video_id)
58         app_token = self._search_regex([
59             r'clientAPI\s*:\s*"([^"]+)"',
60             r'data-de-api-key\s*=\s*"([^"]+)"'
61         ], webpage, 'app token')
62         response = self._download_json(
63             'http://player.piksel.com/ws/ws_program/api/%s/mode/json/apiv/5' % app_token,
64             video_id, query={
65                 'v': video_id
66             })['response']
67         failure = response.get('failure')
68         if failure:
69             raise ExtractorError(response['failure']['reason'], expected=True)
70         video_data = response['WsProgramResponse']['program']['asset']
71         title = video_data['title']
72
73         formats = []
74
75         m3u8_url = dict_get(video_data, [
76             'm3u8iPadURL',
77             'ipadM3u8Url',
78             'm3u8AndroidURL',
79             'm3u8iPhoneURL',
80             'iphoneM3u8Url'])
81         if m3u8_url:
82             formats.extend(self._extract_m3u8_formats(
83                 m3u8_url, video_id, 'mp4', 'm3u8_native',
84                 m3u8_id='hls', fatal=False))
85
86         asset_type = dict_get(video_data, ['assetType', 'asset_type'])
87         for asset_file in video_data.get('assetFiles', []):
88             # TODO: extract rtmp formats
89             http_url = asset_file.get('http_url')
90             if not http_url:
91                 continue
92             tbr = None
93             vbr = int_or_none(asset_file.get('videoBitrate'), 1024)
94             abr = int_or_none(asset_file.get('audioBitrate'), 1024)
95             if asset_type == 'video':
96                 tbr = vbr + abr
97             elif asset_type == 'audio':
98                 tbr = abr
99
100             format_id = ['http']
101             if tbr:
102                 format_id.append(compat_str(tbr))
103
104             formats.append({
105                 'format_id': '-'.join(format_id),
106                 'url': unescapeHTML(http_url),
107                 'vbr': vbr,
108                 'abr': abr,
109                 'width': int_or_none(asset_file.get('videoWidth')),
110                 'height': int_or_none(asset_file.get('videoHeight')),
111                 'filesize': int_or_none(asset_file.get('filesize')),
112                 'tbr': tbr,
113             })
114         self._sort_formats(formats)
115
116         return {
117             'id': video_id,
118             'title': title,
119             'description': video_data.get('description'),
120             'thumbnail': video_data.get('thumbnailUrl'),
121             'timestamp': parse_iso8601(video_data.get('dateadd')),
122             'formats': formats,
123         }