lbry.py 8.7 KB

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