qdance.py 7.3 KB

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