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