]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/kusi.py
Merge branch 'mutantmonkey-kusi'
[yt-dlp.git] / youtube_dl / extractor / kusi.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import random
5 import re
6
7 from .common import InfoExtractor
8 from ..compat import compat_urllib_parse_unquote_plus
9 from ..utils import (
10 int_or_none,
11 float_or_none,
12 timeconvert,
13 update_url_query,
14 xpath_text,
15 )
16
17
18 class KUSIIE(InfoExtractor):
19 _VALID_URL = r'http://(?:www\.)?kusi\.com/(?P<path>story/.+|video\?clipId=(?P<clipId>\d+))'
20 _TESTS = [{
21 'url': 'http://www.kusi.com/story/31183873/turko-files-case-closed-put-on-hold',
22 'md5': 'f926e7684294cf8cb7bdf8858e1b3988',
23 'info_dict': {
24 'id': '12203019',
25 'ext': 'mp4',
26 'title': 'Turko Files: Case Closed! & Put On Hold!',
27 'duration': 231,
28 },
29 }, {
30 'url': 'http://kusi.com/video?clipId=12203019',
31 'info_dict': {
32 'id': '12203019',
33 'ext': 'mp4',
34 'title': 'Turko Files: Case Closed! & Put On Hold!',
35 'duration': 231.0,
36 'upload_date': '20160210',
37 'timestamp': 1455087571,
38 'thumbnail': 're:^https?://.*\.jpg$'
39 },
40 'params': {
41 'skip_download': True, # Same as previous one
42 },
43 }]
44
45 def _real_extract(self, url):
46 mobj = re.match(self._VALID_URL, url)
47 clip_id = mobj.group('clipId')
48 video_id = clip_id or mobj.group('path')
49
50 webpage = self._download_webpage(url, video_id)
51
52 if clip_id is None:
53 video_id = clip_id = self._html_search_regex(
54 r'"clipId"\s*,\s*"(\d+)"', webpage, 'clip id')
55
56 affiliate_id = self._search_regex(
57 r'affiliateId\s*:\s*\'([^\']+)\'', webpage, 'affiliate id')
58
59 # See __Packages/worldnow/model/GalleryModel.as of WNGallery.swf
60 xml_url = update_url_query('http://www.kusi.com/build.asp', {
61 'buildtype': 'buildfeaturexmlrequest',
62 'featureType': 'Clip',
63 'featureid': clip_id,
64 'affiliateno': affiliate_id,
65 'clientgroupid': '1',
66 'rnd': int(round(random.random() * 1000000)),
67 })
68
69 doc = self._download_xml(xml_url, video_id)
70
71 video_title = xpath_text(doc, 'HEADLINE')
72 duration = float_or_none(
73 xpath_text(doc, 'DURATION', fatal=False), scale=1000)
74 description = xpath_text(doc, 'ABSTRACT', fatal=False)
75 thumbnail = xpath_text(doc, './THUMBNAILIMAGE/FILENAME', fatal=False)
76 createtion_time = timeconvert(
77 xpath_text(doc, 'rfc822creationdate', fatal=False))
78
79 quality_options = doc.find('{http://search.yahoo.com/mrss/}group').findall('{http://search.yahoo.com/mrss/}content')
80 formats = []
81 for quality in quality_options:
82 formats.append({
83 'url': compat_urllib_parse_unquote_plus(quality.attrib['url']),
84 'height': int_or_none(quality.attrib.get('height')),
85 'width': int_or_none(quality.attrib.get('width')),
86 'vbr': float_or_none(quality.attrib.get('bitratebits'), scale=1024),
87 })
88 self._sort_formats(formats)
89
90 return {
91 'id': video_id,
92 'title': video_title,
93 'description': description,
94 'duration': duration,
95 'formats': formats,
96 'thumbnail': thumbnail,
97 'timestamp': createtion_time,
98 }