mirror of https://github.com/yt-dlp/yt-dlp.git
[SponsorBlock] Obey `extractor-retries` and `sleep-requests`
This commit is contained in:
parent
19b824f693
commit
ef58c47637
|
@ -1,6 +1,8 @@
|
|||
from hashlib import sha256
|
||||
import itertools
|
||||
import json
|
||||
import re
|
||||
from hashlib import sha256
|
||||
import time
|
||||
|
||||
from .ffmpeg import FFmpegPostProcessor
|
||||
from ..compat import compat_urllib_parse_urlencode, compat_HTTPError
|
||||
|
@ -33,6 +35,7 @@ class SponsorBlockPP(FFmpegPostProcessor):
|
|||
self.to_screen(f'SponsorBlock is not supported for {extractor}')
|
||||
return [], info
|
||||
|
||||
self.to_screen('Fetching SponsorBlock segments')
|
||||
info['sponsorblock_chapters'] = self._get_sponsor_chapters(info, info['duration'])
|
||||
return [], info
|
||||
|
||||
|
@ -79,18 +82,28 @@ class SponsorBlockPP(FFmpegPostProcessor):
|
|||
'service': service,
|
||||
'categories': json.dumps(self._categories),
|
||||
})
|
||||
self.write_debug(f'SponsorBlock query: {url}')
|
||||
for d in self._get_json(url):
|
||||
if d['videoID'] == video_id:
|
||||
return d['segments']
|
||||
return []
|
||||
|
||||
def _get_json(self, url):
|
||||
self.write_debug(f'SponsorBlock query: {url}')
|
||||
# While this is not an extractor, it behaves similar to one and
|
||||
# so obey extractor_retries and sleep_interval_requests
|
||||
max_retries = self.get_param('extractor_retries', 3)
|
||||
sleep_interval = self.get_param('sleep_interval_requests') or 0
|
||||
for retries in itertools.count():
|
||||
try:
|
||||
rsp = self._downloader.urlopen(sanitized_Request(url))
|
||||
return json.loads(rsp.read().decode(rsp.info().get_param('charset') or 'utf-8'))
|
||||
except network_exceptions as e:
|
||||
if isinstance(e, compat_HTTPError) and e.code == 404:
|
||||
return []
|
||||
raise PostProcessingError(f'Unable to communicate with SponsorBlock API - {e}')
|
||||
|
||||
return json.loads(rsp.read().decode(rsp.info().get_param('charset') or 'utf-8'))
|
||||
if retries < max_retries:
|
||||
self.report_warning(f'{e}. Retrying...')
|
||||
if sleep_interval > 0:
|
||||
self.to_screen(f'Sleeping {sleep_interval} seconds ...')
|
||||
time.sleep(sleep_interval)
|
||||
continue
|
||||
raise PostProcessingError(f'Unable to communicate with SponsorBlock API: {e}')
|
||||
|
|
Loading…
Reference in New Issue