lbry.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. determine_ext,
  8. ExtractorError,
  9. int_or_none,
  10. mimetype2ext,
  11. try_get,
  12. urljoin,
  13. )
  14. class LBRYIE(InfoExtractor):
  15. IE_NAME = 'lbry.tv'
  16. _CLAIM_ID_REGEX = r'[0-9a-f]{1,40}'
  17. _VALID_URL = r'https?://(?:www\.)?(?:lbry\.tv|odysee\.com)/(?P<id>@[^:]+:{0}/[^:]+:{0}|[^:]+:{0}|\$/embed/[^/]+/{0})'.format(_CLAIM_ID_REGEX)
  18. _TESTS = [{
  19. # Video
  20. 'url': 'https://lbry.tv/@Mantega:1/First-day-LBRY:1',
  21. 'md5': '65bd7ec1f6744ada55da8e4c48a2edf9',
  22. 'info_dict': {
  23. 'id': '17f983b61f53091fb8ea58a9c56804e4ff8cff4d',
  24. 'ext': 'mp4',
  25. 'title': 'First day in LBRY? Start HERE!',
  26. 'description': 'md5:f6cb5c704b332d37f5119313c2c98f51',
  27. 'timestamp': 1595694354,
  28. 'upload_date': '20200725',
  29. }
  30. }, {
  31. # Audio
  32. 'url': 'https://lbry.tv/@LBRYFoundation:0/Episode-1:e',
  33. 'md5': 'c94017d3eba9b49ce085a8fad6b98d00',
  34. 'info_dict': {
  35. 'id': 'e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  36. 'ext': 'mp3',
  37. 'title': 'The LBRY Foundation Community Podcast Episode 1 - Introduction, Streaming on LBRY, Transcoding',
  38. 'description': 'md5:661ac4f1db09f31728931d7b88807a61',
  39. 'timestamp': 1591312601,
  40. 'upload_date': '20200604',
  41. 'tags': list,
  42. 'duration': 2570,
  43. 'channel': 'The LBRY Foundation',
  44. 'channel_id': '0ed629d2b9c601300cacf7eabe9da0be79010212',
  45. 'channel_url': 'https://lbry.tv/@LBRYFoundation:0ed629d2b9c601300cacf7eabe9da0be79010212',
  46. }
  47. }, {
  48. 'url': 'https://odysee.com/@BrodieRobertson:5/apple-is-tracking-everything-you-do-on:e',
  49. 'only_matching': True,
  50. }, {
  51. 'url': "https://odysee.com/@ScammerRevolts:b0/I-SYSKEY'D-THE-SAME-SCAMMERS-3-TIMES!:b",
  52. 'only_matching': True,
  53. }, {
  54. 'url': 'https://lbry.tv/Episode-1:e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  55. 'only_matching': True,
  56. }, {
  57. 'url': 'https://lbry.tv/$/embed/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  58. 'only_matching': True,
  59. }, {
  60. 'url': 'https://lbry.tv/Episode-1:e7',
  61. 'only_matching': True,
  62. }]
  63. def _call_api_proxy(self, method, display_id, params):
  64. return self._download_json(
  65. 'https://api.lbry.tv/api/v1/proxy', display_id,
  66. headers={'Content-Type': 'application/json-rpc'},
  67. data=json.dumps({
  68. 'method': method,
  69. 'params': params,
  70. }).encode())['result']
  71. def _real_extract(self, url):
  72. display_id = self._match_id(url)
  73. if display_id.startswith('$/embed/'):
  74. display_id = display_id[8:].replace('/', ':')
  75. else:
  76. display_id = display_id.replace(':', '#')
  77. uri = 'lbry://' + display_id
  78. result = self._call_api_proxy(
  79. 'resolve', display_id, {'urls': [uri]})[uri]
  80. result_value = result['value']
  81. if result_value.get('stream_type') not in ('video', 'audio'):
  82. raise ExtractorError('Unsupported URL', expected=True)
  83. claim_id = result['claim_id']
  84. title = result_value['title']
  85. streaming_url = self._call_api_proxy(
  86. 'get', claim_id, {'uri': uri})['streaming_url']
  87. source = result_value.get('source') or {}
  88. media = result_value.get('video') or result_value.get('audio') or {}
  89. signing_channel = result.get('signing_channel') or {}
  90. channel_name = signing_channel.get('name')
  91. channel_claim_id = signing_channel.get('claim_id')
  92. channel_url = None
  93. if channel_name and channel_claim_id:
  94. channel_url = urljoin(url, '/%s:%s' % (channel_name, channel_claim_id))
  95. return {
  96. 'id': claim_id,
  97. 'title': title,
  98. 'thumbnail': try_get(result_value, lambda x: x['thumbnail']['url'], compat_str),
  99. 'description': result_value.get('description'),
  100. 'license': result_value.get('license'),
  101. 'timestamp': int_or_none(result.get('timestamp')),
  102. 'tags': result_value.get('tags'),
  103. 'width': int_or_none(media.get('width')),
  104. 'height': int_or_none(media.get('height')),
  105. 'duration': int_or_none(media.get('duration')),
  106. 'channel': try_get(signing_channel, lambda x: x['value']['title']),
  107. 'channel_id': channel_claim_id,
  108. 'channel_url': channel_url,
  109. 'ext': determine_ext(source.get('name')) or mimetype2ext(source.get('media_type')),
  110. 'filesize': int_or_none(source.get('size')),
  111. 'url': streaming_url,
  112. }