From 199e72429106375218902102812e26c2fc6624b5 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Mon, 15 Feb 2016 17:30:53 -0800 Subject: [PATCH 1/3] [KUSI] Add new extractor --- youtube_dl/extractor/__init__.py | 1 + youtube_dl/extractor/kusi.py | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 youtube_dl/extractor/kusi.py diff --git a/youtube_dl/extractor/__init__.py b/youtube_dl/extractor/__init__.py index 537d25777..bfc2008be 100644 --- a/youtube_dl/extractor/__init__.py +++ b/youtube_dl/extractor/__init__.py @@ -338,6 +338,7 @@ from .konserthusetplay import KonserthusetPlayIE from .kontrtube import KontrTubeIE from .krasview import KrasViewIE from .ku6 import Ku6IE +from .kusi import KUSIIE from .kuwo import ( KuwoIE, KuwoAlbumIE, diff --git a/youtube_dl/extractor/kusi.py b/youtube_dl/extractor/kusi.py new file mode 100644 index 000000000..20407411b --- /dev/null +++ b/youtube_dl/extractor/kusi.py @@ -0,0 +1,61 @@ +# coding: utf-8 +from __future__ import unicode_literals + +import re + +from .common import InfoExtractor +from ..compat import compat_urllib_parse_unquote_plus +from ..utils import int_or_none + + +class KUSIIE(InfoExtractor): + _VALID_URL = r'http://(?:www\.)?kusi\.com/(?Pstory/.+|video\?clipId=(?P\d+))' + _TEST = { + 'url': 'http://www.kusi.com/story/31183873/turko-files-case-closed-put-on-hold', + 'md5': 'f926e7684294cf8cb7bdf8858e1b3988', + 'info_dict': { + 'id': '12203019', + 'ext': 'mp4', + 'title': 'Turko Files: Case Closed! & Put On Hold!', + 'duration': 231000, + } + } + + def _real_extract(self, url): + mobj = re.match(self._VALID_URL, url) + + if mobj.group('clipId') is not None: + video_id = mobj.group('clipId') + else: + webpage = self._download_webpage(url, mobj.group('path')) + video_id = self._html_search_regex(r'"clipId", "(\d+)"', webpage, + 'clipId') + + xml_url = 'http://www.kusi.com/build.asp?buildtype=buildfeaturexml'\ + 'request&featureType=Clip&featureid={0}&affiliateno=956&'\ + 'clientgroupid=1&rnd=562461'.format(video_id) + doc = self._download_xml(xml_url, video_id, + note='Downloading video info', + errnote='Failed to download video info') + + video_title = doc.find('HEADLINE').text + duration = int_or_none(doc.find('DURATION'), get_attr='text') + description = doc.find('ABSTRACT') + + quality_options = doc.find('{http://search.yahoo.com/mrss/}group').findall('{http://search.yahoo.com/mrss/}content') + formats = [] + for quality in quality_options: + if 'height' in quality.attrib: + formats.append({ + 'url': compat_urllib_parse_unquote_plus(quality.attrib['url']), + 'height': quality.attrib['height'], + }) + self._sort_formats(formats) + + return { + 'id': video_id, + 'title': video_title, + 'description': description, + 'duration': duration, + 'formats': formats, + } From b6f94d81ea8d3243edf5a4378760281d9d9c2cba Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Fri, 4 Mar 2016 14:32:01 +0800 Subject: [PATCH 2/3] [kusi] Add a test for the alternative form of URL --- youtube_dl/extractor/kusi.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/youtube_dl/extractor/kusi.py b/youtube_dl/extractor/kusi.py index 20407411b..5e87c5f79 100644 --- a/youtube_dl/extractor/kusi.py +++ b/youtube_dl/extractor/kusi.py @@ -10,7 +10,7 @@ from ..utils import int_or_none class KUSIIE(InfoExtractor): _VALID_URL = r'http://(?:www\.)?kusi\.com/(?Pstory/.+|video\?clipId=(?P\d+))' - _TEST = { + _TESTS = [{ 'url': 'http://www.kusi.com/story/31183873/turko-files-case-closed-put-on-hold', 'md5': 'f926e7684294cf8cb7bdf8858e1b3988', 'info_dict': { @@ -18,8 +18,19 @@ class KUSIIE(InfoExtractor): 'ext': 'mp4', 'title': 'Turko Files: Case Closed! & Put On Hold!', 'duration': 231000, - } - } + }, + }, { + 'url': 'http://kusi.com/video?clipId=12203019', + 'info_dict': { + 'id': '12203019', + 'ext': 'mp4', + 'title': 'Turko Files: Case Closed! & Put On Hold!', + 'duration': 231000, + }, + 'params': { + 'skip_download': True, # Same as previous one + }, + }] def _real_extract(self, url): mobj = re.match(self._VALID_URL, url) From 5f1688f27106b6501a2a97d7e6fc1d9f079f1f9f Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Fri, 4 Mar 2016 23:08:47 +0800 Subject: [PATCH 3/3] [kusi] Simplify and improve --- youtube_dl/extractor/kusi.py | 72 ++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/youtube_dl/extractor/kusi.py b/youtube_dl/extractor/kusi.py index 5e87c5f79..5d458e31e 100644 --- a/youtube_dl/extractor/kusi.py +++ b/youtube_dl/extractor/kusi.py @@ -1,11 +1,18 @@ # coding: utf-8 from __future__ import unicode_literals +import random import re from .common import InfoExtractor from ..compat import compat_urllib_parse_unquote_plus -from ..utils import int_or_none +from ..utils import ( + int_or_none, + float_or_none, + timeconvert, + update_url_query, + xpath_text, +) class KUSIIE(InfoExtractor): @@ -17,7 +24,7 @@ class KUSIIE(InfoExtractor): 'id': '12203019', 'ext': 'mp4', 'title': 'Turko Files: Case Closed! & Put On Hold!', - 'duration': 231000, + 'duration': 231, }, }, { 'url': 'http://kusi.com/video?clipId=12203019', @@ -25,7 +32,10 @@ class KUSIIE(InfoExtractor): 'id': '12203019', 'ext': 'mp4', 'title': 'Turko Files: Case Closed! & Put On Hold!', - 'duration': 231000, + 'duration': 231.0, + 'upload_date': '20160210', + 'timestamp': 1455087571, + 'thumbnail': 're:^https?://.*\.jpg$' }, 'params': { 'skip_download': True, # Same as previous one @@ -34,33 +44,47 @@ class KUSIIE(InfoExtractor): def _real_extract(self, url): mobj = re.match(self._VALID_URL, url) + clip_id = mobj.group('clipId') + video_id = clip_id or mobj.group('path') - if mobj.group('clipId') is not None: - video_id = mobj.group('clipId') - else: - webpage = self._download_webpage(url, mobj.group('path')) - video_id = self._html_search_regex(r'"clipId", "(\d+)"', webpage, - 'clipId') + webpage = self._download_webpage(url, video_id) - xml_url = 'http://www.kusi.com/build.asp?buildtype=buildfeaturexml'\ - 'request&featureType=Clip&featureid={0}&affiliateno=956&'\ - 'clientgroupid=1&rnd=562461'.format(video_id) - doc = self._download_xml(xml_url, video_id, - note='Downloading video info', - errnote='Failed to download video info') + if clip_id is None: + video_id = clip_id = self._html_search_regex( + r'"clipId"\s*,\s*"(\d+)"', webpage, 'clip id') - video_title = doc.find('HEADLINE').text - duration = int_or_none(doc.find('DURATION'), get_attr='text') - description = doc.find('ABSTRACT') + affiliate_id = self._search_regex( + r'affiliateId\s*:\s*\'([^\']+)\'', webpage, 'affiliate id') + + # See __Packages/worldnow/model/GalleryModel.as of WNGallery.swf + xml_url = update_url_query('http://www.kusi.com/build.asp', { + 'buildtype': 'buildfeaturexmlrequest', + 'featureType': 'Clip', + 'featureid': clip_id, + 'affiliateno': affiliate_id, + 'clientgroupid': '1', + 'rnd': int(round(random.random() * 1000000)), + }) + + doc = self._download_xml(xml_url, video_id) + + video_title = xpath_text(doc, 'HEADLINE') + duration = float_or_none( + xpath_text(doc, 'DURATION', fatal=False), scale=1000) + description = xpath_text(doc, 'ABSTRACT', fatal=False) + thumbnail = xpath_text(doc, './THUMBNAILIMAGE/FILENAME', fatal=False) + createtion_time = timeconvert( + xpath_text(doc, 'rfc822creationdate', fatal=False)) quality_options = doc.find('{http://search.yahoo.com/mrss/}group').findall('{http://search.yahoo.com/mrss/}content') formats = [] for quality in quality_options: - if 'height' in quality.attrib: - formats.append({ - 'url': compat_urllib_parse_unquote_plus(quality.attrib['url']), - 'height': quality.attrib['height'], - }) + formats.append({ + 'url': compat_urllib_parse_unquote_plus(quality.attrib['url']), + 'height': int_or_none(quality.attrib.get('height')), + 'width': int_or_none(quality.attrib.get('width')), + 'vbr': float_or_none(quality.attrib.get('bitratebits'), scale=1024), + }) self._sort_formats(formats) return { @@ -69,4 +93,6 @@ class KUSIIE(InfoExtractor): 'description': description, 'duration': duration, 'formats': formats, + 'thumbnail': thumbnail, + 'timestamp': createtion_time, }