]>
Commit | Line | Data |
---|---|---|
1 | import functools | |
2 | import json | |
3 | import urllib.parse | |
4 | ||
5 | from .common import InfoExtractor | |
6 | from ..utils import ( | |
7 | ExtractorError, | |
8 | HEADRequest, | |
9 | OnDemandPagedList, | |
10 | UnsupportedError, | |
11 | determine_ext, | |
12 | int_or_none, | |
13 | mimetype2ext, | |
14 | parse_qs, | |
15 | traverse_obj, | |
16 | try_get, | |
17 | url_or_none, | |
18 | urlhandle_detect_ext, | |
19 | urljoin, | |
20 | ) | |
21 | ||
22 | ||
23 | class LBRYBaseIE(InfoExtractor): | |
24 | _BASE_URL_REGEX = r'(?:https?://(?:www\.)?(?:lbry\.tv|odysee\.com)/|lbry://)' | |
25 | _CLAIM_ID_REGEX = r'[0-9a-f]{1,40}' | |
26 | _OPT_CLAIM_ID = '[^:/?#&]+(?:[:#]%s)?' % _CLAIM_ID_REGEX | |
27 | _SUPPORTED_STREAM_TYPES = ['video', 'audio'] | |
28 | ||
29 | def _call_api_proxy(self, method, display_id, params, resource): | |
30 | headers = {'Content-Type': 'application/json-rpc'} | |
31 | token = try_get(self._get_cookies('https://odysee.com'), lambda x: x['auth_token'].value) | |
32 | if token: | |
33 | headers['x-lbry-auth-token'] = token | |
34 | response = self._download_json( | |
35 | 'https://api.lbry.tv/api/v1/proxy', | |
36 | display_id, 'Downloading %s JSON metadata' % resource, | |
37 | headers=headers, | |
38 | data=json.dumps({ | |
39 | 'method': method, | |
40 | 'params': params, | |
41 | }).encode()) | |
42 | err = response.get('error') | |
43 | if err: | |
44 | raise ExtractorError( | |
45 | f'{self.IE_NAME} said: {err.get("code")} - {err.get("message")}', expected=True) | |
46 | return response['result'] | |
47 | ||
48 | def _resolve_url(self, url, display_id, resource): | |
49 | return self._call_api_proxy( | |
50 | 'resolve', display_id, {'urls': url}, resource)[url] | |
51 | ||
52 | def _permanent_url(self, url, claim_name, claim_id): | |
53 | return urljoin( | |
54 | url.replace('lbry://', 'https://lbry.tv/'), | |
55 | '/%s:%s' % (claim_name, claim_id)) | |
56 | ||
57 | def _parse_stream(self, stream, url): | |
58 | stream_type = traverse_obj(stream, ('value', 'stream_type', {str})) | |
59 | ||
60 | info = traverse_obj(stream, { | |
61 | 'title': ('value', 'title', {str}), | |
62 | 'thumbnail': ('value', 'thumbnail', 'url', {url_or_none}), | |
63 | 'description': ('value', 'description', {str}), | |
64 | 'license': ('value', 'license', {str}), | |
65 | 'timestamp': ('timestamp', {int_or_none}), | |
66 | 'release_timestamp': ('value', 'release_time', {int_or_none}), | |
67 | 'tags': ('value', 'tags', ..., {lambda x: x or None}), | |
68 | 'duration': ('value', stream_type, 'duration', {int_or_none}), | |
69 | 'channel': ('signing_channel', 'value', 'title', {str}), | |
70 | 'channel_id': ('signing_channel', 'claim_id', {str}), | |
71 | }) | |
72 | ||
73 | channel_name = traverse_obj(stream, ('signing_channel', 'name', {str})) | |
74 | if channel_name and info.get('channel_id'): | |
75 | info['channel_url'] = self._permanent_url(url, channel_name, info['channel_id']) | |
76 | ||
77 | return info | |
78 | ||
79 | ||
80 | class LBRYIE(LBRYBaseIE): | |
81 | IE_NAME = 'lbry' | |
82 | _VALID_URL = LBRYBaseIE._BASE_URL_REGEX + r'(?P<id>\$/[^/]+/[^/]+/{1}|@{0}/{0}|(?!@){0})'.format(LBRYBaseIE._OPT_CLAIM_ID, LBRYBaseIE._CLAIM_ID_REGEX) | |
83 | _TESTS = [{ | |
84 | # Video | |
85 | 'url': 'https://lbry.tv/@Mantega:1/First-day-LBRY:1', | |
86 | 'md5': 'fffd15d76062e9a985c22c7c7f2f4805', | |
87 | 'info_dict': { | |
88 | 'id': '17f983b61f53091fb8ea58a9c56804e4ff8cff4d', | |
89 | 'ext': 'mp4', | |
90 | 'title': 'First day in LBRY? Start HERE!', | |
91 | 'description': 'md5:f6cb5c704b332d37f5119313c2c98f51', | |
92 | 'timestamp': 1595694354, | |
93 | 'upload_date': '20200725', | |
94 | 'release_timestamp': 1595340697, | |
95 | 'release_date': '20200721', | |
96 | 'width': 1280, | |
97 | 'height': 720, | |
98 | 'thumbnail': 'https://spee.ch/7/67f2d809c263288c.png', | |
99 | 'license': 'None', | |
100 | 'duration': 346, | |
101 | 'channel': 'LBRY/Odysee rats united!!!', | |
102 | 'channel_id': '1c8ad6a2ab4e889a71146ae4deeb23bb92dab627', | |
103 | 'channel_url': 'https://lbry.tv/@Mantega:1c8ad6a2ab4e889a71146ae4deeb23bb92dab627', | |
104 | 'tags': [ | |
105 | 'first day in lbry', | |
106 | 'lbc', | |
107 | 'lbry', | |
108 | 'start', | |
109 | 'tutorial' | |
110 | ], | |
111 | } | |
112 | }, { | |
113 | # Audio | |
114 | 'url': 'https://lbry.tv/@LBRYFoundation:0/Episode-1:e', | |
115 | 'md5': 'c94017d3eba9b49ce085a8fad6b98d00', | |
116 | 'info_dict': { | |
117 | 'id': 'e7d93d772bd87e2b62d5ab993c1c3ced86ebb396', | |
118 | 'ext': 'mp3', | |
119 | 'title': 'The LBRY Foundation Community Podcast Episode 1 - Introduction, Streaming on LBRY, Transcoding', | |
120 | 'description': 'md5:661ac4f1db09f31728931d7b88807a61', | |
121 | 'timestamp': 1591312601, | |
122 | 'upload_date': '20200604', | |
123 | 'release_timestamp': 1591312421, | |
124 | 'release_date': '20200604', | |
125 | 'tags': list, | |
126 | 'duration': 2570, | |
127 | 'channel': 'The LBRY Foundation', | |
128 | 'channel_id': '0ed629d2b9c601300cacf7eabe9da0be79010212', | |
129 | 'channel_url': 'https://lbry.tv/@LBRYFoundation:0ed629d2b9c601300cacf7eabe9da0be79010212', | |
130 | 'vcodec': 'none', | |
131 | 'thumbnail': 'https://spee.ch/d/0bc63b0e6bf1492d.png', | |
132 | 'license': 'None', | |
133 | } | |
134 | }, { | |
135 | # HLS | |
136 | 'url': 'https://odysee.com/@gardeningincanada:b/plants-i-will-never-grow-again.-the:e', | |
137 | 'md5': '25049011f3c8bc2f8b60ad88a031837e', | |
138 | 'info_dict': { | |
139 | 'id': 'e51671357333fe22ae88aad320bde2f6f96b1410', | |
140 | 'ext': 'mp4', | |
141 | 'title': 'PLANTS I WILL NEVER GROW AGAIN. THE BLACK LIST PLANTS FOR A CANADIAN GARDEN | Gardening in Canada 🍁', | |
142 | 'description': 'md5:9c539c6a03fb843956de61a4d5288d5e', | |
143 | 'timestamp': 1618254123, | |
144 | 'upload_date': '20210412', | |
145 | 'release_timestamp': 1618254002, | |
146 | 'release_date': '20210412', | |
147 | 'tags': list, | |
148 | 'duration': 554, | |
149 | 'channel': 'Gardening In Canada', | |
150 | 'channel_id': 'b8be0e93b423dad221abe29545fbe8ec36e806bc', | |
151 | 'channel_url': 'https://odysee.com/@gardeningincanada:b8be0e93b423dad221abe29545fbe8ec36e806bc', | |
152 | 'formats': 'mincount:3', | |
153 | 'thumbnail': 'https://thumbnails.lbry.com/AgHSc_HzrrE', | |
154 | 'license': 'Copyrighted (contact publisher)', | |
155 | } | |
156 | }, { | |
157 | # HLS live stream (might expire) | |
158 | 'url': 'https://odysee.com/@RT:fd/livestream_RT:d', | |
159 | 'info_dict': { | |
160 | 'id': 'fdd11cb3ab75f95efb7b3bc2d726aa13ac915b66', | |
161 | 'ext': 'mp4', | |
162 | 'live_status': 'is_live', | |
163 | 'title': 'startswith:RT News | Livestream 24/7', | |
164 | 'description': 'md5:fe68d0056dfe79c1a6b8ce8c34d5f6fa', | |
165 | 'timestamp': int, | |
166 | 'upload_date': str, | |
167 | 'release_timestamp': int, | |
168 | 'release_date': str, | |
169 | 'tags': list, | |
170 | 'duration': None, | |
171 | 'channel': 'RT', | |
172 | 'channel_id': 'fdd11cb3ab75f95efb7b3bc2d726aa13ac915b66', | |
173 | 'channel_url': 'https://odysee.com/@RT:fdd11cb3ab75f95efb7b3bc2d726aa13ac915b66', | |
174 | 'formats': 'mincount:1', | |
175 | 'thumbnail': 'startswith:https://thumb', | |
176 | 'license': 'None', | |
177 | }, | |
178 | 'params': {'skip_download': True} | |
179 | }, { | |
180 | # original quality format w/higher resolution than HLS formats | |
181 | 'url': 'https://odysee.com/@wickedtruths:2/Biotechnological-Invasion-of-Skin-(April-2023):4', | |
182 | 'md5': '305b0b3b369bde1b984961f005b67193', | |
183 | 'info_dict': { | |
184 | 'id': '41fbfe805eb73c8d3012c0c49faa0f563274f634', | |
185 | 'ext': 'mp4', | |
186 | 'title': 'Biotechnological Invasion of Skin (April 2023)', | |
187 | 'description': 'md5:709a2f4c07bd8891cda3a7cc2d6fcf5c', | |
188 | 'channel': 'Wicked Truths', | |
189 | 'channel_id': '23d2bbf856b0ceed5b1d7c5960bcc72da5a20cb0', | |
190 | 'channel_url': 'https://odysee.com/@wickedtruths:23d2bbf856b0ceed5b1d7c5960bcc72da5a20cb0', | |
191 | 'timestamp': 1685790036, | |
192 | 'upload_date': '20230603', | |
193 | 'release_timestamp': 1685617473, | |
194 | 'release_date': '20230601', | |
195 | 'duration': 1063, | |
196 | 'thumbnail': 'https://thumbs.odycdn.com/4e6d39da4df0cfdad45f64e253a15959.webp', | |
197 | 'tags': ['smart skin surveillance', 'biotechnology invasion of skin', 'morgellons'], | |
198 | 'license': 'None', | |
199 | 'protocol': 'https', # test for direct mp4 download | |
200 | }, | |
201 | }, { | |
202 | 'url': 'https://odysee.com/@BrodieRobertson:5/apple-is-tracking-everything-you-do-on:e', | |
203 | 'only_matching': True, | |
204 | }, { | |
205 | 'url': 'https://odysee.com/@ScammerRevolts:b0/I-SYSKEY\'D-THE-SAME-SCAMMERS-3-TIMES!:b', | |
206 | 'only_matching': True, | |
207 | }, { | |
208 | 'url': 'https://lbry.tv/Episode-1:e7d93d772bd87e2b62d5ab993c1c3ced86ebb396', | |
209 | 'only_matching': True, | |
210 | }, { | |
211 | 'url': 'https://lbry.tv/$/embed/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396', | |
212 | 'only_matching': True, | |
213 | }, { | |
214 | 'url': 'https://lbry.tv/Episode-1:e7', | |
215 | 'only_matching': True, | |
216 | }, { | |
217 | 'url': 'https://lbry.tv/@LBRYFoundation/Episode-1', | |
218 | 'only_matching': True, | |
219 | }, { | |
220 | 'url': 'https://lbry.tv/$/download/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396', | |
221 | 'only_matching': True, | |
222 | }, { | |
223 | 'url': 'https://lbry.tv/@lacajadepandora:a/TRUMP-EST%C3%81-BIEN-PUESTO-con-Pilar-Baselga,-Carlos-Senra,-Luis-Palacios-(720p_30fps_H264-192kbit_AAC):1', | |
224 | 'only_matching': True, | |
225 | }, { | |
226 | 'url': 'lbry://@lbry#3f/odysee#7', | |
227 | 'only_matching': True, | |
228 | }] | |
229 | ||
230 | def _real_extract(self, url): | |
231 | display_id = self._match_id(url) | |
232 | if display_id.startswith('$/'): | |
233 | display_id = display_id.split('/', 2)[-1].replace('/', ':') | |
234 | else: | |
235 | display_id = display_id.replace(':', '#') | |
236 | display_id = urllib.parse.unquote(display_id) | |
237 | uri = 'lbry://' + display_id | |
238 | result = self._resolve_url(uri, display_id, 'stream') | |
239 | headers = {'Referer': 'https://odysee.com/'} | |
240 | ||
241 | formats = [] | |
242 | stream_type = traverse_obj(result, ('value', 'stream_type', {str})) | |
243 | ||
244 | if stream_type in self._SUPPORTED_STREAM_TYPES: | |
245 | claim_id, is_live = result['claim_id'], False | |
246 | streaming_url = self._call_api_proxy( | |
247 | 'get', claim_id, {'uri': uri}, 'streaming url')['streaming_url'] | |
248 | ||
249 | # GET request returns original video/audio file if available | |
250 | ext = urlhandle_detect_ext(self._request_webpage( | |
251 | streaming_url, display_id, 'Checking for original quality', headers=headers)) | |
252 | if ext != 'm3u8': | |
253 | formats.append({ | |
254 | 'url': streaming_url, | |
255 | 'format_id': 'original', | |
256 | 'quality': 1, | |
257 | **traverse_obj(result, ('value', { | |
258 | 'ext': ('source', (('name', {determine_ext}), ('media_type', {mimetype2ext}))), | |
259 | 'filesize': ('source', 'size', {int_or_none}), | |
260 | 'width': ('video', 'width', {int_or_none}), | |
261 | 'height': ('video', 'height', {int_or_none}), | |
262 | }), get_all=False), | |
263 | 'vcodec': 'none' if stream_type == 'audio' else None, | |
264 | }) | |
265 | ||
266 | # HEAD request returns redirect response to m3u8 URL if available | |
267 | final_url = self._request_webpage( | |
268 | HEADRequest(streaming_url), display_id, headers=headers, | |
269 | note='Downloading streaming redirect url info').geturl() | |
270 | ||
271 | elif result.get('value_type') == 'stream': | |
272 | claim_id, is_live = result['signing_channel']['claim_id'], True | |
273 | live_data = self._download_json( | |
274 | 'https://api.odysee.live/livestream/is_live', claim_id, | |
275 | query={'channel_claim_id': claim_id}, | |
276 | note='Downloading livestream JSON metadata')['data'] | |
277 | final_url = live_data.get('VideoURL') | |
278 | # Upcoming videos may still give VideoURL | |
279 | if not live_data.get('Live'): | |
280 | final_url = None | |
281 | self.raise_no_formats('This stream is not live', True, claim_id) | |
282 | ||
283 | else: | |
284 | raise UnsupportedError(url) | |
285 | ||
286 | if determine_ext(final_url) == 'm3u8': | |
287 | formats.extend(self._extract_m3u8_formats( | |
288 | final_url, display_id, 'mp4', m3u8_id='hls', live=is_live, headers=headers)) | |
289 | ||
290 | return { | |
291 | **self._parse_stream(result, url), | |
292 | 'id': claim_id, | |
293 | 'formats': formats, | |
294 | 'is_live': is_live, | |
295 | 'http_headers': headers, | |
296 | } | |
297 | ||
298 | ||
299 | class LBRYChannelIE(LBRYBaseIE): | |
300 | IE_NAME = 'lbry:channel' | |
301 | _VALID_URL = LBRYBaseIE._BASE_URL_REGEX + r'(?P<id>@%s)/?(?:[?&]|$)' % LBRYBaseIE._OPT_CLAIM_ID | |
302 | _TESTS = [{ | |
303 | 'url': 'https://lbry.tv/@LBRYFoundation:0', | |
304 | 'info_dict': { | |
305 | 'id': '0ed629d2b9c601300cacf7eabe9da0be79010212', | |
306 | 'title': 'The LBRY Foundation', | |
307 | 'description': 'Channel for the LBRY Foundation. Follow for updates and news.', | |
308 | }, | |
309 | 'playlist_mincount': 29, | |
310 | }, { | |
311 | 'url': 'https://lbry.tv/@LBRYFoundation', | |
312 | 'only_matching': True, | |
313 | }, { | |
314 | 'url': 'lbry://@lbry#3f', | |
315 | 'only_matching': True, | |
316 | }] | |
317 | _PAGE_SIZE = 50 | |
318 | ||
319 | def _fetch_page(self, claim_id, url, params, page): | |
320 | page += 1 | |
321 | page_params = { | |
322 | 'channel_ids': [claim_id], | |
323 | 'claim_type': 'stream', | |
324 | 'no_totals': True, | |
325 | 'page': page, | |
326 | 'page_size': self._PAGE_SIZE, | |
327 | } | |
328 | page_params.update(params) | |
329 | result = self._call_api_proxy( | |
330 | 'claim_search', claim_id, page_params, 'page %d' % page) | |
331 | for item in (result.get('items') or []): | |
332 | stream_claim_name = item.get('name') | |
333 | stream_claim_id = item.get('claim_id') | |
334 | if not (stream_claim_name and stream_claim_id): | |
335 | continue | |
336 | ||
337 | yield { | |
338 | **self._parse_stream(item, url), | |
339 | '_type': 'url', | |
340 | 'id': stream_claim_id, | |
341 | 'url': self._permanent_url(url, stream_claim_name, stream_claim_id), | |
342 | } | |
343 | ||
344 | def _real_extract(self, url): | |
345 | display_id = self._match_id(url).replace(':', '#') | |
346 | result = self._resolve_url( | |
347 | 'lbry://' + display_id, display_id, 'channel') | |
348 | claim_id = result['claim_id'] | |
349 | qs = parse_qs(url) | |
350 | content = qs.get('content', [None])[0] | |
351 | params = { | |
352 | 'fee_amount': qs.get('fee_amount', ['>=0'])[0], | |
353 | 'order_by': { | |
354 | 'new': ['release_time'], | |
355 | 'top': ['effective_amount'], | |
356 | 'trending': ['trending_group', 'trending_mixed'], | |
357 | }[qs.get('order', ['new'])[0]], | |
358 | 'stream_types': [content] if content in ['audio', 'video'] else self._SUPPORTED_STREAM_TYPES, | |
359 | } | |
360 | duration = qs.get('duration', [None])[0] | |
361 | if duration: | |
362 | params['duration'] = { | |
363 | 'long': '>=1200', | |
364 | 'short': '<=240', | |
365 | }[duration] | |
366 | language = qs.get('language', ['all'])[0] | |
367 | if language != 'all': | |
368 | languages = [language] | |
369 | if language == 'en': | |
370 | languages.append('none') | |
371 | params['any_languages'] = languages | |
372 | entries = OnDemandPagedList( | |
373 | functools.partial(self._fetch_page, claim_id, url, params), | |
374 | self._PAGE_SIZE) | |
375 | result_value = result.get('value') or {} | |
376 | return self.playlist_result( | |
377 | entries, claim_id, result_value.get('title'), | |
378 | result_value.get('description')) |