]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/ninenow.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / ninenow.py
CommitLineData
38e0f16a 1from .common import InfoExtractor
38e0f16a 2from ..utils import (
832f9d52 3 ExtractorError,
38e0f16a 4 float_or_none,
e897bd82 5 int_or_none,
832f9d52 6 smuggle_url,
03c86279 7 str_or_none,
8 try_get,
9 unified_strdate,
10 unified_timestamp,
38e0f16a
RA
11)
12
13
14class NineNowIE(InfoExtractor):
15 IE_NAME = '9now.com.au'
16 _VALID_URL = r'https?://(?:www\.)?9now\.com\.au/(?:[^/]+/){2}(?P<id>[^/?#]+)'
832f9d52 17 _GEO_COUNTRIES = ['AU']
38e0f16a
RA
18 _TESTS = [{
19 # clip
20 'url': 'https://www.9now.com.au/afl-footy-show/2016/clip-ciql02091000g0hp5oktrnytc',
21 'md5': '17cf47d63ec9323e562c9957a968b565',
22 'info_dict': {
23 'id': '16801',
24 'ext': 'mp4',
25 'title': 'St. Kilda\'s Joey Montagna on the potential for a player\'s strike',
26 'description': 'Is a boycott of the NAB Cup "on the table"?',
27 'uploader_id': '4460760524001',
28 'upload_date': '20160713',
29 'timestamp': 1468421266,
30 },
31 'skip': 'Only available in Australia',
32 }, {
33 # episode
34 'url': 'https://www.9now.com.au/afl-footy-show/2016/episode-19',
35 'only_matching': True,
36 }, {
37 # DRM protected
342f0c36 38 'url': 'https://www.9now.com.au/andrew-marrs-history-of-the-world/season-1/episode-1',
38e0f16a 39 'only_matching': True,
03c86279 40 }, {
41 # episode of series
42 'url': 'https://www.9now.com.au/lego-masters/season-3/episode-3',
43 'info_dict': {
44 'id': '6249614030001',
45 'title': 'Episode 3',
46 'ext': 'mp4',
47 'season_number': 3,
48 'episode_number': 3,
49 'description': 'In the first elimination of the competition, teams will have 10 hours to build a world inside a snow globe.',
50 'uploader_id': '4460760524001',
51 'timestamp': 1619002200,
52 'upload_date': '20210421',
53 },
54 'expected_warnings': ['Ignoring subtitle tracks'],
6148833f 55 'params': {
03c86279 56 'skip_download': True,
add96eb9 57 },
38e0f16a
RA
58 }]
59 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/4460760524001/default_default/index.html?videoId=%s'
60
61 def _real_extract(self, url):
62 display_id = self._match_id(url)
63 webpage = self._download_webpage(url, display_id)
64 page_data = self._parse_json(self._search_regex(
65 r'window\.__data\s*=\s*({.*?});', webpage,
6fd26a7d
S
66 'page data', default='{}'), display_id, fatal=False)
67 if not page_data:
68 page_data = self._parse_json(self._parse_json(self._search_regex(
69 r'window\.__data\s*=\s*JSON\.parse\s*\(\s*(".+?")\s*\)\s*;',
70 webpage, 'page data'), display_id), display_id)
56c0ead4
S
71
72 for kind in ('episode', 'clip'):
73 current_key = page_data.get(kind, {}).get(
add96eb9 74 f'current{kind.capitalize()}Key')
56c0ead4
S
75 if not current_key:
76 continue
add96eb9 77 cache = page_data.get(kind, {}).get(f'{kind}Cache', {})
56c0ead4
S
78 if not cache:
79 continue
03c86279 80 common_data = {
add96eb9 81 'episode': (cache.get(current_key) or next(iter(cache.values())))[kind],
82 'season': (cache.get(current_key) or next(iter(cache.values()))).get('season', None),
03c86279 83 }
56c0ead4
S
84 break
85 else:
86 raise ExtractorError('Unable to find video data')
87
03c86279 88 if not self.get_param('allow_unplayable_formats') and try_get(common_data, lambda x: x['episode']['video']['drm'], bool):
89 self.report_drm(display_id)
90 brightcove_id = try_get(
add96eb9 91 common_data, lambda x: x['episode']['video']['brightcoveId'], str) or 'ref:{}'.format(common_data['episode']['video']['referenceId'])
03c86279 92 video_id = str_or_none(try_get(common_data, lambda x: x['episode']['video']['id'])) or brightcove_id
38e0f16a 93
add96eb9 94 title = try_get(common_data, lambda x: x['episode']['name'], str)
03c86279 95 season_number = try_get(common_data, lambda x: x['season']['seasonNumber'], int)
96 episode_number = try_get(common_data, lambda x: x['episode']['episodeNumber'], int)
add96eb9 97 timestamp = unified_timestamp(try_get(common_data, lambda x: x['episode']['airDate'], str))
98 release_date = unified_strdate(try_get(common_data, lambda x: x['episode']['availability'], str))
03c86279 99 thumbnails_data = try_get(common_data, lambda x: x['episode']['image']['sizes'], dict) or {}
38e0f16a
RA
100 thumbnails = [{
101 'id': thumbnail_id,
102 'url': thumbnail_url,
03c86279 103 'width': int_or_none(thumbnail_id[1:]),
104 } for thumbnail_id, thumbnail_url in thumbnails_data.items()]
38e0f16a
RA
105
106 return {
107 '_type': 'url_transparent',
832f9d52
S
108 'url': smuggle_url(
109 self.BRIGHTCOVE_URL_TEMPLATE % brightcove_id,
110 {'geo_countries': self._GEO_COUNTRIES}),
38e0f16a
RA
111 'id': video_id,
112 'title': title,
add96eb9 113 'description': try_get(common_data, lambda x: x['episode']['description'], str),
03c86279 114 'duration': float_or_none(try_get(common_data, lambda x: x['episode']['video']['duration'], float), 1000),
38e0f16a
RA
115 'thumbnails': thumbnails,
116 'ie_key': 'BrightcoveNew',
03c86279 117 'season_number': season_number,
118 'episode_number': episode_number,
119 'timestamp': timestamp,
120 'release_date': release_date,
38e0f16a 121 }