lbry.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. )
  13. class LBRYIE(InfoExtractor):
  14. IE_NAME = 'lbry.tv'
  15. _VALID_URL = r'https?://(?:www\.)?(?:lbry\.tv|odysee\.com)/(?P<id>@[0-9a-zA-Z-]+:[0-9a-z]+/[0-9a-zA-Z().-]+:[0-9a-z])'
  16. _TESTS = [{
  17. # Video
  18. 'url': 'https://lbry.tv/@Mantega:1/First-day-LBRY:1',
  19. 'md5': '65bd7ec1f6744ada55da8e4c48a2edf9',
  20. 'info_dict': {
  21. 'id': '17f983b61f53091fb8ea58a9c56804e4ff8cff4d',
  22. 'ext': 'mp4',
  23. 'title': 'First day in LBRY? Start HERE!',
  24. 'description': 'md5:f6cb5c704b332d37f5119313c2c98f51',
  25. 'timestamp': 1595694354,
  26. 'upload_date': '20200725',
  27. }
  28. }, {
  29. # Audio
  30. 'url': 'https://lbry.tv/@LBRYFoundation:0/Episode-1:e',
  31. 'md5': 'c94017d3eba9b49ce085a8fad6b98d00',
  32. 'info_dict': {
  33. 'id': 'e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  34. 'ext': 'mp3',
  35. 'title': 'The LBRY Foundation Community Podcast Episode 1 - Introduction, Streaming on LBRY, Transcoding',
  36. 'description': 'md5:661ac4f1db09f31728931d7b88807a61',
  37. 'timestamp': 1591312601,
  38. 'upload_date': '20200604',
  39. }
  40. }, {
  41. 'url': 'https://odysee.com/@BrodieRobertson:5/apple-is-tracking-everything-you-do-on:e',
  42. 'only_matching': True,
  43. }]
  44. def _call_api_proxy(self, method, display_id, params):
  45. return self._download_json(
  46. 'https://api.lbry.tv/api/v1/proxy', display_id,
  47. headers={'Content-Type': 'application/json-rpc'},
  48. data=json.dumps({
  49. 'method': method,
  50. 'params': params,
  51. }).encode())['result']
  52. def _real_extract(self, url):
  53. display_id = self._match_id(url).replace(':', '#')
  54. uri = 'lbry://' + display_id
  55. result = self._call_api_proxy(
  56. 'resolve', display_id, {'urls': [uri]})[uri]
  57. result_value = result['value']
  58. if result_value.get('stream_type') not in ('video', 'audio'):
  59. raise ExtractorError('Unsupported URL', expected=True)
  60. streaming_url = self._call_api_proxy(
  61. 'get', display_id, {'uri': uri})['streaming_url']
  62. source = result_value.get('source') or {}
  63. media = result_value.get('video') or result_value.get('audio') or {}
  64. signing_channel = result_value.get('signing_channel') or {}
  65. return {
  66. 'id': result['claim_id'],
  67. 'title': result_value['title'],
  68. 'thumbnail': try_get(result_value, lambda x: x['thumbnail']['url'], compat_str),
  69. 'description': result_value.get('description'),
  70. 'license': result_value.get('license'),
  71. 'timestamp': int_or_none(result.get('timestamp')),
  72. 'tags': result_value.get('tags'),
  73. 'width': int_or_none(media.get('width')),
  74. 'height': int_or_none(media.get('height')),
  75. 'duration': int_or_none(media.get('duration')),
  76. 'channel': signing_channel.get('name'),
  77. 'channel_id': signing_channel.get('claim_id'),
  78. 'ext': determine_ext(source.get('name')) or mimetype2ext(source.get('media_type')),
  79. 'filesize': int_or_none(source.get('size')),
  80. 'url': streaming_url,
  81. }