From 7ab078eae9d34687e0a3db3e76e79cff6316a677 Mon Sep 17 00:00:00 2001 From: Tatsuyuki Ishi Date: Wed, 13 Sep 2017 22:23:57 +0900 Subject: [PATCH] Address review issues --- youtube_dl/extractor/mixcloud.py | 51 +++++++++++++++----------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/youtube_dl/extractor/mixcloud.py b/youtube_dl/extractor/mixcloud.py index 3c34d6be4..df4237174 100644 --- a/youtube_dl/extractor/mixcloud.py +++ b/youtube_dl/extractor/mixcloud.py @@ -18,7 +18,7 @@ from ..utils import ( ExtractorError, OnDemandPagedList, str_to_int, -) + try_get) class MixcloudIE(InfoExtractor): @@ -81,12 +81,8 @@ class MixcloudIE(InfoExtractor): full_info_json = self._parse_json(self._html_search_regex( r'', webpage, 'play info'), 'play info') for item in full_info_json: - item_data = item.get("cloudcast", {}) \ - .get("data", {}) \ - .get("cloudcastLookup", {}) - if item_data \ - .get("streamInfo", {}) \ - .get("url", "") != "": + item_data = try_get(item, lambda x: x['cloudcast']['data']['cloudcastLookup']) + if try_get(item_data, lambda x: x['streamInfo']['url']) not in ['', None]: info_json = item_data break @@ -108,7 +104,7 @@ class MixcloudIE(InfoExtractor): kpa_target = encrypted_play_info else: kp = 'https://' - kpa_target = base64.b64decode(info_json["streamInfo"]["url"]) + kpa_target = base64.b64decode(info_json['streamInfo']['url']) partial_key = self._decrypt_xor_cipher(kpa_target, kp) for quote in ["'", '"']: key = self._search_regex(r'{0}({1}[^{0}]*){0}'.format(quote, re.escape(partial_key)), js, @@ -142,25 +138,26 @@ class MixcloudIE(InfoExtractor): else: title = info_json['name'] - thumbnail = 'https://thumbnailer.mixcloud.com/unsafe/600x600/' + info_json['picture']['urlRoot'] - uploader = info_json['owner']['displayName'] - uploader_id = info_json['owner']['username'] - description = info_json['description'] - view_count = info_json['plays'] - formats = [ - { - 'format_id': 'normal', - 'url': self._decrypt_xor_cipher(key, base64.b64decode(info_json['streamInfo']['url'])) - }, - { - 'format_id': 'hls', - 'url': self._decrypt_xor_cipher(key, base64.b64decode(info_json['streamInfo']['hlsUrl'])) - }, - { - 'format_id': 'dash', - 'url': self._decrypt_xor_cipher(key, base64.b64decode(info_json['streamInfo']['dashUrl'])) - } - ] + thumbnail = try_get(info_json, + lambda x: 'https://thumbnailer.mixcloud.com/unsafe/600x600/' + x['picture']['urlRoot']) + uploader = try_get(info_json, lambda x: x['owner']['displayName']) + uploader_id = try_get(info_json, lambda x: x['owner']['username']) + description = try_get(info_json, lambda x: x['description']) + view_count = try_get(info_json, lambda x: x['plays']) + formats = [{ + 'format_id': 'normal', + 'url': self._decrypt_xor_cipher(key, base64.b64decode(info_json['streamInfo']['url'])) + }] + + hls_encrypted = try_get(info_json, lambda x: x['streamInfo']['hlsUrl']) + if hls_encrypted is not None: + hls_url = self._decrypt_xor_cipher(key, base64.b64decode(hls_encrypted)) + formats.extend(self._extract_m3u8_formats(hls_url, title)) + + dash_encrypted = try_get(info_json, lambda x: x['streamInfo']['dashUrl']) + if dash_encrypted is not None: + dash_url = self._decrypt_xor_cipher(key, base64.b64decode(dash_encrypted)) + formats.extend(self._extract_mpd_formats(dash_url, title)) return { 'id': track_id,