From 18b5708685df753d2c33ebeb143cdd192dbdea82 Mon Sep 17 00:00:00 2001 From: Pablo Castorino Date: Thu, 31 Jan 2019 19:33:36 -0300 Subject: [PATCH] cleanup code, flake8 passed --- youtube_dl/extractor/contar.py | 119 ++++++++++++++++----------------- 1 file changed, 57 insertions(+), 62 deletions(-) diff --git a/youtube_dl/extractor/contar.py b/youtube_dl/extractor/contar.py index 3f29ac14e..3752b5e88 100644 --- a/youtube_dl/extractor/contar.py +++ b/youtube_dl/extractor/contar.py @@ -5,12 +5,12 @@ from .common import InfoExtractor from ..utils import ( int_or_none, urlencode_postdata, - compat_str, ExtractorError, ) + class ContarBaseIE(InfoExtractor): - + _NETRC_MACHINE = 'contar' _API_BASE = 'https://api.cont.ar/api/v2/' @@ -22,16 +22,16 @@ class ContarBaseIE(InfoExtractor): raise ExtractorError( '%s said: %s' % (self.IE_NAME, error), expected=True) - def _call_api(self, path, video_id, headers = {}, note='Downloading JSON metadata'): + def _call_api(self, path, video_id, headers={}, note='Downloading JSON metadata'): if self._auth_token: headers['Authorization'] = 'Bearer ' + self._auth_token - + result = self._download_json( self._API_BASE + path, video_id, headers=headers, note=note) - + self._handle_errors(result) return result['data'] - + def _real_initialize(self): email, password = self._get_login_info() if email is None: @@ -42,21 +42,19 @@ class ContarBaseIE(InfoExtractor): 'email': email, 'password': password, })) - + self._handle_errors(result) self._auth_token = result['token'] - - def _get_video_info(self, video, video_id, base = {}): - #print(json.dumps(video, indent=4, sort_keys=True)) - #print "id = %s S%sE%s" % (video.get('id'), season.get('name') , video.get('episode')) - + + def _get_video_info(self, video, video_id, base={}): + formats = self._get_formats(video.get('streams', []), video.get('id')) subtitles = self._get_subtitles(video['subtitles'].get('data', []), video.get('id')) - + serie_info = base.get('serie_info') or self._get_serie_info(video.get('serie')) - season_number = base.get('season_number') or self._get_season_number(serie_info, video.get('id')); + season_number = base.get('season_number') or self._get_season_number(serie_info, video.get('id')) episode_number = video.get('episode') - + info = { 'id': video.get('id'), 'title': video.get('name'), @@ -70,34 +68,33 @@ class ContarBaseIE(InfoExtractor): 'duration': int_or_none(video.get('length')), 'thumbnail': video.get('posterImage'), 'release_year': int_or_none(serie_info.get('year')), - #'timestamp': timestamp, + # 'timestamp': timestamp, 'formats': formats, 'subtitles': subtitles, } - + return info - + def _get_serie_info(self, serie_id, headers={}): serie = self._call_api('serie/' + serie_id, serie_id, headers=headers, note='Downloading Serie JSON metadata') return serie - + def _get_season_number(self, serie_info, video_id): for season in serie_info['seasons'].get('data', []): - #print(json.dumps(season, indent=4, sort_keys=True)) season_number = season.get('name') for episode in season['videos'].get('data', []): - if episode.get('id') == video_id: + if episode.get('id') == video_id: return season_number return None - + def _get_subtitles(self, subtitles, video_id): subs = {} for sub in subtitles: lang = sub.get('lang').lower() - subs[lang] = [{ 'url': sub.get('url'), 'ext': 'srt'}] - + subs[lang] = [{'url': sub.get('url'), 'ext': 'srt'}] + return subs - + def _get_formats(self, videos, video_id): formats = [] for stream in videos: @@ -105,18 +102,18 @@ class ContarBaseIE(InfoExtractor): type = stream.get('type') if (type == 'HLS'): formats.extend(self._extract_m3u8_formats(stream_url, - video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls', - fatal=False)) + video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls', + fatal=False)) elif (type == 'DASH'): formats.extend(self._extract_mpd_formats( stream_url, video_id, mpd_id='dash', fatal=False)) - + self._sort_formats(formats) return formats - - + + class ContarIE(ContarBaseIE): - + _UUID_RE = r'[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}' _VALID_URL = r'https?://(?:www\.)?cont\.ar/watch/(?P%s)' % _UUID_RE _TEST = { @@ -142,17 +139,17 @@ class ContarIE(ContarBaseIE): 'format': 'hls-4755-1' } } - + def _real_extract(self, url): video_id = self._match_id(url) - + video = self._call_api('videos/' + video_id, video_id, headers={'Referer': url}) - info = self._get_video_info(video, video_id); + info = self._get_video_info(video, video_id) return info - - + + class ContarSerieIE(ContarBaseIE): - + _UUID_RE = r'[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}' _VALID_URL = r'https?://(?:www\.)?cont\.ar/serie/(?P%s)' % _UUID_RE _TEST = { @@ -161,7 +158,7 @@ class ContarSerieIE(ContarBaseIE): 'id': '353247d5-da97-4cb6-8571-c4fbab28c643', 'title': 'Vidas de Radio', 'description': 'Ana Gerschenson conduce el ciclo que repasa historias de grandes personalidades que le dieron vida al medio; marcaron una época de la Argentina y de tu vida, esas voces amigas que estuvieron siempre y son Vidas De Radio.' - #'thumbnail': r're:^https?://.*\.jpg$', + # 'thumbnail': r're:^https?://.*\.jpg$', # TODO more properties, either as: # * A value # * MD5 checksum; start the string with md5: @@ -206,32 +203,30 @@ class ContarSerieIE(ContarBaseIE): 'format': 'bestvideo', } } - + def _real_extract(self, url): serie_id = self._match_id(url) - + serie_info = self._get_serie_info(serie_id, headers={'Referer': url}) - - seasons = [] + entries = [] - + base = {} base['serie_info'] = serie_info for season in serie_info['seasons'].get('data', []): - #print(json.dumps(season, indent=4, sort_keys=True)) base['season_number'] = season.get('name') for episode in season['videos'].get('data', []): - info = self._get_video_info(episode, serie_id, base); + info = self._get_video_info(episode, serie_id, base) entries.append(info) - + return self.playlist_result( entries, serie_id, - serie_info.get('name'), serie_info.get('story_large')) - + serie_info.get('name'), serie_info.get('story_large')) + class ContarChannelIE(ContarBaseIE): - + _UUID_RE = r'[\d]{1,}' _VALID_URL = r'https?://(?:www\.)?cont\.ar/channel/(?P%s)' % _UUID_RE _TEST = { @@ -252,23 +247,24 @@ class ContarChannelIE(ContarBaseIE): 'skip_download': True } } - + def _real_extract(self, url): list_id = self._match_id(url) channel_info = self._call_api('channel/info/' + list_id, list_id, headers={'Referer': url}, note='Downloading Channel Info JSON metadata') list = self._call_api('channel/series/' + list_id, list_id, headers={'Referer': url}, note='Downloading Channel List JSON metadata') - entries = [] - + entries = [] + for video in list: if (video.get('type') == 'SERIE'): url = 'www.cont.ar/serie/%s' % video.get('uuid') entries.append(self.url_result(url, video_id=video.get('uuid'), video_title=video.get('name'))) - + return self.playlist_result( - entries, list_id, channel_info.get('name'), channel_info.get('description')) + entries, list_id, channel_info.get('name'), channel_info.get('description')) + class ContarBrowseIE(ContarBaseIE): - + _UUID_RE = r'[\d]{1,}' _VALID_URL = r'https?://(?:www\.)?cont\.ar/browse/genre/(?P%s)' % _UUID_RE _TEST = { @@ -288,19 +284,18 @@ class ContarBrowseIE(ContarBaseIE): 'skip_download': True } } - + def _real_extract(self, url): list_id = self._match_id(url) - + list = self._call_api('full/section/' + list_id, list_id, headers={'Referer': url}) - entries = [] - + entries = [] + for video in list['videos'].get('data', []): if (video.get('type') == 'SERIE'): url = 'www.cont.ar/serie/%s' % video.get('uuid') entries.append(self.url_result(url, video_id=video.get('uuid'), video_title=video.get('name'))) - + return self.playlist_result( entries, list_id, - list.get('title')) - + list.get('title'))