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