[SponsorBlock] Obey `extractor-retries` and `sleep-requests`

This commit is contained in:
pukkandan 2021-10-18 09:19:37 +05:30
parent 19b824f693
commit ef58c47637
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698
1 changed files with 23 additions and 10 deletions

View File

@ -1,6 +1,8 @@
from hashlib import sha256
import itertools
import json import json
import re import re
from hashlib import sha256 import time
from .ffmpeg import FFmpegPostProcessor from .ffmpeg import FFmpegPostProcessor
from ..compat import compat_urllib_parse_urlencode, compat_HTTPError 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}') self.to_screen(f'SponsorBlock is not supported for {extractor}')
return [], info return [], info
self.to_screen('Fetching SponsorBlock segments')
info['sponsorblock_chapters'] = self._get_sponsor_chapters(info, info['duration']) info['sponsorblock_chapters'] = self._get_sponsor_chapters(info, info['duration'])
return [], info return [], info
@ -79,18 +82,28 @@ class SponsorBlockPP(FFmpegPostProcessor):
'service': service, 'service': service,
'categories': json.dumps(self._categories), 'categories': json.dumps(self._categories),
}) })
self.write_debug(f'SponsorBlock query: {url}')
for d in self._get_json(url): for d in self._get_json(url):
if d['videoID'] == video_id: if d['videoID'] == video_id:
return d['segments'] return d['segments']
return [] return []
def _get_json(self, url): 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: try:
rsp = self._downloader.urlopen(sanitized_Request(url)) 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: except network_exceptions as e:
if isinstance(e, compat_HTTPError) and e.code == 404: if isinstance(e, compat_HTTPError) and e.code == 404:
return [] return []
raise PostProcessingError(f'Unable to communicate with SponsorBlock API - {e}') if retries < max_retries:
self.report_warning(f'{e}. Retrying...')
return json.loads(rsp.read().decode(rsp.info().get_param('charset') or 'utf-8')) 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}')