]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/lbry.py
[wimtv] Add extractor (#161)
[yt-dlp.git] / yt_dlp / extractor / lbry.py
CommitLineData
8bdd16b4 1# coding: utf-8
2from __future__ import unicode_literals
3
29f7c58a 4import functools
8bdd16b4 5import json
6
7from .common import InfoExtractor
a820dc72
RA
8from ..compat import (
9 compat_str,
10 compat_urllib_parse_unquote,
11)
8bdd16b4 12from ..utils import (
13 determine_ext,
14 ExtractorError,
15 int_or_none,
16 mimetype2ext,
29f7c58a 17 OnDemandPagedList,
8bdd16b4 18 try_get,
29f7c58a 19 urljoin,
8bdd16b4 20)
21
22
29f7c58a 23class LBRYBaseIE(InfoExtractor):
57db6a87 24 _BASE_URL_REGEX = r'(?:https?://(?:www\.)?(?:lbry\.tv|odysee\.com)/|lbry://)'
29f7c58a 25 _CLAIM_ID_REGEX = r'[0-9a-f]{1,40}'
57db6a87 26 _OPT_CLAIM_ID = '[^:/?#&]+(?:[:#]%s)?' % _CLAIM_ID_REGEX
29f7c58a 27 _SUPPORTED_STREAM_TYPES = ['video', 'audio']
28
29 def _call_api_proxy(self, method, display_id, params, resource):
30 return self._download_json(
31 'https://api.lbry.tv/api/v1/proxy',
32 display_id, 'Downloading %s JSON metadata' % resource,
33 headers={'Content-Type': 'application/json-rpc'},
34 data=json.dumps({
35 'method': method,
36 'params': params,
37 }).encode())['result']
38
39 def _resolve_url(self, url, display_id, resource):
40 return self._call_api_proxy(
41 'resolve', display_id, {'urls': url}, resource)[url]
42
43 def _permanent_url(self, url, claim_name, claim_id):
57db6a87 44 return urljoin(
45 url.replace('lbry://', 'https://lbry.tv/'),
46 '/%s:%s' % (claim_name, claim_id))
29f7c58a 47
48 def _parse_stream(self, stream, url):
49 stream_value = stream.get('value') or {}
50 stream_type = stream_value.get('stream_type')
51 source = stream_value.get('source') or {}
52 media = stream_value.get(stream_type) or {}
53 signing_channel = stream.get('signing_channel') or {}
54 channel_name = signing_channel.get('name')
55 channel_claim_id = signing_channel.get('claim_id')
56 channel_url = None
57 if channel_name and channel_claim_id:
58 channel_url = self._permanent_url(url, channel_name, channel_claim_id)
59
60 info = {
61 'thumbnail': try_get(stream_value, lambda x: x['thumbnail']['url'], compat_str),
62 'description': stream_value.get('description'),
63 'license': stream_value.get('license'),
64 'timestamp': int_or_none(stream.get('timestamp')),
65 'tags': stream_value.get('tags'),
66 'duration': int_or_none(media.get('duration')),
67 'channel': try_get(signing_channel, lambda x: x['value']['title']),
68 'channel_id': channel_claim_id,
69 'channel_url': channel_url,
70 'ext': determine_ext(source.get('name')) or mimetype2ext(source.get('media_type')),
71 'filesize': int_or_none(source.get('size')),
72 }
73 if stream_type == 'audio':
74 info['vcodec'] = 'none'
75 else:
76 info.update({
77 'width': int_or_none(media.get('width')),
78 'height': int_or_none(media.get('height')),
79 })
80 return info
81
82
83class LBRYIE(LBRYBaseIE):
84 IE_NAME = 'lbry'
85 _VALID_URL = LBRYBaseIE._BASE_URL_REGEX + r'(?P<id>\$/[^/]+/[^/]+/{1}|@{0}/{0}|(?!@){0})'.format(LBRYBaseIE._OPT_CLAIM_ID, LBRYBaseIE._CLAIM_ID_REGEX)
8bdd16b4 86 _TESTS = [{
87 # Video
88 'url': 'https://lbry.tv/@Mantega:1/First-day-LBRY:1',
89 'md5': '65bd7ec1f6744ada55da8e4c48a2edf9',
90 'info_dict': {
91 'id': '17f983b61f53091fb8ea58a9c56804e4ff8cff4d',
92 'ext': 'mp4',
93 'title': 'First day in LBRY? Start HERE!',
94 'description': 'md5:f6cb5c704b332d37f5119313c2c98f51',
95 'timestamp': 1595694354,
96 'upload_date': '20200725',
29f7c58a 97 'width': 1280,
98 'height': 720,
8bdd16b4 99 }
100 }, {
101 # Audio
102 'url': 'https://lbry.tv/@LBRYFoundation:0/Episode-1:e',
103 'md5': 'c94017d3eba9b49ce085a8fad6b98d00',
104 'info_dict': {
105 'id': 'e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
106 'ext': 'mp3',
107 'title': 'The LBRY Foundation Community Podcast Episode 1 - Introduction, Streaming on LBRY, Transcoding',
108 'description': 'md5:661ac4f1db09f31728931d7b88807a61',
109 'timestamp': 1591312601,
110 'upload_date': '20200604',
29f7c58a 111 'tags': list,
112 'duration': 2570,
113 'channel': 'The LBRY Foundation',
114 'channel_id': '0ed629d2b9c601300cacf7eabe9da0be79010212',
115 'channel_url': 'https://lbry.tv/@LBRYFoundation:0ed629d2b9c601300cacf7eabe9da0be79010212',
116 'vcodec': 'none',
8bdd16b4 117 }
118 }, {
119 'url': 'https://odysee.com/@BrodieRobertson:5/apple-is-tracking-everything-you-do-on:e',
120 'only_matching': True,
70c5802b 121 }, {
122 'url': "https://odysee.com/@ScammerRevolts:b0/I-SYSKEY'D-THE-SAME-SCAMMERS-3-TIMES!:b",
123 'only_matching': True,
29f7c58a 124 }, {
125 'url': 'https://lbry.tv/Episode-1:e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
126 'only_matching': True,
127 }, {
128 'url': 'https://lbry.tv/$/embed/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
129 'only_matching': True,
130 }, {
131 'url': 'https://lbry.tv/Episode-1:e7',
132 'only_matching': True,
133 }, {
134 'url': 'https://lbry.tv/@LBRYFoundation/Episode-1',
135 'only_matching': True,
136 }, {
137 'url': 'https://lbry.tv/$/download/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
138 'only_matching': True,
a820dc72
RA
139 }, {
140 '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',
141 'only_matching': True,
57db6a87 142 }, {
143 'url': 'lbry://@lbry#3f/odysee#7',
144 'only_matching': True,
8bdd16b4 145 }]
146
8bdd16b4 147 def _real_extract(self, url):
29f7c58a 148 display_id = self._match_id(url)
149 if display_id.startswith('$/'):
150 display_id = display_id.split('/', 2)[-1].replace('/', ':')
151 else:
152 display_id = display_id.replace(':', '#')
a820dc72 153 display_id = compat_urllib_parse_unquote(display_id)
8bdd16b4 154 uri = 'lbry://' + display_id
29f7c58a 155 result = self._resolve_url(uri, display_id, 'stream')
8bdd16b4 156 result_value = result['value']
29f7c58a 157 if result_value.get('stream_type') not in self._SUPPORTED_STREAM_TYPES:
8bdd16b4 158 raise ExtractorError('Unsupported URL', expected=True)
29f7c58a 159 claim_id = result['claim_id']
160 title = result_value['title']
8bdd16b4 161 streaming_url = self._call_api_proxy(
29f7c58a 162 'get', claim_id, {'uri': uri}, 'streaming url')['streaming_url']
163 info = self._parse_stream(result, url)
164 info.update({
165 'id': claim_id,
166 'title': title,
8bdd16b4 167 'url': streaming_url,
29f7c58a 168 })
169 return info
170
171
172class LBRYChannelIE(LBRYBaseIE):
173 IE_NAME = 'lbry:channel'
57db6a87 174 _VALID_URL = LBRYBaseIE._BASE_URL_REGEX + r'(?P<id>@%s)/?(?:[?&]|$)' % LBRYBaseIE._OPT_CLAIM_ID
29f7c58a 175 _TESTS = [{
176 'url': 'https://lbry.tv/@LBRYFoundation:0',
177 'info_dict': {
178 'id': '0ed629d2b9c601300cacf7eabe9da0be79010212',
179 'title': 'The LBRY Foundation',
180 'description': 'Channel for the LBRY Foundation. Follow for updates and news.',
181 },
182 'playlist_count': 29,
183 }, {
184 'url': 'https://lbry.tv/@LBRYFoundation',
185 'only_matching': True,
57db6a87 186 }, {
187 'url': 'lbry://@lbry#3f',
188 'only_matching': True,
29f7c58a 189 }]
190 _PAGE_SIZE = 50
191
192 def _fetch_page(self, claim_id, url, page):
193 page += 1
194 result = self._call_api_proxy(
195 'claim_search', claim_id, {
196 'channel_ids': [claim_id],
197 'claim_type': 'stream',
198 'no_totals': True,
199 'page': page,
200 'page_size': self._PAGE_SIZE,
201 'stream_types': self._SUPPORTED_STREAM_TYPES,
202 }, 'page %d' % page)
203 for item in (result.get('items') or []):
204 stream_claim_name = item.get('name')
205 stream_claim_id = item.get('claim_id')
206 if not (stream_claim_name and stream_claim_id):
207 continue
208
209 info = self._parse_stream(item, url)
210 info.update({
211 '_type': 'url',
212 'id': stream_claim_id,
213 'title': try_get(item, lambda x: x['value']['title']),
214 'url': self._permanent_url(url, stream_claim_name, stream_claim_id),
215 })
216 yield info
217
218 def _real_extract(self, url):
219 display_id = self._match_id(url).replace(':', '#')
220 result = self._resolve_url(
221 'lbry://' + display_id, display_id, 'channel')
222 claim_id = result['claim_id']
223 entries = OnDemandPagedList(
224 functools.partial(self._fetch_page, claim_id, url),
225 self._PAGE_SIZE)
226 result_value = result.get('value') or {}
227 return self.playlist_result(
228 entries, claim_id, result_value.get('title'),
229 result_value.get('description'))