]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/kusi.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / kusi.py
CommitLineData
5f1688f2 1import random
ac668111 2import urllib.parse
199e7242 3
4from .common import InfoExtractor
5f1688f2 5from ..utils import (
5f1688f2 6 float_or_none,
ac668111 7 int_or_none,
5f1688f2
YCH
8 timeconvert,
9 update_url_query,
10 xpath_text,
11)
199e7242 12
13
14class KUSIIE(InfoExtractor):
5886b38d 15 _VALID_URL = r'https?://(?:www\.)?kusi\.com/(?P<path>story/.+|video\?clipId=(?P<clipId>\d+))'
b6f94d81 16 _TESTS = [{
a06e1498
S
17 'url': 'http://www.kusi.com/story/32849881/turko-files-refused-to-help-it-aint-right',
18 'md5': '4e76ce8e53660ce9697d06c0ba6fc47d',
199e7242 19 'info_dict': {
a06e1498 20 'id': '12689020',
199e7242 21 'ext': 'mp4',
a06e1498
S
22 'title': "Turko Files: Refused to Help, It Ain't Right!",
23 'duration': 223.586,
24 'upload_date': '20160826',
25 'timestamp': 1472233118,
ec85ded8 26 'thumbnail': r're:^https?://.*\.jpg$'
b6f94d81
YCH
27 },
28 }, {
29 'url': 'http://kusi.com/video?clipId=12203019',
a06e1498 30 'only_matching': True,
b6f94d81 31 }]
199e7242 32
33 def _real_extract(self, url):
5ad28e7f 34 mobj = self._match_valid_url(url)
5f1688f2
YCH
35 clip_id = mobj.group('clipId')
36 video_id = clip_id or mobj.group('path')
199e7242 37
5f1688f2 38 webpage = self._download_webpage(url, video_id)
199e7242 39
5f1688f2
YCH
40 if clip_id is None:
41 video_id = clip_id = self._html_search_regex(
42 r'"clipId"\s*,\s*"(\d+)"', webpage, 'clip id')
199e7242 43
5f1688f2
YCH
44 affiliate_id = self._search_regex(
45 r'affiliateId\s*:\s*\'([^\']+)\'', webpage, 'affiliate id')
46
47 # See __Packages/worldnow/model/GalleryModel.as of WNGallery.swf
48 xml_url = update_url_query('http://www.kusi.com/build.asp', {
49 'buildtype': 'buildfeaturexmlrequest',
50 'featureType': 'Clip',
51 'featureid': clip_id,
52 'affiliateno': affiliate_id,
53 'clientgroupid': '1',
54 'rnd': int(round(random.random() * 1000000)),
55 })
56
57 doc = self._download_xml(xml_url, video_id)
58
fa880d20
YCH
59 video_title = xpath_text(doc, 'HEADLINE', fatal=True)
60 duration = float_or_none(xpath_text(doc, 'DURATION'), scale=1000)
61 description = xpath_text(doc, 'ABSTRACT')
62 thumbnail = xpath_text(doc, './THUMBNAILIMAGE/FILENAME')
a0566bbf 63 creation_time = timeconvert(xpath_text(doc, 'rfc822creationdate'))
199e7242 64
65 quality_options = doc.find('{http://search.yahoo.com/mrss/}group').findall('{http://search.yahoo.com/mrss/}content')
66 formats = []
67 for quality in quality_options:
5f1688f2 68 formats.append({
ac668111 69 'url': urllib.parse.unquote_plus(quality.attrib['url']),
5f1688f2
YCH
70 'height': int_or_none(quality.attrib.get('height')),
71 'width': int_or_none(quality.attrib.get('width')),
fa880d20 72 'vbr': float_or_none(quality.attrib.get('bitratebits'), scale=1000),
5f1688f2 73 })
199e7242 74
75 return {
76 'id': video_id,
77 'title': video_title,
78 'description': description,
79 'duration': duration,
80 'formats': formats,
5f1688f2 81 'thumbnail': thumbnail,
a0566bbf 82 'timestamp': creation_time,
199e7242 83 }