]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/qdance.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / qdance.py
1 import json
2 import time
3
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 int_or_none,
8 jwt_decode_hs256,
9 str_or_none,
10 traverse_obj,
11 try_call,
12 url_or_none,
13 )
14
15
16 class QDanceIE(InfoExtractor):
17 _NETRC_MACHINE = 'qdance'
18 _VALID_URL = r'https?://(?:www\.)?q-dance\.com/network/(?:library|live)/(?P<id>[\w-]+)'
19 _TESTS = [{
20 'note': 'vod',
21 'url': 'https://www.q-dance.com/network/library/146542138',
22 'info_dict': {
23 'id': '146542138',
24 'ext': 'mp4',
25 'title': 'Sound Rush [LIVE] | Defqon.1 Weekend Festival 2022 | Friday | RED',
26 'display_id': 'sound-rush-live-v3-defqon-1-weekend-festival-2022-friday-red',
27 'description': 'Relive Defqon.1 - Primal Energy 2022 with the sounds of Sound Rush LIVE at the RED on Friday! 🔥',
28 'season': 'Defqon.1 Weekend Festival 2022',
29 'season_id': '31840632',
30 'series': 'Defqon.1',
31 'series_id': '31840378',
32 'thumbnail': 'https://images.q-dance.network/1674829540-20220624171509-220624171509_delio_dn201093-2.jpg',
33 'availability': 'premium_only',
34 'duration': 1829,
35 },
36 'params': {'skip_download': 'm3u8'},
37 }, {
38 'note': 'livestream',
39 'url': 'https://www.q-dance.com/network/live/149170353',
40 'info_dict': {
41 'id': '149170353',
42 'ext': 'mp4',
43 'title': r're:^Defqon\.1 2023 - Friday - RED',
44 'display_id': 'defqon-1-2023-friday-red',
45 'description': 'md5:3c73fbbd4044e578e696adfc64019163',
46 'season': 'Defqon.1 Weekend Festival 2023',
47 'season_id': '141735599',
48 'series': 'Defqon.1',
49 'series_id': '31840378',
50 'thumbnail': 'https://images.q-dance.network/1686849069-area-thumbs_red.png',
51 'availability': 'subscriber_only',
52 'live_status': 'is_live',
53 'channel_id': 'qdancenetwork.video_149170353',
54 },
55 'skip': 'Completed livestream',
56 }, {
57 'note': 'vod with alphanumeric id',
58 'url': 'https://www.q-dance.com/network/library/WhDleSIWSfeT3Q9ObBKBeA',
59 'info_dict': {
60 'id': 'WhDleSIWSfeT3Q9ObBKBeA',
61 'ext': 'mp4',
62 'title': 'Aftershock I Defqon.1 Weekend Festival 2023 I Sunday I BLUE',
63 'display_id': 'naam-i-defqon-1-weekend-festival-2023-i-dag-i-podium',
64 'description': 'Relive Defqon.1 Path of the Warrior with Aftershock at the BLUE 🔥',
65 'series': 'Defqon.1',
66 'series_id': '31840378',
67 'season': 'Defqon.1 Weekend Festival 2023',
68 'season_id': '141735599',
69 'duration': 3507,
70 'availability': 'premium_only',
71 'thumbnail': 'https://images.q-dance.network/1698158361-230625-135716-defqon-1-aftershock.jpg',
72 },
73 'params': {'skip_download': 'm3u8'},
74 }, {
75 'url': 'https://www.q-dance.com/network/library/-uRFKXwmRZGVnve7av9uqA',
76 'only_matching': True,
77 }]
78
79 _access_token = None
80 _refresh_token = None
81
82 def _call_login_api(self, data, note='Logging in'):
83 login = self._download_json(
84 'https://members.id-t.com/api/auth/login', None, note, headers={
85 'content-type': 'application/json',
86 'brand': 'qdance',
87 'origin': 'https://www.q-dance.com',
88 'referer': 'https://www.q-dance.com/',
89 }, data=json.dumps(data, separators=(',', ':')).encode(),
90 expected_status=lambda x: True)
91
92 tokens = traverse_obj(login, ('data', {
93 '_id-t-accounts-token': ('accessToken', {str}),
94 '_id-t-accounts-refresh': ('refreshToken', {str}),
95 '_id-t-accounts-id-token': ('idToken', {str}),
96 }))
97
98 if not tokens.get('_id-t-accounts-token'):
99 error = ': '.join(traverse_obj(login, ('error', ('code', 'message'), {str})))
100 if 'validation_error' not in error:
101 raise ExtractorError(f'Q-Dance API said "{error}"')
102 msg = 'Invalid username or password' if 'email' in data else 'Refresh token has expired'
103 raise ExtractorError(msg, expected=True)
104
105 for name, value in tokens.items():
106 self._set_cookie('.q-dance.com', name, value)
107
108 def _perform_login(self, username, password):
109 self._call_login_api({'email': username, 'password': password})
110
111 def _real_initialize(self):
112 cookies = self._get_cookies('https://www.q-dance.com/')
113 self._refresh_token = try_call(lambda: cookies['_id-t-accounts-refresh'].value)
114 self._access_token = try_call(lambda: cookies['_id-t-accounts-token'].value)
115 if not self._access_token:
116 self.raise_login_required()
117
118 def _get_auth(self):
119 if (try_call(lambda: jwt_decode_hs256(self._access_token)['exp']) or 0) <= int(time.time() - 120):
120 if not self._refresh_token:
121 raise ExtractorError(
122 'Cannot refresh access token, login with yt-dlp or refresh cookies in browser')
123 self._call_login_api({'refreshToken': self._refresh_token}, note='Refreshing access token')
124 self._real_initialize()
125
126 return {'Authorization': self._access_token}
127
128 def _real_extract(self, url):
129 video_id = self._match_id(url)
130 webpage = self._download_webpage(url, video_id)
131 data = self._search_nuxt_data(webpage, video_id, traverse=('data', 0, 'data'))
132
133 def extract_availability(level):
134 level = int_or_none(level) or 0
135 return self._availability(
136 needs_premium=(level >= 20), needs_subscription=(level >= 15), needs_auth=True)
137
138 info = traverse_obj(data, {
139 'title': ('title', {str.strip}),
140 'description': ('description', {str.strip}),
141 'display_id': ('slug', {str}),
142 'thumbnail': ('thumbnail', {url_or_none}),
143 'duration': ('durationInSeconds', {int_or_none}, {lambda x: x or None}),
144 'availability': ('subscription', 'level', {extract_availability}),
145 'is_live': ('type', {lambda x: x.lower() == 'live'}),
146 'artist': ('acts', ..., {str}),
147 'series': ('event', 'title', {str.strip}),
148 'series_id': ('event', 'id', {str_or_none}),
149 'season': ('eventEdition', 'title', {str.strip}),
150 'season_id': ('eventEdition', 'id', {str_or_none}),
151 'channel_id': ('pubnub', 'channelName', {str}),
152 })
153
154 stream = self._download_json(
155 f'https://dc9h6qmsoymbq.cloudfront.net/api/content/videos/{video_id}/url',
156 video_id, headers=self._get_auth(), expected_status=401)
157
158 m3u8_url = traverse_obj(stream, ('data', 'url', {url_or_none}))
159 if not m3u8_url and traverse_obj(stream, ('error', 'code')) == 'unauthorized':
160 raise ExtractorError('Your account does not have access to this content', expected=True)
161
162 formats = self._extract_m3u8_formats(
163 m3u8_url, video_id, fatal=False, live=True) if m3u8_url else []
164 if not formats:
165 self.raise_no_formats('No active streams found', expected=bool(info.get('is_live')))
166
167 return {
168 **info,
169 'id': video_id,
170 'formats': formats,
171 }