]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/soundcloud.py
Standardize retry mechanism (#1649)
[yt-dlp.git] / yt_dlp / extractor / soundcloud.py
index 6dfa50c602fb8f9406a7887531f5020a5d992409..2730052a01861bbe52b8e1df3e99809e59345e7a 100644 (file)
@@ -19,7 +19,6 @@
     int_or_none,
     KNOWN_EXTENSIONS,
     mimetype2ext,
-    remove_end,
     parse_qs,
     str_or_none,
     try_get,
 
 class SoundcloudEmbedIE(InfoExtractor):
     _VALID_URL = r'https?://(?:w|player|p)\.soundcloud\.com/player/?.*?\burl=(?P<id>.+)'
+    _EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?://)?(?:w\.)?soundcloud\.com/player.+?)\1']
     _TEST = {
         # from https://www.soundi.fi/uutiset/ennakkokuuntelussa-timo-kaukolammen-station-to-station-to-station-julkaisua-juhlitaan-tanaan-g-livelabissa/
         'url': 'https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F922213810&show_artwork=true&maxwidth=640&maxheight=960&dnt=1&secret_token=s-ziYey',
         'only_matching': True,
     }
 
-    @staticmethod
-    def _extract_urls(webpage):
-        return [m.group('url') for m in re.finditer(
-            r'<iframe[^>]+src=(["\'])(?P<url>(?:https?://)?(?:w\.)?soundcloud\.com/player.+?)\1',
-            webpage)]
-
     def _real_extract(self, url):
         query = parse_qs(url)
         api_url = query['url'][0]
@@ -67,7 +61,7 @@ class SoundcloudBaseIE(InfoExtractor):
     _HEADERS = {}
 
     def _store_client_id(self, client_id):
-        self._downloader.cache.store('soundcloud', 'client_id', client_id)
+        self.cache.store('soundcloud', 'client_id', client_id)
 
     def _update_client_id(self):
         webpage = self._download_webpage('https://soundcloud.com/', None)
@@ -104,7 +98,7 @@ def _download_json(self, *args, **kwargs):
                 raise
 
     def _initialize_pre_login(self):
-        self._CLIENT_ID = self._downloader.cache.load('soundcloud', 'client_id') or 'a3e059563d7fd3372b49b37f00a00bcf'
+        self._CLIENT_ID = self.cache.load('soundcloud', 'client_id') or 'a3e059563d7fd3372b49b37f00a00bcf'
 
     def _perform_login(self, username, password):
         if username != 'oauth':
@@ -666,25 +660,20 @@ def _entries(self, url, playlist_id):
             'offset': 0,
         }
 
-        retries = self.get_param('extractor_retries', 3)
-
         for i in itertools.count():
-            attempt, last_error = -1, None
-            while attempt < retries:
-                attempt += 1
-                if last_error:
-                    self.report_warning('%s. Retrying ...' % remove_end(last_error, '.'), playlist_id)
+            for retry in self.RetryManager():
                 try:
                     response = self._download_json(
                         url, playlist_id, query=query, headers=self._HEADERS,
-                        note='Downloading track page %s%s' % (i + 1, f' (retry #{attempt})' if attempt else ''))
+                        note=f'Downloading track page {i + 1}')
                     break
                 except ExtractorError as e:
                     # Downloading page may result in intermittent 502 HTTP error
                     # See https://github.com/yt-dlp/yt-dlp/issues/872
-                    if attempt >= retries or not isinstance(e.cause, compat_HTTPError) or e.cause.code != 502:
+                    if not isinstance(e.cause, compat_HTTPError) or e.cause.code != 502:
                         raise
-                    last_error = str(e.cause or e.msg)
+                    retry.error = e
+                    continue
 
             def resolve_entry(*candidates):
                 for cand in candidates: