]>
Commit | Line | Data |
---|---|---|
1 | import functools | |
2 | import json | |
3 | ||
4 | from .common import InfoExtractor | |
5 | from ..compat import ( | |
6 | compat_str, | |
7 | compat_urllib_parse_unquote, | |
8 | ) | |
9 | from ..utils import ( | |
10 | determine_ext, | |
11 | ExtractorError, | |
12 | int_or_none, | |
13 | mimetype2ext, | |
14 | parse_qs, | |
15 | OnDemandPagedList, | |
16 | try_get, | |
17 | UnsupportedError, | |
18 | urljoin, | |
19 | ) | |
20 | ||
21 | ||
22 | class LBRYBaseIE(InfoExtractor): | |
23 | _BASE_URL_REGEX = r'(?:https?://(?:www\.)?(?:lbry\.tv|odysee\.com)/|lbry://)' | |
24 | _CLAIM_ID_REGEX = r'[0-9a-f]{1,40}' | |
25 | _OPT_CLAIM_ID = '[^:/?#&]+(?:[:#]%s)?' % _CLAIM_ID_REGEX | |
26 | _SUPPORTED_STREAM_TYPES = ['video', 'audio'] | |
27 | ||
28 | def _call_api_proxy(self, method, display_id, params, resource): | |
29 | response = self._download_json( | |
30 | 'https://api.lbry.tv/api/v1/proxy', | |
31 | display_id, 'Downloading %s JSON metadata' % resource, | |
32 | headers={'Content-Type': 'application/json-rpc'}, | |
33 | data=json.dumps({ | |
34 | 'method': method, | |
35 | 'params': params, | |
36 | }).encode()) | |
37 | err = response.get('error') | |
38 | if err: | |
39 | raise ExtractorError( | |
40 | f'{self.IE_NAME} said: {err.get("code")} - {err.get("message")}', expected=True) | |
41 | return response['result'] | |
42 | ||
43 | def _resolve_url(self, url, display_id, resource): | |
44 | return self._call_api_proxy( | |
45 | 'resolve', display_id, {'urls': url}, resource)[url] | |
46 | ||
47 | def _permanent_url(self, url, claim_name, claim_id): | |
48 | return urljoin( | |
49 | url.replace('lbry://', 'https://lbry.tv/'), | |
50 | '/%s:%s' % (claim_name, claim_id)) | |
51 | ||
52 | def _parse_stream(self, stream, url): | |
53 | stream_value = stream.get('value') or {} | |
54 | stream_type = stream_value.get('stream_type') | |
55 | source = stream_value.get('source') or {} | |
56 | media = stream_value.get(stream_type) or {} | |
57 | signing_channel = stream.get('signing_channel') or {} | |
58 | channel_name = signing_channel.get('name') | |
59 | channel_claim_id = signing_channel.get('claim_id') | |
60 | channel_url = None | |
61 | if channel_name and channel_claim_id: | |
62 | channel_url = self._permanent_url(url, channel_name, channel_claim_id) | |
63 | ||
64 | info = { | |
65 | 'thumbnail': try_get(stream_value, lambda x: x['thumbnail']['url'], compat_str), | |
66 | 'description': stream_value.get('description'), | |
67 | 'license': stream_value.get('license'), | |
68 | 'timestamp': int_or_none(stream.get('timestamp')), | |
69 | 'release_timestamp': int_or_none(stream_value.get('release_time')), | |
70 | 'tags': stream_value.get('tags'), | |
71 | 'duration': int_or_none(media.get('duration')), | |
72 | 'channel': try_get(signing_channel, lambda x: x['value']['title']), | |
73 | 'channel_id': channel_claim_id, | |
74 | 'channel_url': channel_url, | |
75 | 'ext': determine_ext(source.get('name')) or mimetype2ext(source.get('media_type')), | |
76 | 'filesize': int_or_none(source.get('size')), | |
77 | } | |
78 | if stream_type == 'audio': | |
79 | info['vcodec'] = 'none' | |
80 | else: | |
81 | info.update({ | |
82 | 'width': int_or_none(media.get('width')), | |
83 | 'height': int_or_none(media.get('height')), | |
84 | }) | |
85 | return info | |
86 | ||
87 | ||
88 | class LBRYIE(LBRYBaseIE): | |
89 | IE_NAME = 'lbry' | |
90 | _VALID_URL = LBRYBaseIE._BASE_URL_REGEX + r'(?P<id>\$/[^/]+/[^/]+/{1}|@{0}/{0}|(?!@){0})'.format(LBRYBaseIE._OPT_CLAIM_ID, LBRYBaseIE._CLAIM_ID_REGEX) | |
91 | _TESTS = [{ | |
92 | # Video | |
93 | 'url': 'https://lbry.tv/@Mantega:1/First-day-LBRY:1', | |
94 | 'md5': '65bd7ec1f6744ada55da8e4c48a2edf9', | |
95 | 'info_dict': { | |
96 | 'id': '17f983b61f53091fb8ea58a9c56804e4ff8cff4d', | |
97 | 'ext': 'mp4', | |
98 | 'title': 'First day in LBRY? Start HERE!', | |
99 | 'description': 'md5:f6cb5c704b332d37f5119313c2c98f51', | |
100 | 'timestamp': 1595694354, | |
101 | 'upload_date': '20200725', | |
102 | 'release_timestamp': 1595340697, | |
103 | 'release_date': '20200721', | |
104 | 'width': 1280, | |
105 | 'height': 720, | |
106 | } | |
107 | }, { | |
108 | # Audio | |
109 | 'url': 'https://lbry.tv/@LBRYFoundation:0/Episode-1:e', | |
110 | 'md5': 'c94017d3eba9b49ce085a8fad6b98d00', | |
111 | 'info_dict': { | |
112 | 'id': 'e7d93d772bd87e2b62d5ab993c1c3ced86ebb396', | |
113 | 'ext': 'mp3', | |
114 | 'title': 'The LBRY Foundation Community Podcast Episode 1 - Introduction, Streaming on LBRY, Transcoding', | |
115 | 'description': 'md5:661ac4f1db09f31728931d7b88807a61', | |
116 | 'timestamp': 1591312601, | |
117 | 'upload_date': '20200604', | |
118 | 'release_timestamp': 1591312421, | |
119 | 'release_date': '20200604', | |
120 | 'tags': list, | |
121 | 'duration': 2570, | |
122 | 'channel': 'The LBRY Foundation', | |
123 | 'channel_id': '0ed629d2b9c601300cacf7eabe9da0be79010212', | |
124 | 'channel_url': 'https://lbry.tv/@LBRYFoundation:0ed629d2b9c601300cacf7eabe9da0be79010212', | |
125 | 'vcodec': 'none', | |
126 | } | |
127 | }, { | |
128 | # HLS | |
129 | 'url': 'https://odysee.com/@gardeningincanada:b/plants-i-will-never-grow-again.-the:e', | |
130 | 'md5': 'fc82f45ea54915b1495dd7cb5cc1289f', | |
131 | 'info_dict': { | |
132 | 'id': 'e51671357333fe22ae88aad320bde2f6f96b1410', | |
133 | 'ext': 'mp4', | |
134 | 'title': 'PLANTS I WILL NEVER GROW AGAIN. THE BLACK LIST PLANTS FOR A CANADIAN GARDEN | Gardening in Canada 🍁', | |
135 | 'description': 'md5:9c539c6a03fb843956de61a4d5288d5e', | |
136 | 'timestamp': 1618254123, | |
137 | 'upload_date': '20210412', | |
138 | 'release_timestamp': 1618254002, | |
139 | 'release_date': '20210412', | |
140 | 'tags': list, | |
141 | 'duration': 554, | |
142 | 'channel': 'Gardening In Canada', | |
143 | 'channel_id': 'b8be0e93b423dad221abe29545fbe8ec36e806bc', | |
144 | 'channel_url': 'https://odysee.com/@gardeningincanada:b8be0e93b423dad221abe29545fbe8ec36e806bc', | |
145 | 'formats': 'mincount:3', | |
146 | } | |
147 | }, { | |
148 | 'url': 'https://odysee.com/@BrodieRobertson:5/apple-is-tracking-everything-you-do-on:e', | |
149 | 'only_matching': True, | |
150 | }, { | |
151 | 'url': "https://odysee.com/@ScammerRevolts:b0/I-SYSKEY'D-THE-SAME-SCAMMERS-3-TIMES!:b", | |
152 | 'only_matching': True, | |
153 | }, { | |
154 | 'url': 'https://lbry.tv/Episode-1:e7d93d772bd87e2b62d5ab993c1c3ced86ebb396', | |
155 | 'only_matching': True, | |
156 | }, { | |
157 | 'url': 'https://lbry.tv/$/embed/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396', | |
158 | 'only_matching': True, | |
159 | }, { | |
160 | 'url': 'https://lbry.tv/Episode-1:e7', | |
161 | 'only_matching': True, | |
162 | }, { | |
163 | 'url': 'https://lbry.tv/@LBRYFoundation/Episode-1', | |
164 | 'only_matching': True, | |
165 | }, { | |
166 | 'url': 'https://lbry.tv/$/download/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396', | |
167 | 'only_matching': True, | |
168 | }, { | |
169 | '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', | |
170 | 'only_matching': True, | |
171 | }, { | |
172 | 'url': 'lbry://@lbry#3f/odysee#7', | |
173 | 'only_matching': True, | |
174 | }] | |
175 | ||
176 | def _real_extract(self, url): | |
177 | display_id = self._match_id(url) | |
178 | if display_id.startswith('$/'): | |
179 | display_id = display_id.split('/', 2)[-1].replace('/', ':') | |
180 | else: | |
181 | display_id = display_id.replace(':', '#') | |
182 | display_id = compat_urllib_parse_unquote(display_id) | |
183 | uri = 'lbry://' + display_id | |
184 | result = self._resolve_url(uri, display_id, 'stream') | |
185 | if result['value'].get('stream_type') in self._SUPPORTED_STREAM_TYPES: | |
186 | claim_id, is_live, headers = result['claim_id'], False, None | |
187 | streaming_url = self._call_api_proxy( | |
188 | 'get', claim_id, {'uri': uri}, 'streaming url')['streaming_url'] | |
189 | final_url = self._request_webpage( | |
190 | streaming_url, display_id, note='Downloading streaming redirect url info').geturl() | |
191 | elif result.get('value_type') == 'stream': | |
192 | claim_id, is_live = result['signing_channel']['claim_id'], True | |
193 | headers = {'referer': 'https://player.odysee.live/'} | |
194 | live_data = self._download_json( | |
195 | f'https://api.live.odysee.com/v1/odysee/live/{claim_id}', claim_id, | |
196 | note='Downloading livestream JSON metadata')['data'] | |
197 | streaming_url = final_url = live_data.get('url') | |
198 | if not final_url and not live_data.get('live'): | |
199 | self.raise_no_formats('This stream is not live', True, claim_id) | |
200 | else: | |
201 | raise UnsupportedError(url) | |
202 | ||
203 | info = self._parse_stream(result, url) | |
204 | if determine_ext(final_url) == 'm3u8': | |
205 | info['formats'] = self._extract_m3u8_formats( | |
206 | final_url, display_id, 'mp4', 'm3u8_native', m3u8_id='hls', live=is_live, headers=headers) | |
207 | self._sort_formats(info['formats']) | |
208 | else: | |
209 | info['url'] = streaming_url | |
210 | return { | |
211 | **info, | |
212 | 'id': claim_id, | |
213 | 'title': result['value']['title'], | |
214 | 'is_live': is_live, | |
215 | 'http_headers': headers, | |
216 | } | |
217 | ||
218 | ||
219 | class LBRYChannelIE(LBRYBaseIE): | |
220 | IE_NAME = 'lbry:channel' | |
221 | _VALID_URL = LBRYBaseIE._BASE_URL_REGEX + r'(?P<id>@%s)/?(?:[?&]|$)' % LBRYBaseIE._OPT_CLAIM_ID | |
222 | _TESTS = [{ | |
223 | 'url': 'https://lbry.tv/@LBRYFoundation:0', | |
224 | 'info_dict': { | |
225 | 'id': '0ed629d2b9c601300cacf7eabe9da0be79010212', | |
226 | 'title': 'The LBRY Foundation', | |
227 | 'description': 'Channel for the LBRY Foundation. Follow for updates and news.', | |
228 | }, | |
229 | 'playlist_count': 29, | |
230 | }, { | |
231 | 'url': 'https://lbry.tv/@LBRYFoundation', | |
232 | 'only_matching': True, | |
233 | }, { | |
234 | 'url': 'lbry://@lbry#3f', | |
235 | 'only_matching': True, | |
236 | }] | |
237 | _PAGE_SIZE = 50 | |
238 | ||
239 | def _fetch_page(self, claim_id, url, params, page): | |
240 | page += 1 | |
241 | page_params = { | |
242 | 'channel_ids': [claim_id], | |
243 | 'claim_type': 'stream', | |
244 | 'no_totals': True, | |
245 | 'page': page, | |
246 | 'page_size': self._PAGE_SIZE, | |
247 | } | |
248 | page_params.update(params) | |
249 | result = self._call_api_proxy( | |
250 | 'claim_search', claim_id, page_params, 'page %d' % page) | |
251 | for item in (result.get('items') or []): | |
252 | stream_claim_name = item.get('name') | |
253 | stream_claim_id = item.get('claim_id') | |
254 | if not (stream_claim_name and stream_claim_id): | |
255 | continue | |
256 | ||
257 | info = self._parse_stream(item, url) | |
258 | info.update({ | |
259 | '_type': 'url', | |
260 | 'id': stream_claim_id, | |
261 | 'title': try_get(item, lambda x: x['value']['title']), | |
262 | 'url': self._permanent_url(url, stream_claim_name, stream_claim_id), | |
263 | }) | |
264 | yield info | |
265 | ||
266 | def _real_extract(self, url): | |
267 | display_id = self._match_id(url).replace(':', '#') | |
268 | result = self._resolve_url( | |
269 | 'lbry://' + display_id, display_id, 'channel') | |
270 | claim_id = result['claim_id'] | |
271 | qs = parse_qs(url) | |
272 | content = qs.get('content', [None])[0] | |
273 | params = { | |
274 | 'fee_amount': qs.get('fee_amount', ['>=0'])[0], | |
275 | 'order_by': { | |
276 | 'new': ['release_time'], | |
277 | 'top': ['effective_amount'], | |
278 | 'trending': ['trending_group', 'trending_mixed'], | |
279 | }[qs.get('order', ['new'])[0]], | |
280 | 'stream_types': [content] if content in ['audio', 'video'] else self._SUPPORTED_STREAM_TYPES, | |
281 | } | |
282 | duration = qs.get('duration', [None])[0] | |
283 | if duration: | |
284 | params['duration'] = { | |
285 | 'long': '>=1200', | |
286 | 'short': '<=240', | |
287 | }[duration] | |
288 | language = qs.get('language', ['all'])[0] | |
289 | if language != 'all': | |
290 | languages = [language] | |
291 | if language == 'en': | |
292 | languages.append('none') | |
293 | params['any_languages'] = languages | |
294 | entries = OnDemandPagedList( | |
295 | functools.partial(self._fetch_page, claim_id, url, params), | |
296 | self._PAGE_SIZE) | |
297 | result_value = result.get('value') or {} | |
298 | return self.playlist_result( | |
299 | entries, claim_id, result_value.get('title'), | |
300 | result_value.get('description')) |