lbry.py 9.7 KB

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