]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/hrti.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / hrti.py
CommitLineData
6b03e1e2
AT
1import json
2
3from .common import InfoExtractor
3d2623a8 4from ..networking import Request
5from ..networking.exceptions import HTTPError
6b03e1e2 6from ..utils import (
e3755a62 7 ExtractorError,
e897bd82 8 clean_html,
e3755a62
S
9 int_or_none,
10 parse_age_limit,
e3755a62 11 try_get,
6b03e1e2
AT
12)
13
14
e3755a62
S
15class HRTiBaseIE(InfoExtractor):
16 """
17 Base Information Extractor for Croatian Radiotelevision
18 video on demand site https://hrti.hrt.hr
19 Reverse engineered from the JavaScript app in app.min.js
20 """
6b03e1e2
AT
21 _NETRC_MACHINE = 'hrti'
22
e3755a62
S
23 _APP_LANGUAGE = 'hr'
24 _APP_VERSION = '1.1'
25 _APP_PUBLICATION_ID = 'all_in_one'
26 _API_URL = 'http://clientapi.hrt.hr/client_api.php/config/identify/format/json'
52efa4b3 27 _token = None
6b03e1e2 28
52efa4b3 29 def _initialize_pre_login(self):
e3755a62
S
30 init_data = {
31 'application_publication_id': self._APP_PUBLICATION_ID
32 }
33
34 uuid = self._download_json(
35 self._API_URL, None, note='Downloading uuid',
36 errnote='Unable to download uuid',
37 data=json.dumps(init_data).encode('utf-8'))['uuid']
38
39 app_data = {
40 'uuid': uuid,
41 'application_publication_id': self._APP_PUBLICATION_ID,
42 'application_version': self._APP_VERSION
43 }
44
3d2623a8 45 req = Request(self._API_URL, data=json.dumps(app_data).encode('utf-8'))
6b03e1e2
AT
46 req.get_method = lambda: 'PUT'
47
48 resources = self._download_json(
e3755a62
S
49 req, None, note='Downloading session information',
50 errnote='Unable to download session information')
51
52 self._session_id = resources['session_id']
6b03e1e2 53
6b03e1e2
AT
54 modules = resources['modules']
55
e3755a62
S
56 self._search_url = modules['vod_catalog']['resources']['search']['uri'].format(
57 language=self._APP_LANGUAGE,
58 application_id=self._APP_PUBLICATION_ID)
6b03e1e2 59
3089bc74
S
60 self._login_url = (modules['user']['resources']['login']['uri']
61 + '/format/json').format(session_id=self._session_id)
6b03e1e2 62
e3755a62 63 self._logout_url = modules['user']['resources']['logout']['uri']
6b03e1e2 64
52efa4b3 65 def _perform_login(self, username, password):
e3755a62 66 auth_data = {
6b03e1e2
AT
67 'username': username,
68 'password': password,
e3755a62
S
69 }
70
6b03e1e2
AT
71 try:
72 auth_info = self._download_json(
e3755a62
S
73 self._login_url, None, note='Logging in', errnote='Unable to log in',
74 data=json.dumps(auth_data).encode('utf-8'))
75 except ExtractorError as e:
3d2623a8 76 if isinstance(e.cause, HTTPError) and e.cause.status == 406:
77 auth_info = self._parse_json(e.cause.response.read().encode('utf-8'), None)
e3755a62
S
78 else:
79 raise
80
81 error_message = auth_info.get('error', {}).get('message')
82 if error_message:
83 raise ExtractorError(
84 '%s said: %s' % (self.IE_NAME, error_message),
85 expected=True)
86
87 self._token = auth_info['secure_streaming_token']
6b03e1e2
AT
88
89 def _real_initialize(self):
52efa4b3 90 if not self._token:
91 # TODO: figure out authentication with cookies
92 self.raise_login_required(method='password')
6b03e1e2 93
e3755a62
S
94
95class HRTiIE(HRTiBaseIE):
96 _VALID_URL = r'''(?x)
97 (?:
98 hrti:(?P<short_id>[0-9]+)|
99 https?://
b0dde668 100 hrti\.hrt\.hr/(?:\#/)?video/show/(?P<id>[0-9]+)/(?P<display_id>[^/]+)?
e3755a62
S
101 )
102 '''
103 _TESTS = [{
104 'url': 'https://hrti.hrt.hr/#/video/show/2181385/republika-dokumentarna-serija-16-hd',
105 'info_dict': {
106 'id': '2181385',
107 'display_id': 'republika-dokumentarna-serija-16-hd',
108 'ext': 'mp4',
109 'title': 'REPUBLIKA, dokumentarna serija (1/6) (HD)',
110 'description': 'md5:48af85f620e8e0e1df4096270568544f',
111 'duration': 2922,
112 'view_count': int,
113 'average_rating': int,
114 'episode_number': int,
115 'season_number': int,
116 'age_limit': 12,
117 },
118 'skip': 'Requires account credentials',
119 }, {
120 'url': 'https://hrti.hrt.hr/#/video/show/2181385/',
121 'only_matching': True,
122 }, {
123 'url': 'hrti:2181385',
124 'only_matching': True,
b0dde668
AT
125 }, {
126 'url': 'https://hrti.hrt.hr/video/show/3873068/cuvar-dvorca-dramska-serija-14',
127 'only_matching': True,
e3755a62 128 }]
6b03e1e2
AT
129
130 def _real_extract(self, url):
5ad28e7f 131 mobj = self._match_valid_url(url)
e3755a62
S
132 video_id = mobj.group('short_id') or mobj.group('id')
133 display_id = mobj.group('display_id') or video_id
6b03e1e2 134
e3755a62
S
135 video = self._download_json(
136 '%s/video_id/%s/format/json' % (self._search_url, video_id),
137 display_id, 'Downloading video metadata JSON')['video'][0]
6b03e1e2 138
e3755a62
S
139 title_info = video['title']
140 title = title_info['title_long']
6b03e1e2
AT
141
142 movie = video['video_assets']['movie'][0]
e3755a62
S
143 m3u8_url = movie['url'].format(TOKEN=self._token)
144 formats = self._extract_m3u8_formats(
145 m3u8_url, display_id, 'mp4', entry_protocol='m3u8_native',
146 m3u8_id='hls')
6b03e1e2 147
e3755a62
S
148 description = clean_html(title_info.get('summary_long'))
149 age_limit = parse_age_limit(video.get('parental_control', {}).get('rating'))
150 view_count = int_or_none(video.get('views'))
151 average_rating = int_or_none(video.get('user_rating'))
152 duration = int_or_none(movie.get('duration'))
6b03e1e2
AT
153
154 return {
155 'id': video_id,
e3755a62 156 'display_id': display_id,
6b03e1e2
AT
157 'title': title,
158 'description': description,
e3755a62
S
159 'duration': duration,
160 'view_count': view_count,
161 'average_rating': average_rating,
162 'age_limit': age_limit,
6b03e1e2
AT
163 'formats': formats,
164 }
e3755a62
S
165
166
167class HRTiPlaylistIE(HRTiBaseIE):
ae5af890 168 _VALID_URL = r'https?://hrti\.hrt\.hr/(?:#/)?video/list/category/(?P<id>[0-9]+)/(?P<display_id>[^/]+)?'
e3755a62
S
169 _TESTS = [{
170 'url': 'https://hrti.hrt.hr/#/video/list/category/212/ekumena',
171 'info_dict': {
172 'id': '212',
173 'title': 'ekumena',
174 },
175 'playlist_mincount': 8,
176 'skip': 'Requires account credentials',
177 }, {
178 'url': 'https://hrti.hrt.hr/#/video/list/category/212/',
179 'only_matching': True,
ae5af890
S
180 }, {
181 'url': 'https://hrti.hrt.hr/video/list/category/212/ekumena',
182 'only_matching': True,
e3755a62
S
183 }]
184
185 def _real_extract(self, url):
5ad28e7f 186 mobj = self._match_valid_url(url)
e3755a62
S
187 category_id = mobj.group('id')
188 display_id = mobj.group('display_id') or category_id
189
190 response = self._download_json(
191 '%s/category_id/%s/format/json' % (self._search_url, category_id),
192 display_id, 'Downloading video metadata JSON')
193
194 video_ids = try_get(
195 response, lambda x: x['video_listings'][0]['alternatives'][0]['list'],
196 list) or [video['id'] for video in response.get('videos', []) if video.get('id')]
197
4cb13d0d 198 entries = [self.url_result('hrti:%s' % video_id) for video_id in video_ids]
e3755a62
S
199
200 return self.playlist_result(entries, category_id, display_id)