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