[cnn] add support for money.cnn.com videos(closes #2797)
[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?://(?:(?P<sub_domain>edition|www|money)\.)?cnn\.com/(?:video/(?:data/.+?|\?)/)?videos?/
15         (?P<path>.+?/(?P<title>[^/]+?)(?:\.(?:[a-z\-]+)|(?=&)))'''
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         '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',
30         'md5': 'b5cc60c60a3477d185af8f19a2a26f4e',
31         'info_dict': {
32             'id': 'us/2013/08/21/sot-student-gives-epic-speech.georgia-institute-of-technology',
33             'ext': 'mp4',
34             'title': "Student's epic speech stuns new freshmen",
35             'description': "A Georgia Tech student welcomes the incoming freshmen with an epic speech backed by music from \"2001: A Space Odyssey.\"",
36             'upload_date': '20130821',
37         }
38     }, {
39         'url': 'http://www.cnn.com/video/data/2.0/video/living/2014/12/22/growing-america-nashville-salemtown-board-episode-1.hln.html',
40         'md5': 'f14d02ebd264df951feb2400e2c25a1b',
41         'info_dict': {
42             'id': 'living/2014/12/22/growing-america-nashville-salemtown-board-episode-1.hln',
43             'ext': 'mp4',
44             'title': 'Nashville Ep. 1: Hand crafted skateboards',
45             'description': 'md5:e7223a503315c9f150acac52e76de086',
46             'upload_date': '20141222',
47         }
48     }, {
49         'url': 'http://money.cnn.com/video/news/2016/08/19/netflix-stunning-stats.cnnmoney/index.html',
50         'md5': '52a515dc1b0f001cd82e4ceda32be9d1',
51         'info_dict': {
52             'id': '/video/news/2016/08/19/netflix-stunning-stats.cnnmoney',
53             'ext': 'mp4',
54             'title': '5 stunning stats about Netflix',
55             'description': 'Did you know that Netflix has more than 80 million members? Here are five facts about the online video distributor that you probably didn\'t know.',
56             'upload_date': '20160819',
57         }
58     }, {
59         'url': 'http://cnn.com/video/?/video/politics/2015/03/27/pkg-arizona-senator-church-attendance-mandatory.ktvk',
60         'only_matching': True,
61     }, {
62         'url': 'http://cnn.com/video/?/video/us/2015/04/06/dnt-baker-refuses-anti-gay-order.wkmg',
63         'only_matching': True,
64     }, {
65         'url': 'http://edition.cnn.com/videos/arts/2016/04/21/olympic-games-cultural-a-z-brazil.cnn',
66         'only_matching': True,
67     }]
68
69     _CONFIG = {
70         # http://edition.cnn.com/.element/apps/cvp/3.0/cfg/spider/cnn/expansion/config.xml
71         'edition': {
72             'data_src': 'http://edition.cnn.com/video/data/3.0/video/%s/index.xml',
73             'media_src': 'http://pmd.cdn.turner.com/cnn/big',
74         },
75         # http://money.cnn.com/.element/apps/cvp2/cfg/config.xml
76         'money': {
77             'data_src': 'http://money.cnn.com/video/data/4.0/video/%s.xml',
78             'media_src': 'http://ht3.cdn.turner.com/money/big',
79         },
80     }
81
82     def _real_extract(self, url):
83         sub_domain, path, page_title = re.match(self._VALID_URL, url).groups()
84         if sub_domain not in ('money', 'edition'):
85             sub_domain = 'edition'
86         config = self._CONFIG[sub_domain]
87         info_url = config['data_src'] % path
88         info = self._download_xml(info_url, page_title)
89
90         formats = []
91         rex = re.compile(r'''(?x)
92             (?P<width>[0-9]+)x(?P<height>[0-9]+)
93             (?:_(?P<bitrate>[0-9]+)k)?
94         ''')
95         for f in info.findall('files/file'):
96             video_url = config['media_src'] + f.text.strip()
97             fdct = {
98                 'format_id': f.attrib['bitrate'],
99                 'url': video_url,
100             }
101
102             mf = rex.match(f.attrib['bitrate'])
103             if mf:
104                 fdct['width'] = int(mf.group('width'))
105                 fdct['height'] = int(mf.group('height'))
106                 fdct['tbr'] = int_or_none(mf.group('bitrate'))
107             else:
108                 mf = rex.search(f.text)
109                 if mf:
110                     fdct['width'] = int(mf.group('width'))
111                     fdct['height'] = int(mf.group('height'))
112                     fdct['tbr'] = int_or_none(mf.group('bitrate'))
113                 else:
114                     mi = re.match(r'ios_(audio|[0-9]+)$', f.attrib['bitrate'])
115                     if mi:
116                         if mi.group(1) == 'audio':
117                             fdct['vcodec'] = 'none'
118                             fdct['ext'] = 'm4a'
119                         else:
120                             fdct['tbr'] = int(mi.group(1))
121
122             formats.append(fdct)
123
124         self._sort_formats(formats)
125
126         thumbnails = [{
127             'height': int(t.attrib['height']),
128             'width': int(t.attrib['width']),
129             'url': t.text,
130         } for t in info.findall('images/image')]
131
132         metas_el = info.find('metas')
133         upload_date = (
134             metas_el.attrib.get('version') if metas_el is not None else None)
135
136         duration_el = info.find('length')
137         duration = parse_duration(duration_el.text)
138
139         return {
140             'id': info.attrib['id'],
141             'title': info.find('headline').text,
142             'formats': formats,
143             'thumbnails': thumbnails,
144             'description': info.find('description').text,
145             'duration': duration,
146             'upload_date': upload_date,
147         }
148
149
150 class CNNBlogsIE(InfoExtractor):
151     _VALID_URL = r'https?://[^\.]+\.blogs\.cnn\.com/.+'
152     _TEST = {
153         'url': 'http://reliablesources.blogs.cnn.com/2014/02/09/criminalizing-journalism/',
154         'md5': '3e56f97b0b6ffb4b79f4ea0749551084',
155         'info_dict': {
156             'id': 'bestoftv/2014/02/09/criminalizing-journalism.cnn',
157             'ext': 'mp4',
158             'title': 'Criminalizing journalism?',
159             'description': 'Glenn Greenwald responds to comments made this week on Capitol Hill that journalists could be criminal accessories.',
160             'upload_date': '20140209',
161         },
162         'add_ie': ['CNN'],
163     }
164
165     def _real_extract(self, url):
166         webpage = self._download_webpage(url, url_basename(url))
167         cnn_url = self._html_search_regex(r'data-url="(.+?)"', webpage, 'cnn url')
168         return {
169             '_type': 'url',
170             'url': cnn_url,
171             'ie_key': CNNIE.ie_key(),
172         }
173
174
175 class CNNArticleIE(InfoExtractor):
176     _VALID_URL = r'https?://(?:(?:edition|www)\.)?cnn\.com/(?!videos?/)'
177     _TEST = {
178         'url': 'http://www.cnn.com/2014/12/21/politics/obama-north-koreas-hack-not-war-but-cyber-vandalism/',
179         'md5': '689034c2a3d9c6dc4aa72d65a81efd01',
180         'info_dict': {
181             'id': 'bestoftv/2014/12/21/ip-north-korea-obama.cnn',
182             'ext': 'mp4',
183             'title': 'Obama: Cyberattack not an act of war',
184             'description': 'md5:51ce6750450603795cad0cdfbd7d05c5',
185             'upload_date': '20141221',
186         },
187         'add_ie': ['CNN'],
188     }
189
190     def _real_extract(self, url):
191         webpage = self._download_webpage(url, url_basename(url))
192         cnn_url = self._html_search_regex(r"video:\s*'([^']+)'", webpage, 'cnn url')
193         return {
194             '_type': 'url',
195             'url': 'http://cnn.com/video/?/video/' + cnn_url,
196             'ie_key': CNNIE.ie_key(),
197         }