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