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