[fox] Remove unused imports
[ytdl] / youtube_dl / extractor / fox.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import uuid
6
7 from .adobepass import AdobePassIE
8 from ..compat import (
9     compat_str,
10     compat_urllib_parse_unquote,
11 )
12 from ..utils import (
13     int_or_none,
14     parse_age_limit,
15     parse_duration,
16     try_get,
17     unified_timestamp,
18 )
19
20
21 class FOXIE(AdobePassIE):
22     _VALID_URL = r'https?://(?:www\.)?fox\.com/watch/(?P<id>[\da-fA-F]+)'
23     _TESTS = [{
24         # clip
25         'url': 'https://www.fox.com/watch/4b765a60490325103ea69888fb2bd4e8/',
26         'md5': 'ebd296fcc41dd4b19f8115d8461a3165',
27         'info_dict': {
28             'id': '4b765a60490325103ea69888fb2bd4e8',
29             'ext': 'mp4',
30             'title': 'Aftermath: Bruce Wayne Develops Into The Dark Knight',
31             'description': 'md5:549cd9c70d413adb32ce2a779b53b486',
32             'duration': 102,
33             'timestamp': 1504291893,
34             'upload_date': '20170901',
35             'creator': 'FOX',
36             'series': 'Gotham',
37             'age_limit': 14,
38         },
39         'params': {
40             'skip_download': True,
41         },
42     }, {
43         # episode, geo-restricted
44         'url': 'https://www.fox.com/watch/087036ca7f33c8eb79b08152b4dd75c1/',
45         'only_matching': True,
46     }, {
47         # episode, geo-restricted, tv provided required
48         'url': 'https://www.fox.com/watch/30056b295fb57f7452aeeb4920bc3024/',
49         'only_matching': True,
50     }]
51     _HOME_PAGE_URL = 'https://www.fox.com/'
52     _API_KEY = 'abdcbed02c124d393b39e818a4312055'
53     _access_token = None
54
55     def _call_api(self, path, video_id, data=None):
56         headers = {
57             'X-Api-Key': self._API_KEY,
58         }
59         if self._access_token:
60             headers['Authorization'] = 'Bearer ' + self._access_token
61         return self._download_json(
62             'https://api2.fox.com/v2.0/' + path,
63             video_id, data=data, headers=headers)
64
65     def _real_initialize(self):
66         if not self._access_token:
67             mvpd_auth = self._get_cookies(self._HOME_PAGE_URL).get('mvpd-auth')
68             if mvpd_auth:
69                 self._access_token = (self._parse_json(compat_urllib_parse_unquote(
70                     mvpd_auth.value), None, fatal=False) or {}).get('accessToken')
71             if not self._access_token:
72                 self._access_token = self._call_api(
73                     'login', None, json.dumps({
74                         'deviceId': compat_str(uuid.uuid4()),
75                     }).encode())['accessToken']
76
77     def _real_extract(self, url):
78         video_id = self._match_id(url)
79
80         video = self._call_api('vodplayer/' + video_id, video_id)
81
82         title = video['name']
83         release_url = video['url']
84         m3u8_url = self._download_json(release_url, video_id)['playURL']
85         formats = self._extract_m3u8_formats(
86             m3u8_url, video_id, 'mp4',
87             entry_protocol='m3u8_native', m3u8_id='hls')
88         self._sort_formats(formats)
89
90         data = try_get(
91             video, lambda x: x['trackingData']['properties'], dict) or {}
92
93         duration = int_or_none(video.get('durationInSeconds')) or int_or_none(
94             video.get('duration')) or parse_duration(video.get('duration'))
95         timestamp = unified_timestamp(video.get('datePublished'))
96         creator = data.get('brand') or data.get('network') or video.get('network')
97         series = video.get('seriesName') or data.get(
98             'seriesName') or data.get('show')
99
100         subtitles = {}
101         for doc_rel in video.get('documentReleases', []):
102             rel_url = doc_rel.get('url')
103             if not url or doc_rel.get('format') != 'SCC':
104                 continue
105             subtitles['en'] = [{
106                 'url': rel_url,
107                 'ext': 'scc',
108             }]
109             break
110
111         return {
112             'id': video_id,
113             'title': title,
114             'formats': formats,
115             'description': video.get('description'),
116             'duration': duration,
117             'timestamp': timestamp,
118             'age_limit': parse_age_limit(video.get('contentRating')),
119             'creator': creator,
120             'series': series,
121             'season_number': int_or_none(video.get('seasonNumber')),
122             'episode': video.get('name'),
123             'episode_number': int_or_none(video.get('episodeNumber')),
124             'release_year': int_or_none(video.get('releaseYear')),
125             'subtitles': subtitles,
126         }