]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/gaia.py
[extractor] Add `_perform_login` function (#2943)
[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(compat_urllib_parse_unquote(auth.value), None, fatal=False)
60 self._jwt = auth.get('jwt')
61
62 def _perform_login(self, username, password):
63 if self._jwt:
64 return
65 auth = self._download_json(
66 'https://auth.gaia.com/v1/login',
67 None, data=urlencode_postdata({
68 'username': username,
69 'password': password
70 }))
71 if auth.get('success') is False:
72 raise ExtractorError(', '.join(auth['messages']), expected=True)
73 self._jwt = auth.get('jwt')
74
75 def _real_extract(self, url):
76 display_id, vtype = self._match_valid_url(url).groups()
77 node_id = self._download_json(
78 'https://brooklyn.gaia.com/pathinfo', display_id, query={
79 'path': 'video/' + display_id,
80 })['id']
81 node = self._download_json(
82 'https://brooklyn.gaia.com/node/%d' % node_id, node_id)
83 vdata = node[vtype]
84 media_id = compat_str(vdata['nid'])
85 title = node['title']
86
87 headers = None
88 if self._jwt:
89 headers = {'Authorization': 'Bearer ' + self._jwt}
90 media = self._download_json(
91 'https://brooklyn.gaia.com/media/' + media_id,
92 media_id, headers=headers)
93 formats = self._extract_m3u8_formats(
94 media['mediaUrls']['bcHLS'], media_id, 'mp4')
95 self._sort_formats(formats)
96
97 subtitles = {}
98 text_tracks = media.get('textTracks', {})
99 for key in ('captions', 'subtitles'):
100 for lang, sub_url in text_tracks.get(key, {}).items():
101 subtitles.setdefault(lang, []).append({
102 'url': sub_url,
103 })
104
105 fivestar = node.get('fivestar', {})
106 fields = node.get('fields', {})
107
108 def get_field_value(key, value_key='value'):
109 return try_get(fields, lambda x: x[key][0][value_key])
110
111 return {
112 'id': media_id,
113 'display_id': display_id,
114 'title': title,
115 'formats': formats,
116 'description': strip_or_none(get_field_value('body') or get_field_value('teaser')),
117 'timestamp': int_or_none(node.get('created')),
118 'subtitles': subtitles,
119 'duration': int_or_none(vdata.get('duration')),
120 'like_count': int_or_none(try_get(fivestar, lambda x: x['up_count']['value'])),
121 'dislike_count': int_or_none(try_get(fivestar, lambda x: x['down_count']['value'])),
122 'comment_count': int_or_none(node.get('comment_count')),
123 'series': try_get(node, lambda x: x['series']['title'], compat_str),
124 'season_number': int_or_none(get_field_value('season')),
125 'season_id': str_or_none(get_field_value('series_nid', 'nid')),
126 'episode_number': int_or_none(get_field_value('episode')),
127 }