From 71b7f6133b79601202e777a90e0dfcbd56d8ed34 Mon Sep 17 00:00:00 2001 From: Kai Curtis Date: Sun, 11 Mar 2018 22:20:08 -0700 Subject: [PATCH] Prefer helper funcs, control flow w/ return values Native regex and json parse have been replaced by library versions with more comprehensive error handling. There was flow control via try/catch before that was based on an error being thrown only in test. This has been replaced by an if/else based on return value. --- youtube_dl/extractor/tastytrade.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/youtube_dl/extractor/tastytrade.py b/youtube_dl/extractor/tastytrade.py index 9d6bbc1c0..31acdaed8 100644 --- a/youtube_dl/extractor/tastytrade.py +++ b/youtube_dl/extractor/tastytrade.py @@ -2,12 +2,7 @@ from __future__ import unicode_literals from .common import InfoExtractor from .ooyala import OoyalaIE -from youtube_dl.utils import ( - ExtractorError, -) -import json -import re import sys @@ -56,22 +51,28 @@ class TastyTradeIE(InfoExtractor): info = {'id': None, 'title': None, 'description': None} - try: - info = self._search_json_ld(webpage, display_id, fatal=False) - except ExtractorError as ex: - json_string_match = re.search( - r'var episodeData = \$.parseJSON\("(?P.*)"\)', webpage, 0) + json_ld_info = self._search_json_ld( + webpage, display_id, default=None, fatal=False) - if (json_string_match): - escaped_json_string = json_string_match.group('episode_json') + if (json_ld_info): + info = json_ld_info + else: + escaped_json_string = self._search_regex( + r'var episodeData = \$.parseJSON\("(?P.*)"\)', + webpage, + 'episode json', + fatal=False, + group='episode_json' + ) + if (escaped_json_string): if sys.version_info[0] >= 3: unescaped_json_string = bytes( escaped_json_string, "utf-8").decode('unicode_escape') else: unescaped_json_string = escaped_json_string.decode( 'string_escape') - metadata = json.loads(unescaped_json_string) + metadata = self._parse_json(unescaped_json_string, ooyala_code) info = { 'id': metadata.get('mediaId'), 'title': metadata.get('title'),