From fd311fb1e3c29700042fc9d11d634d505ca59bb4 Mon Sep 17 00:00:00 2001 From: NobleKangaroo Date: Wed, 27 Jan 2021 17:01:25 -0500 Subject: [PATCH] Improve error handling logic, follow code conventions --- youtube_dl/extractor/funimation.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/youtube_dl/extractor/funimation.py b/youtube_dl/extractor/funimation.py index 4c264fa4c..7b6464cfa 100644 --- a/youtube_dl/extractor/funimation.py +++ b/youtube_dl/extractor/funimation.py @@ -110,24 +110,18 @@ class FunimationIE(InfoExtractor): headers = {} if self._TOKEN: headers['Authorization'] = 'Token %s' % self._TOKEN - try: - sources = self._download_json( - 'https://www.funimation.com/api/showexperience/%s/' % video_id, - video_id, headers=headers, query={ - 'pinst_id': ''.join([random.choice(string.digits + string.ascii_letters) for _ in range(8)]), - }) - sources = sources['items'] - except KeyError: - if 'errors' in sources: - errors = sources['errors'] - if len(errors) > 0: - error = errors[0] - if 'detail' in error: - detail = error['detail'] - raise ExtractorError('%s said: %s' % ( - self.IE_NAME, detail), expected=True) - else: - raise ExtractorError(error, expected=True) + meta = self._download_json( + 'https://www.funimation.com/api/showexperience/%s/' % video_id, + video_id, headers=headers, query={ + 'pinst_id': ''.join([random.choice(string.digits + string.ascii_letters) for _ in range(8)]), + }) + sources = meta.get('items') or [] + errors = meta.get('errors') + if errors: + if isinstance(errors, list): + raise ExtractorError('\nERROR: '.join([error.get('detail') or error.get('title') or str(error) for error in errors]), expected=True) + else: + raise ExtractorError(errors, expected=True) except ExtractorError as e: if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403: error = self._parse_json(e.cause.read(), video_id)['errors'][0]