]>
Commit | Line | Data |
---|---|---|
3ca3f77f U |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import json | |
5 | import re | |
6 | ||
7 | from .common import InfoExtractor | |
8 | from ..compat import compat_HTTPError | |
9 | from ..utils import ( | |
10 | clean_html, | |
11 | ExtractorError, | |
12 | int_or_none, | |
13 | parse_age_limit, | |
14 | sanitized_Request, | |
15 | try_get, | |
16 | ) | |
17 | ||
18 | ||
19 | class 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 | """ | |
25 | _NETRC_MACHINE = 'hrti' | |
26 | ||
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' | |
31 | ||
32 | def _initialize_api(self): | |
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')) | |
49 | req.get_method = lambda: 'PUT' | |
50 | ||
51 | resources = self._download_json( | |
52 | req, None, note='Downloading session information', | |
53 | errnote='Unable to download session information') | |
54 | ||
55 | self._session_id = resources['session_id'] | |
56 | ||
57 | modules = resources['modules'] | |
58 | ||
59 | self._search_url = modules['vod_catalog']['resources']['search']['uri'].format( | |
60 | language=self._APP_LANGUAGE, | |
61 | application_id=self._APP_PUBLICATION_ID) | |
62 | ||
63 | self._login_url = (modules['user']['resources']['login']['uri'] | |
64 | + '/format/json').format(session_id=self._session_id) | |
65 | ||
66 | self._logout_url = modules['user']['resources']['logout']['uri'] | |
67 | ||
68 | def _login(self): | |
69 | username, password = self._get_login_info() | |
70 | # TODO: figure out authentication with cookies | |
71 | if username is None or password is None: | |
72 | self.raise_login_required() | |
73 | ||
74 | auth_data = { | |
75 | 'username': username, | |
76 | 'password': password, | |
77 | } | |
78 | ||
79 | try: | |
80 | auth_info = self._download_json( | |
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'] | |
96 | ||
97 | def _real_initialize(self): | |
98 | self._initialize_api() | |
99 | self._login() | |
100 | ||
101 | ||
102 | class HRTiIE(HRTiBaseIE): | |
103 | _VALID_URL = r'''(?x) | |
104 | (?: | |
105 | hrti:(?P<short_id>[0-9]+)| | |
106 | https?:// | |
107 | hrti\.hrt\.hr/(?:\#/)?video/show/(?P<id>[0-9]+)/(?P<display_id>[^/]+)? | |
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, | |
132 | }, { | |
133 | 'url': 'https://hrti.hrt.hr/video/show/3873068/cuvar-dvorca-dramska-serija-14', | |
134 | 'only_matching': True, | |
135 | }] | |
136 | ||
137 | def _real_extract(self, url): | |
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 | |
141 | ||
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] | |
145 | ||
146 | title_info = video['title'] | |
147 | title = title_info['title_long'] | |
148 | ||
149 | movie = video['video_assets']['movie'][0] | |
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') | |
154 | self._sort_formats(formats) | |
155 | ||
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')) | |
161 | ||
162 | return { | |
163 | 'id': video_id, | |
164 | 'display_id': display_id, | |
165 | 'title': title, | |
166 | 'description': description, | |
167 | 'duration': duration, | |
168 | 'view_count': view_count, | |
169 | 'average_rating': average_rating, | |
170 | 'age_limit': age_limit, | |
171 | 'formats': formats, | |
172 | } | |
173 | ||
174 | ||
175 | class HRTiPlaylistIE(HRTiBaseIE): | |
176 | _VALID_URL = r'https?://hrti\.hrt\.hr/(?:#/)?video/list/category/(?P<id>[0-9]+)/(?P<display_id>[^/]+)?' | |
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, | |
188 | }, { | |
189 | 'url': 'https://hrti.hrt.hr/video/list/category/212/ekumena', | |
190 | 'only_matching': True, | |
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 | ||
206 | entries = [self.url_result('hrti:%s' % video_id) for video_id in video_ids] | |
207 | ||
208 | return self.playlist_result(entries, category_id, display_id) |