logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

lbry.py (11266B)


  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. # HLS
  114. 'url': 'https://odysee.com/@gardeningincanada:b/plants-i-will-never-grow-again.-the:e',
  115. 'md5': 'fc82f45ea54915b1495dd7cb5cc1289f',
  116. 'info_dict': {
  117. 'id': 'e51671357333fe22ae88aad320bde2f6f96b1410',
  118. 'ext': 'mp4',
  119. 'title': 'PLANTS I WILL NEVER GROW AGAIN. THE BLACK LIST PLANTS FOR A CANADIAN GARDEN | Gardening in Canada 🍁',
  120. 'description': 'md5:9c539c6a03fb843956de61a4d5288d5e',
  121. 'timestamp': 1618254123,
  122. 'upload_date': '20210412',
  123. 'release_timestamp': 1618254002,
  124. 'release_date': '20210412',
  125. 'tags': list,
  126. 'duration': 554,
  127. 'channel': 'Gardening In Canada',
  128. 'channel_id': 'b8be0e93b423dad221abe29545fbe8ec36e806bc',
  129. 'channel_url': 'https://odysee.com/@gardeningincanada:b8be0e93b423dad221abe29545fbe8ec36e806bc',
  130. 'formats': 'mincount:3',
  131. }
  132. }, {
  133. 'url': 'https://odysee.com/@BrodieRobertson:5/apple-is-tracking-everything-you-do-on:e',
  134. 'only_matching': True,
  135. }, {
  136. 'url': "https://odysee.com/@ScammerRevolts:b0/I-SYSKEY'D-THE-SAME-SCAMMERS-3-TIMES!:b",
  137. 'only_matching': True,
  138. }, {
  139. 'url': 'https://lbry.tv/Episode-1:e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  140. 'only_matching': True,
  141. }, {
  142. 'url': 'https://lbry.tv/$/embed/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  143. 'only_matching': True,
  144. }, {
  145. 'url': 'https://lbry.tv/Episode-1:e7',
  146. 'only_matching': True,
  147. }, {
  148. 'url': 'https://lbry.tv/@LBRYFoundation/Episode-1',
  149. 'only_matching': True,
  150. }, {
  151. 'url': 'https://lbry.tv/$/download/Episode-1/e7d93d772bd87e2b62d5ab993c1c3ced86ebb396',
  152. 'only_matching': True,
  153. }, {
  154. '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',
  155. 'only_matching': True,
  156. }]
  157. def _real_extract(self, url):
  158. display_id = self._match_id(url)
  159. if display_id.startswith('$/'):
  160. display_id = display_id.split('/', 2)[-1].replace('/', ':')
  161. else:
  162. display_id = display_id.replace(':', '#')
  163. display_id = compat_urllib_parse_unquote(display_id)
  164. uri = 'lbry://' + display_id
  165. result = self._resolve_url(uri, display_id, 'stream')
  166. result_value = result['value']
  167. if result_value.get('stream_type') not in self._SUPPORTED_STREAM_TYPES:
  168. raise ExtractorError('Unsupported URL', expected=True)
  169. claim_id = result['claim_id']
  170. title = result_value['title']
  171. streaming_url = self._call_api_proxy(
  172. 'get', claim_id, {'uri': uri}, 'streaming url')['streaming_url']
  173. info = self._parse_stream(result, url)
  174. urlh = self._request_webpage(
  175. streaming_url, display_id, note='Downloading streaming redirect url info')
  176. if determine_ext(urlh.geturl()) == 'm3u8':
  177. info['formats'] = self._extract_m3u8_formats(
  178. urlh.geturl(), display_id, 'mp4', entry_protocol='m3u8_native',
  179. m3u8_id='hls')
  180. self._sort_formats(info['formats'])
  181. else:
  182. info['url'] = streaming_url
  183. info.update({
  184. 'id': claim_id,
  185. 'title': title,
  186. })
  187. return info
  188. class LBRYChannelIE(LBRYBaseIE):
  189. IE_NAME = 'lbry:channel'
  190. _VALID_URL = LBRYBaseIE._BASE_URL_REGEX + r'(?P<id>@%s)/?(?:[?#&]|$)' % LBRYBaseIE._OPT_CLAIM_ID
  191. _TESTS = [{
  192. 'url': 'https://lbry.tv/@LBRYFoundation:0',
  193. 'info_dict': {
  194. 'id': '0ed629d2b9c601300cacf7eabe9da0be79010212',
  195. 'title': 'The LBRY Foundation',
  196. 'description': 'Channel for the LBRY Foundation. Follow for updates and news.',
  197. },
  198. 'playlist_count': 29,
  199. }, {
  200. 'url': 'https://lbry.tv/@LBRYFoundation',
  201. 'only_matching': True,
  202. }]
  203. _PAGE_SIZE = 50
  204. def _fetch_page(self, claim_id, url, params, page):
  205. page += 1
  206. page_params = {
  207. 'channel_ids': [claim_id],
  208. 'claim_type': 'stream',
  209. 'no_totals': True,
  210. 'page': page,
  211. 'page_size': self._PAGE_SIZE,
  212. }
  213. page_params.update(params)
  214. result = self._call_api_proxy(
  215. 'claim_search', claim_id, page_params, 'page %d' % page)
  216. for item in (result.get('items') or []):
  217. stream_claim_name = item.get('name')
  218. stream_claim_id = item.get('claim_id')
  219. if not (stream_claim_name and stream_claim_id):
  220. continue
  221. info = self._parse_stream(item, url)
  222. info.update({
  223. '_type': 'url',
  224. 'id': stream_claim_id,
  225. 'title': try_get(item, lambda x: x['value']['title']),
  226. 'url': self._permanent_url(url, stream_claim_name, stream_claim_id),
  227. })
  228. yield info
  229. def _real_extract(self, url):
  230. display_id = self._match_id(url).replace(':', '#')
  231. result = self._resolve_url(
  232. 'lbry://' + display_id, display_id, 'channel')
  233. claim_id = result['claim_id']
  234. qs = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
  235. content = qs.get('content', [None])[0]
  236. params = {
  237. 'fee_amount': qs.get('fee_amount', ['>=0'])[0],
  238. 'order_by': {
  239. 'new': ['release_time'],
  240. 'top': ['effective_amount'],
  241. 'trending': ['trending_group', 'trending_mixed'],
  242. }[qs.get('order', ['new'])[0]],
  243. 'stream_types': [content] if content in ['audio', 'video'] else self._SUPPORTED_STREAM_TYPES,
  244. }
  245. duration = qs.get('duration', [None])[0]
  246. if duration:
  247. params['duration'] = {
  248. 'long': '>=1200',
  249. 'short': '<=240',
  250. }[duration]
  251. language = qs.get('language', ['all'])[0]
  252. if language != 'all':
  253. languages = [language]
  254. if language == 'en':
  255. languages.append('none')
  256. params['any_languages'] = languages
  257. entries = OnDemandPagedList(
  258. functools.partial(self._fetch_page, claim_id, url, params),
  259. self._PAGE_SIZE)
  260. result_value = result.get('value') or {}
  261. return self.playlist_result(
  262. entries, claim_id, result_value.get('title'),
  263. result_value.get('description'))