3826ce7e1ee29bde4d640cb62bf2c127a83eb7bc
[youtube-dl] / 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     url_basename,
10 )
11
12
13 class CNNIE(InfoExtractor):
14     _VALID_URL = r'''(?x)https?://((edition|www)\.)?cnn\.com/video/(data/.+?|\?)/
15         (?P<path>.+?/(?P<title>[^/]+?)(?:\.cnn(-ap)?|(?=&)))'''
16
17     _TESTS = [{
18         'url': 'http://edition.cnn.com/video/?/video/sports/2013/06/09/nadal-1-on-1.cnn',
19         'md5': '3e6121ea48df7e2259fe73a0628605c4',
20         'info_dict': {
21             'id': 'sports_2013_06_09_nadal-1-on-1.cnn',
22             'ext': 'mp4',
23             'title': 'Nadal wins 8th French Open title',
24             'description': 'World Sport\'s Amanda Davies chats with 2013 French Open champion Rafael Nadal.',
25             'duration': 135,
26             'upload_date': '20130609',
27         },
28     },
29     {
30         "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",
31         "md5": "b5cc60c60a3477d185af8f19a2a26f4e",
32         "info_dict": {
33             'id': 'us/2013/08/21/sot-student-gives-epic-speech.georgia-institute-of-technology',
34             'ext': 'mp4',
35             "title": "Student's epic speech stuns new freshmen",
36             "description": "A Georgia Tech student welcomes the incoming freshmen with an epic speech backed by music from \"2001: A Space Odyssey.\"",
37             "upload_date": "20130821",
38         }
39     }]
40
41     def _real_extract(self, url):
42         mobj = re.match(self._VALID_URL, url)
43         path = mobj.group('path')
44         page_title = mobj.group('title')
45         info_url = 'http://cnn.com/video/data/3.0/%s/index.xml' % path
46         info = self._download_xml(info_url, page_title)
47
48         formats = []
49         rex = re.compile(r'''(?x)
50             (?P<width>[0-9]+)x(?P<height>[0-9]+)
51             (?:_(?P<bitrate>[0-9]+)k)?
52         ''')
53         for f in info.findall('files/file'):
54             video_url = 'http://ht.cdn.turner.com/cnn/big%s' % (f.text.strip())
55             fdct = {
56                 'format_id': f.attrib['bitrate'],
57                 'url': video_url,
58             }
59
60             mf = rex.match(f.attrib['bitrate'])
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                 mf = rex.search(f.text)
67                 if mf:
68                     fdct['width'] = int(mf.group('width'))
69                     fdct['height'] = int(mf.group('height'))
70                     fdct['tbr'] = int_or_none(mf.group('bitrate'))
71                 else:
72                     mi = re.match(r'ios_(audio|[0-9]+)$', f.attrib['bitrate'])
73                     if mi:
74                         if mi.group(1) == 'audio':
75                             fdct['vcodec'] = 'none'
76                             fdct['ext'] = 'm4a'
77                         else:
78                             fdct['tbr'] = int(mi.group(1))
79
80             formats.append(fdct)
81
82         self._sort_formats(formats)
83
84         thumbnails = [{
85             'height': int(t.attrib['height']),
86             'width': int(t.attrib['width']),
87             'url': t.text,
88         } for t in info.findall('images/image')]
89
90         metas_el = info.find('metas')
91         upload_date = (
92             metas_el.attrib.get('version') if metas_el is not None else None)
93
94         duration_el = info.find('length')
95         duration = parse_duration(duration_el.text)
96
97         return {
98             'id': info.attrib['id'],
99             'title': info.find('headline').text,
100             'formats': formats,
101             'thumbnails': thumbnails,
102             'description': info.find('description').text,
103             'duration': duration,
104             'upload_date': upload_date,
105         }
106
107
108 class CNNBlogsIE(InfoExtractor):
109     _VALID_URL = r'https?://[^\.]+\.blogs\.cnn\.com/.+'
110     _TEST = {
111         'url': 'http://reliablesources.blogs.cnn.com/2014/02/09/criminalizing-journalism/',
112         'md5': '3e56f97b0b6ffb4b79f4ea0749551084',
113         'info_dict': {
114             'id': 'bestoftv/2014/02/09/criminalizing-journalism.cnn',
115             'ext': 'mp4',
116             'title': 'Criminalizing journalism?',
117             'description': 'Glenn Greenwald responds to comments made this week on Capitol Hill that journalists could be criminal accessories.',
118             'upload_date': '20140209',
119         },
120         'add_ie': ['CNN'],
121     }
122
123     def _real_extract(self, url):
124         webpage = self._download_webpage(url, url_basename(url))
125         cnn_url = self._html_search_regex(r'data-url="(.+?)"', webpage, 'cnn url')
126         return {
127             '_type': 'url',
128             'url': cnn_url,
129             'ie_key': CNNIE.ie_key(),
130         }