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