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