[cnn] Add multiple formats, duration, and upload_date
[youtube-dl] / youtube_dl / extractor / cnn.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5     int_or_none,
6     parse_duration,
7 )
8
9
10 class CNNIE(InfoExtractor):
11     _VALID_URL = r'''(?x)https?://((edition|www)\.)?cnn\.com/video/(data/.+?|\?)/
12         (?P<path>.+?/(?P<title>[^/]+?)(?:\.cnn|(?=&)))'''
13
14     _TESTS = [{
15         u'url': u'http://edition.cnn.com/video/?/video/sports/2013/06/09/nadal-1-on-1.cnn',
16         u'file': u'sports_2013_06_09_nadal-1-on-1.cnn.mp4',
17         u'md5': u'3e6121ea48df7e2259fe73a0628605c4',
18         u'info_dict': {
19             u'title': u'Nadal wins 8th French Open title',
20             u'description': u'World Sport\'s Amanda Davies chats with 2013 French Open champion Rafael Nadal.',
21             u'duration': 135,
22             u'upload_date': u'20130609',
23         },
24     },
25     {
26         u"url": u"http://edition.cnn.com/video/?/video/us/2013/08/21/sot-student-gives-epic-speech.georgia-institute-of-technology&utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+rss%2Fcnn_topstories+%28RSS%3A+Top+Stories%29",
27         u"file": u"us_2013_08_21_sot-student-gives-epic-speech.georgia-institute-of-technology.mp4",
28         u"md5": u"b5cc60c60a3477d185af8f19a2a26f4e",
29         u"info_dict": {
30             u"title": "Student's epic speech stuns new freshmen",
31             u"description": "A Georgia Tech student welcomes the incoming freshmen with an epic speech backed by music from \"2001: A Space Odyssey.\""
32         }
33     }]
34
35     def _real_extract(self, url):
36         mobj = re.match(self._VALID_URL, url)
37         path = mobj.group('path')
38         page_title = mobj.group('title')
39         info_url = u'http://cnn.com/video/data/3.0/%s/index.xml' % path
40         info = self._download_xml(info_url, page_title)
41
42         formats = []
43         rex = re.compile(r'''(?x)
44             (?P<width>[0-9]+)x(?P<height>[0-9]+)
45             (?:_(?P<bitrate>[0-9]+)k)?
46         ''')
47         for f in info.findall('files/file'):
48             video_url = 'http://ht.cdn.turner.com/cnn/big%s' % (f.text.strip())
49             fdct = {
50                 'format_id': f.attrib['bitrate'],
51                 'url': video_url,
52             }
53
54             mf = rex.match(f.attrib['bitrate'])
55             if mf:
56                 fdct['width'] = int(mf.group('width'))
57                 fdct['height'] = int(mf.group('height'))
58                 fdct['tbr'] = int_or_none(mf.group('bitrate'))
59             else:
60                 mf = rex.search(f.text)
61                 if mf:
62                     fdct['width'] = int(mf.group('width'))
63                     fdct['height'] = int(mf.group('height'))
64                     fdct['tbr'] = int_or_none(mf.group('bitrate'))
65                 else:
66                     mi = re.match(r'ios_(audio|[0-9]+)$', f.attrib['bitrate'])
67                     if mi:
68                         if mi.group(1) == 'audio':
69                             fdct['vcodec'] = 'none'
70                             fdct['ext'] = 'm4a'
71                         else:
72                             fdct['tbr'] = int(mi.group(1))
73
74             formats.append(fdct)
75
76         self._sort_formats(formats)
77
78         thumbnails = sorted([((int(t.attrib['height']),int(t.attrib['width'])), t.text) for t in info.findall('images/image')])
79         thumbs_dict = [{'resolution': res, 'url': t_url} for (res, t_url) in thumbnails]
80
81         metas_el = info.find('metas')
82         upload_date = (
83             metas_el.attrib.get('version') if metas_el is not None else None)
84
85         duration_el = info.find('length')
86         duration = parse_duration(duration_el.text)
87
88         return {
89             'id': info.attrib['id'],
90             'title': info.find('headline').text,
91             'formats': formats,
92             'thumbnail': thumbnails[-1][1],
93             'thumbnails': thumbs_dict,
94             'description': info.find('description').text,
95             'duration': duration,
96             'upload_date': upload_date,
97         }