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