logo

youtube-dl

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

gaia.py (4700B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_str,
  7. compat_urllib_parse_unquote,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. int_or_none,
  12. str_or_none,
  13. strip_or_none,
  14. try_get,
  15. urlencode_postdata,
  16. )
  17. class GaiaIE(InfoExtractor):
  18. _VALID_URL = r'https?://(?:www\.)?gaia\.com/video/(?P<id>[^/?]+).*?\bfullplayer=(?P<type>feature|preview)'
  19. _TESTS = [{
  20. 'url': 'https://www.gaia.com/video/connecting-universal-consciousness?fullplayer=feature',
  21. 'info_dict': {
  22. 'id': '89356',
  23. 'ext': 'mp4',
  24. 'title': 'Connecting with Universal Consciousness',
  25. 'description': 'md5:844e209ad31b7d31345f5ed689e3df6f',
  26. 'upload_date': '20151116',
  27. 'timestamp': 1447707266,
  28. 'duration': 936,
  29. },
  30. 'params': {
  31. # m3u8 download
  32. 'skip_download': True,
  33. },
  34. }, {
  35. 'url': 'https://www.gaia.com/video/connecting-universal-consciousness?fullplayer=preview',
  36. 'info_dict': {
  37. 'id': '89351',
  38. 'ext': 'mp4',
  39. 'title': 'Connecting with Universal Consciousness',
  40. 'description': 'md5:844e209ad31b7d31345f5ed689e3df6f',
  41. 'upload_date': '20151116',
  42. 'timestamp': 1447707266,
  43. 'duration': 53,
  44. },
  45. 'params': {
  46. # m3u8 download
  47. 'skip_download': True,
  48. },
  49. }]
  50. _NETRC_MACHINE = 'gaia'
  51. _jwt = None
  52. def _real_initialize(self):
  53. auth = self._get_cookies('https://www.gaia.com/').get('auth')
  54. if auth:
  55. auth = self._parse_json(
  56. compat_urllib_parse_unquote(auth.value),
  57. None, fatal=False)
  58. if not auth:
  59. username, password = self._get_login_info()
  60. if username is None:
  61. return
  62. auth = self._download_json(
  63. 'https://auth.gaia.com/v1/login',
  64. None, data=urlencode_postdata({
  65. 'username': username,
  66. 'password': password
  67. }))
  68. if auth.get('success') is False:
  69. raise ExtractorError(', '.join(auth['messages']), expected=True)
  70. if auth:
  71. self._jwt = auth.get('jwt')
  72. def _real_extract(self, url):
  73. display_id, vtype = re.search(self._VALID_URL, url).groups()
  74. node_id = self._download_json(
  75. 'https://brooklyn.gaia.com/pathinfo', display_id, query={
  76. 'path': 'video/' + display_id,
  77. })['id']
  78. node = self._download_json(
  79. 'https://brooklyn.gaia.com/node/%d' % node_id, node_id)
  80. vdata = node[vtype]
  81. media_id = compat_str(vdata['nid'])
  82. title = node['title']
  83. headers = None
  84. if self._jwt:
  85. headers = {'Authorization': 'Bearer ' + self._jwt}
  86. media = self._download_json(
  87. 'https://brooklyn.gaia.com/media/' + media_id,
  88. media_id, headers=headers)
  89. formats = self._extract_m3u8_formats(
  90. media['mediaUrls']['bcHLS'], media_id, 'mp4')
  91. self._sort_formats(formats)
  92. subtitles = {}
  93. text_tracks = media.get('textTracks', {})
  94. for key in ('captions', 'subtitles'):
  95. for lang, sub_url in text_tracks.get(key, {}).items():
  96. subtitles.setdefault(lang, []).append({
  97. 'url': sub_url,
  98. })
  99. fivestar = node.get('fivestar', {})
  100. fields = node.get('fields', {})
  101. def get_field_value(key, value_key='value'):
  102. return try_get(fields, lambda x: x[key][0][value_key])
  103. return {
  104. 'id': media_id,
  105. 'display_id': display_id,
  106. 'title': title,
  107. 'formats': formats,
  108. 'description': strip_or_none(get_field_value('body') or get_field_value('teaser')),
  109. 'timestamp': int_or_none(node.get('created')),
  110. 'subtitles': subtitles,
  111. 'duration': int_or_none(vdata.get('duration')),
  112. 'like_count': int_or_none(try_get(fivestar, lambda x: x['up_count']['value'])),
  113. 'dislike_count': int_or_none(try_get(fivestar, lambda x: x['down_count']['value'])),
  114. 'comment_count': int_or_none(node.get('comment_count')),
  115. 'series': try_get(node, lambda x: x['series']['title'], compat_str),
  116. 'season_number': int_or_none(get_field_value('season')),
  117. 'season_id': str_or_none(get_field_value('series_nid', 'nid')),
  118. 'episode_number': int_or_none(get_field_value('episode')),
  119. }