logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

lbry.py (17659B)


  1. import functools
  2. import json
  3. import re
  4. import urllib.parse
  5. from .common import InfoExtractor
  6. from ..networking import HEADRequest
  7. from ..utils import (
  8. ExtractorError,
  9. OnDemandPagedList,
  10. UnsupportedError,
  11. determine_ext,
  12. int_or_none,
  13. mimetype2ext,
  14. parse_qs,
  15. traverse_obj,
  16. try_get,
  17. url_or_none,
  18. urlhandle_detect_ext,
  19. urljoin,
  20. )
  21. class LBRYBaseIE(InfoExtractor):
  22. _BASE_URL_REGEX = r'(?x)(?:https?://(?:www\.)?(?:lbry\.tv|odysee\.com)/|lbry://)'
  23. _CLAIM_ID_REGEX = r'[0-9a-f]{1,40}'
  24. _OPT_CLAIM_ID = f'[^$@:/?#&]+(?:[:#]{_CLAIM_ID_REGEX})?'
  25. _SUPPORTED_STREAM_TYPES = ['video', 'audio']
  26. _PAGE_SIZE = 50
  27. def _call_api_proxy(self, method, display_id, params, resource):
  28. headers = {'Content-Type': 'application/json-rpc'}
  29. token = try_get(self._get_cookies('https://odysee.com'), lambda x: x['auth_token'].value)
  30. if token:
  31. headers['x-lbry-auth-token'] = token
  32. response = self._download_json(
  33. 'https://api.lbry.tv/api/v1/proxy',
  34. display_id, f'Downloading {resource} JSON metadata',
  35. headers=headers,
  36. data=json.dumps({
  37. 'method': method,
  38. 'params': params,
  39. }).encode())
  40. err = response.get('error')
  41. if err:
  42. raise ExtractorError(
  43. f'{self.IE_NAME} said: {err.get("code")} - {err.get("message")}', expected=True)
  44. return response['result']
  45. def _resolve_url(self, url, display_id, resource):
  46. return self._call_api_proxy(
  47. 'resolve', display_id, {'urls': url}, resource)[url]
  48. def _permanent_url(self, url, claim_name, claim_id):
  49. return urljoin(
  50. url.replace('lbry://', 'https://lbry.tv/'),
  51. f'/{claim_name}:{claim_id}')
  52. def _parse_stream(self, stream, url):
  53. stream_type = traverse_obj(stream, ('value', 'stream_type', {str}))
  54. info = traverse_obj(stream, {
  55. 'title': ('value', 'title', {str}),
  56. 'thumbnail': ('value', 'thumbnail', 'url', {url_or_none}),
  57. 'description': ('value', 'description', {str}),
  58. 'license': ('value', 'license', {str}),
  59. 'timestamp': ('timestamp', {int_or_none}),
  60. 'release_timestamp': ('value', 'release_time', {int_or_none}),
  61. 'tags': ('value', 'tags', ..., filter),
  62. 'duration': ('value', stream_type, 'duration', {int_or_none}),
  63. 'channel': ('signing_channel', 'value', 'title', {str}),
  64. 'channel_id': ('signing_channel', 'claim_id', {str}),
  65. 'uploader_id': ('signing_channel', 'name', {str}),
  66. })
  67. if info.get('uploader_id') and info.get('channel_id'):
  68. info['channel_url'] = self._permanent_url(url, info['uploader_id'], info['channel_id'])
  69. return info
  70. def _fetch_page(self, display_id, url, params, page):
  71. page += 1
  72. page_params = {
  73. 'no_totals': True,
  74. 'page': page,
  75. 'page_size': self._PAGE_SIZE,
  76. **params,
  77. }
  78. result = self._call_api_proxy(
  79. 'claim_search', display_id, page_params, f'page {page}')
  80. for item in traverse_obj(result, ('items', lambda _, v: v['name'] and v['claim_id'])):
  81. yield {
  82. **self._parse_stream(item, url),
  83. '_type': 'url',
  84. 'id': item['claim_id'],
  85. 'url': self._permanent_url(url, item['name'], item['claim_id']),
  86. }
  87. def _playlist_entries(self, url, display_id, claim_param, metadata):
  88. qs = parse_qs(url)
  89. content = qs.get('content', [None])[0]
  90. params = {
  91. 'fee_amount': qs.get('fee_amount', ['>=0'])[0],
  92. 'order_by': {
  93. 'new': ['release_time'],
  94. 'top': ['effective_amount'],
  95. 'trending': ['trending_group', 'trending_mixed'],
  96. }[qs.get('order', ['new'])[0]],
  97. 'claim_type': 'stream',
  98. 'stream_types': [content] if content in ['audio', 'video'] else self._SUPPORTED_STREAM_TYPES,
  99. **claim_param,
  100. }
  101. duration = qs.get('duration', [None])[0]
  102. if duration:
  103. params['duration'] = {
  104. 'long': '>=1200',
  105. 'short': '<=240',
  106. }[duration]
  107. language = qs.get('language', ['all'])[0]
  108. if language != 'all':
  109. languages = [language]
  110. if language == 'en':
  111. languages.append('none')
  112. params['any_languages'] = languages
  113. entries = OnDemandPagedList(
  114. functools.partial(self._fetch_page, display_id, url, params),
  115. self._PAGE_SIZE)
  116. return self.playlist_result(
  117. entries, display_id, **traverse_obj(metadata, ('value', {
  118. 'title': 'title',
  119. 'description': 'description',
  120. })))
  121. class LBRYIE(LBRYBaseIE):
  122. IE_NAME = 'lbry'
  123. IE_DESC = 'odysee.com'
  124. _VALID_URL = LBRYBaseIE._BASE_URL_REGEX + rf'''
  125. (?:\$/(?:download|embed)/)?
  126. (?P<id>
  127. [^$@:/?#]+/{LBRYBaseIE._CLAIM_ID_REGEX}
  128. |(?:@{LBRYBaseIE._OPT_CLAIM_ID}/)?{LBRYBaseIE._OPT_CLAIM_ID}
  129. )'''
  130. _TESTS = [{
  131. # Video
  132. 'url': 'https://lbry.tv/@Mantega:1/First-day-LBRY:1',
  133. 'md5': '65bd7ec1f6744ada55da8e4c48a2edf9',
  134. 'info_dict': {
  135. 'id': '17f983b61f53091fb8ea58a9c56804e4ff8cff4d',
  136. 'ext': 'mp4',
  137. 'title': 'First day in LBRY? Start HERE!',
  138. 'description': 'md5:f6cb5c704b332d37f5119313c2c98f51',
  139. 'timestamp': 1595694354,
  140. 'upload_date': '20200725',
  141. 'release_timestamp': 1595340697,
  142. 'release_date': '20200721',
  143. 'width': 1280,
  144. 'height': 720,
  145. 'thumbnail': 'https://spee.ch/7/67f2d809c263288c.png',
  146. 'license': 'None',
  147. 'uploader_id': '@Mantega',
  148. 'duration': 346,
  149. 'channel': 'LBRY/Odysee rats united!!!',
  150. 'channel_id': '1c8ad6a2ab4e889a71146ae4deeb23bb92dab627',
  151. 'channel_url': 'https://lbry.tv/@Mantega:1c8ad6a2ab4e889a71146ae4deeb23bb92dab627',
  152. 'tags': [
  153. 'first day in lbry',
  154. 'lbc',
  155. 'lbry',
  156. 'start',
  157. 'tutorial',
  158. ],
  159. },
  160. }, {
  161. # Audio
  162. 'url': 'https://lbry.tv/@LBRYFoundation:0/Episode-1:e',
  163. 'md5': 'c94017d3eba9b49ce085a8fad6b98d00',
  164. 'info_dict': {
  165. 'id': 'e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  166. 'ext': 'mp3',
  167. 'title': 'The LBRY Foundation Community Podcast Episode 1 - Introduction, Streaming on LBRY, Transcoding',
  168. 'description': 'md5:661ac4f1db09f31728931d7b88807a61',
  169. 'timestamp': 1591312601,
  170. 'upload_date': '20200604',
  171. 'release_timestamp': 1591312421,
  172. 'release_date': '20200604',
  173. 'tags': list,
  174. 'duration': 2570,
  175. 'channel': 'The LBRY Foundation',
  176. 'channel_id': '0ed629d2b9c601300cacf7eabe9da0be79010212',
  177. 'channel_url': 'https://lbry.tv/@LBRYFoundation:0ed629d2b9c601300cacf7eabe9da0be79010212',
  178. 'vcodec': 'none',
  179. 'thumbnail': 'https://spee.ch/d/0bc63b0e6bf1492d.png',
  180. 'license': 'None',
  181. 'uploader_id': '@LBRYFoundation',
  182. },
  183. }, {
  184. 'url': 'https://odysee.com/@gardeningincanada:b/plants-i-will-never-grow-again.-the:e',
  185. 'md5': 'c35fac796f62a14274b4dc2addb5d0ba',
  186. 'info_dict': {
  187. 'id': 'e51671357333fe22ae88aad320bde2f6f96b1410',
  188. 'ext': 'mp4',
  189. 'title': 'PLANTS I WILL NEVER GROW AGAIN. THE BLACK LIST PLANTS FOR A CANADIAN GARDEN | Gardening in Canada 🍁',
  190. 'description': 'md5:9c539c6a03fb843956de61a4d5288d5e',
  191. 'timestamp': 1618254123,
  192. 'upload_date': '20210412',
  193. 'release_timestamp': 1618254002,
  194. 'release_date': '20210412',
  195. 'tags': list,
  196. 'duration': 554,
  197. 'channel': 'Gardening In Canada',
  198. 'channel_id': 'b8be0e93b423dad221abe29545fbe8ec36e806bc',
  199. 'channel_url': 'https://odysee.com/@gardeningincanada:b8be0e93b423dad221abe29545fbe8ec36e806bc',
  200. 'uploader_id': '@gardeningincanada',
  201. 'formats': 'mincount:3',
  202. 'thumbnail': 'https://thumbnails.lbry.com/AgHSc_HzrrE',
  203. 'license': 'Copyrighted (contact publisher)',
  204. },
  205. }, {
  206. # HLS live stream (might expire)
  207. 'url': 'https://odysee.com/@RT:fd/livestream_RT:d',
  208. 'info_dict': {
  209. 'id': 'fdd11cb3ab75f95efb7b3bc2d726aa13ac915b66',
  210. 'ext': 'mp4',
  211. 'live_status': 'is_live',
  212. 'title': 'startswith:RT News | Livestream 24/7',
  213. 'description': 'md5:fe68d0056dfe79c1a6b8ce8c34d5f6fa',
  214. 'timestamp': int,
  215. 'upload_date': str,
  216. 'release_timestamp': int,
  217. 'release_date': str,
  218. 'tags': list,
  219. 'channel': 'RT',
  220. 'channel_id': 'fdd11cb3ab75f95efb7b3bc2d726aa13ac915b66',
  221. 'channel_url': 'https://odysee.com/@RT:fdd11cb3ab75f95efb7b3bc2d726aa13ac915b66',
  222. 'formats': 'mincount:1',
  223. 'thumbnail': 'startswith:https://thumb',
  224. 'license': 'None',
  225. 'uploader_id': '@RT',
  226. },
  227. 'params': {'skip_download': True},
  228. }, {
  229. # original quality format w/higher resolution than HLS formats
  230. 'url': 'https://odysee.com/@wickedtruths:2/Biotechnological-Invasion-of-Skin-(April-2023):4',
  231. 'md5': '305b0b3b369bde1b984961f005b67193',
  232. 'info_dict': {
  233. 'id': '41fbfe805eb73c8d3012c0c49faa0f563274f634',
  234. 'ext': 'mp4',
  235. 'title': 'Biotechnological Invasion of Skin (April 2023)',
  236. 'description': 'md5:fe28689db2cb7ba3436d819ac3ffc378',
  237. 'channel': 'Wicked Truths',
  238. 'channel_id': '23d2bbf856b0ceed5b1d7c5960bcc72da5a20cb0',
  239. 'channel_url': 'https://odysee.com/@wickedtruths:23d2bbf856b0ceed5b1d7c5960bcc72da5a20cb0',
  240. 'uploader_id': '@wickedtruths',
  241. 'timestamp': 1695114347,
  242. 'upload_date': '20230919',
  243. 'release_timestamp': 1685617473,
  244. 'release_date': '20230601',
  245. 'duration': 1063,
  246. 'thumbnail': 'https://thumbs.odycdn.com/4e6d39da4df0cfdad45f64e253a15959.webp',
  247. 'tags': ['smart skin surveillance', 'biotechnology invasion of skin', 'morgellons'],
  248. 'license': 'None',
  249. 'protocol': 'https', # test for direct mp4 download
  250. },
  251. }, {
  252. 'url': 'https://odysee.com/@BrodieRobertson:5/apple-is-tracking-everything-you-do-on:e',
  253. 'only_matching': True,
  254. }, {
  255. 'url': 'https://odysee.com/@ScammerRevolts:b0/I-SYSKEY\'D-THE-SAME-SCAMMERS-3-TIMES!:b',
  256. 'only_matching': True,
  257. }, {
  258. 'url': 'https://lbry.tv/Episode-1:e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  259. 'only_matching': True,
  260. }, {
  261. 'url': 'https://lbry.tv/$/embed/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  262. 'only_matching': True,
  263. }, {
  264. 'url': 'https://lbry.tv/Episode-1:e7',
  265. 'only_matching': True,
  266. }, {
  267. 'url': 'https://lbry.tv/@LBRYFoundation/Episode-1',
  268. 'only_matching': True,
  269. }, {
  270. 'url': 'https://lbry.tv/$/download/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  271. 'only_matching': True,
  272. }, {
  273. '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',
  274. 'only_matching': True,
  275. }, {
  276. 'url': 'lbry://@lbry#3f/odysee#7',
  277. 'only_matching': True,
  278. }]
  279. def _real_extract(self, url):
  280. display_id = self._match_id(url)
  281. if display_id.startswith('@'):
  282. display_id = display_id.replace(':', '#')
  283. else:
  284. display_id = display_id.replace('/', ':')
  285. display_id = urllib.parse.unquote(display_id)
  286. uri = 'lbry://' + display_id
  287. result = self._resolve_url(uri, display_id, 'stream')
  288. headers = {'Referer': 'https://odysee.com/'}
  289. formats = []
  290. stream_type = traverse_obj(result, ('value', 'stream_type', {str}))
  291. if stream_type in self._SUPPORTED_STREAM_TYPES:
  292. claim_id, is_live = result['claim_id'], False
  293. streaming_url = self._call_api_proxy(
  294. 'get', claim_id, {'uri': uri}, 'streaming url')['streaming_url']
  295. # GET request to v3 API returns original video/audio file if available
  296. direct_url = re.sub(r'/api/v\d+/', '/api/v3/', streaming_url)
  297. urlh = self._request_webpage(
  298. direct_url, display_id, 'Checking for original quality', headers=headers, fatal=False)
  299. if urlh and urlhandle_detect_ext(urlh) != 'm3u8':
  300. formats.append({
  301. 'url': direct_url,
  302. 'format_id': 'original',
  303. 'quality': 1,
  304. **traverse_obj(result, ('value', {
  305. 'ext': ('source', (('name', {determine_ext}), ('media_type', {mimetype2ext}))),
  306. 'filesize': ('source', 'size', {int_or_none}),
  307. 'width': ('video', 'width', {int_or_none}),
  308. 'height': ('video', 'height', {int_or_none}),
  309. }), get_all=False),
  310. 'vcodec': 'none' if stream_type == 'audio' else None,
  311. })
  312. # HEAD request returns redirect response to m3u8 URL if available
  313. final_url = self._request_webpage(
  314. HEADRequest(streaming_url), display_id, headers=headers,
  315. note='Downloading streaming redirect url info').url
  316. elif result.get('value_type') == 'stream':
  317. claim_id, is_live = result['signing_channel']['claim_id'], True
  318. live_data = self._download_json(
  319. 'https://api.odysee.live/livestream/is_live', claim_id,
  320. query={'channel_claim_id': claim_id},
  321. note='Downloading livestream JSON metadata')['data']
  322. final_url = live_data.get('VideoURL')
  323. # Upcoming videos may still give VideoURL
  324. if not live_data.get('Live'):
  325. final_url = None
  326. self.raise_no_formats('This stream is not live', True, claim_id)
  327. else:
  328. raise UnsupportedError(url)
  329. if determine_ext(final_url) == 'm3u8':
  330. formats.extend(self._extract_m3u8_formats(
  331. final_url, display_id, 'mp4', m3u8_id='hls', live=is_live, headers=headers))
  332. return {
  333. **self._parse_stream(result, url),
  334. 'id': claim_id,
  335. 'formats': formats,
  336. 'is_live': is_live,
  337. 'http_headers': headers,
  338. }
  339. class LBRYChannelIE(LBRYBaseIE):
  340. IE_NAME = 'lbry:channel'
  341. IE_DESC = 'odysee.com channels'
  342. _VALID_URL = LBRYBaseIE._BASE_URL_REGEX + rf'(?P<id>@{LBRYBaseIE._OPT_CLAIM_ID})/?(?:[?&]|$)'
  343. _TESTS = [{
  344. 'url': 'https://lbry.tv/@LBRYFoundation:0',
  345. 'info_dict': {
  346. 'id': '0ed629d2b9c601300cacf7eabe9da0be79010212',
  347. 'title': 'The LBRY Foundation',
  348. 'description': 'Channel for the LBRY Foundation. Follow for updates and news.',
  349. },
  350. 'playlist_mincount': 29,
  351. }, {
  352. 'url': 'https://lbry.tv/@LBRYFoundation',
  353. 'only_matching': True,
  354. }, {
  355. 'url': 'lbry://@lbry#3f',
  356. 'only_matching': True,
  357. }]
  358. def _real_extract(self, url):
  359. display_id = self._match_id(url).replace(':', '#')
  360. result = self._resolve_url(f'lbry://{display_id}', display_id, 'channel')
  361. claim_id = result['claim_id']
  362. return self._playlist_entries(url, claim_id, {'channel_ids': [claim_id]}, result)
  363. class LBRYPlaylistIE(LBRYBaseIE):
  364. IE_NAME = 'lbry:playlist'
  365. IE_DESC = 'odysee.com playlists'
  366. _VALID_URL = LBRYBaseIE._BASE_URL_REGEX + r'\$/(?:play)?list/(?P<id>[0-9a-f-]+)'
  367. _TESTS = [{
  368. 'url': 'https://odysee.com/$/playlist/ffef782f27486f0ac138bde8777f72ebdd0548c2',
  369. 'info_dict': {
  370. 'id': 'ffef782f27486f0ac138bde8777f72ebdd0548c2',
  371. 'title': 'Théâtre Classique',
  372. 'description': 'Théâtre Classique',
  373. },
  374. 'playlist_mincount': 4,
  375. }, {
  376. 'url': 'https://odysee.com/$/list/9c6658b3dd21e4f2a0602d523a13150e2b48b770',
  377. 'info_dict': {
  378. 'id': '9c6658b3dd21e4f2a0602d523a13150e2b48b770',
  379. 'title': 'Social Media Exposed',
  380. 'description': 'md5:98af97317aacd5b85d595775ea37d80e',
  381. },
  382. 'playlist_mincount': 34,
  383. }, {
  384. 'url': 'https://odysee.com/$/playlist/938fb11d-215f-4d1c-ad64-723954df2184',
  385. 'info_dict': {
  386. 'id': '938fb11d-215f-4d1c-ad64-723954df2184',
  387. },
  388. 'playlist_mincount': 1000,
  389. }]
  390. def _real_extract(self, url):
  391. display_id = self._match_id(url)
  392. result = traverse_obj(self._call_api_proxy('claim_search', display_id, {
  393. 'claim_ids': [display_id],
  394. 'no_totals': True,
  395. 'page': 1,
  396. 'page_size': self._PAGE_SIZE,
  397. }, 'playlist'), ('items', 0))
  398. claim_param = {'claim_ids': traverse_obj(result, ('value', 'claims', ..., {str}))}
  399. return self._playlist_entries(url, display_id, claim_param, result)