]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/kusi.py
Update changelog
[youtubedl] / 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'https?://(?:www\.)?kusi\.com/(?P<path>story/.+|video\?clipId=(?P<clipId>\d+))'
20 _TESTS = [{
21 'url': 'http://www.kusi.com/story/32849881/turko-files-refused-to-help-it-aint-right',
22 'md5': '4e76ce8e53660ce9697d06c0ba6fc47d',
23 'info_dict': {
24 'id': '12689020',
25 'ext': 'mp4',
26 'title': "Turko Files: Refused to Help, It Ain't Right!",
27 'duration': 223.586,
28 'upload_date': '20160826',
29 'timestamp': 1472233118,
30 'thumbnail': r're:^https?://.*\.jpg$'
31 },
32 }, {
33 'url': 'http://kusi.com/video?clipId=12203019',
34 'only_matching': True,
35 }]
36
37 def _real_extract(self, url):
38 mobj = re.match(self._VALID_URL, url)
39 clip_id = mobj.group('clipId')
40 video_id = clip_id or mobj.group('path')
41
42 webpage = self._download_webpage(url, video_id)
43
44 if clip_id is None:
45 video_id = clip_id = self._html_search_regex(
46 r'"clipId"\s*,\s*"(\d+)"', webpage, 'clip id')
47
48 affiliate_id = self._search_regex(
49 r'affiliateId\s*:\s*\'([^\']+)\'', webpage, 'affiliate id')
50
51 # See __Packages/worldnow/model/GalleryModel.as of WNGallery.swf
52 xml_url = update_url_query('http://www.kusi.com/build.asp', {
53 'buildtype': 'buildfeaturexmlrequest',
54 'featureType': 'Clip',
55 'featureid': clip_id,
56 'affiliateno': affiliate_id,
57 'clientgroupid': '1',
58 'rnd': int(round(random.random() * 1000000)),
59 })
60
61 doc = self._download_xml(xml_url, video_id)
62
63 video_title = xpath_text(doc, 'HEADLINE', fatal=True)
64 duration = float_or_none(xpath_text(doc, 'DURATION'), scale=1000)
65 description = xpath_text(doc, 'ABSTRACT')
66 thumbnail = xpath_text(doc, './THUMBNAILIMAGE/FILENAME')
67 createtion_time = timeconvert(xpath_text(doc, 'rfc822creationdate'))
68
69 quality_options = doc.find('{http://search.yahoo.com/mrss/}group').findall('{http://search.yahoo.com/mrss/}content')
70 formats = []
71 for quality in quality_options:
72 formats.append({
73 'url': compat_urllib_parse_unquote_plus(quality.attrib['url']),
74 'height': int_or_none(quality.attrib.get('height')),
75 'width': int_or_none(quality.attrib.get('width')),
76 'vbr': float_or_none(quality.attrib.get('bitratebits'), scale=1000),
77 })
78 self._sort_formats(formats)
79
80 return {
81 'id': video_id,
82 'title': video_title,
83 'description': description,
84 'duration': duration,
85 'formats': formats,
86 'thumbnail': thumbnail,
87 'timestamp': createtion_time,
88 }