Merge branch 'patch-1' of https://github.com/tuexss/youtube-dl into tuexss-patch-1
[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|hln|ktvk)(?:-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         "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://cnn.com/video/?/video/politics/2015/03/27/pkg-arizona-senator-church-attendance-mandatory.ktvk',
50         'only_matching': True,
51     }]
52
53     def _real_extract(self, url):
54         mobj = re.match(self._VALID_URL, url)
55         path = mobj.group('path')
56         page_title = mobj.group('title')
57         info_url = 'http://edition.cnn.com/video/data/3.0/%s/index.xml' % path
58         info = self._download_xml(info_url, page_title)
59
60         formats = []
61         rex = re.compile(r'''(?x)
62             (?P<width>[0-9]+)x(?P<height>[0-9]+)
63             (?:_(?P<bitrate>[0-9]+)k)?
64         ''')
65         for f in info.findall('files/file'):
66             video_url = 'http://ht.cdn.turner.com/cnn/big%s' % (f.text.strip())
67             fdct = {
68                 'format_id': f.attrib['bitrate'],
69                 'url': video_url,
70             }
71
72             mf = rex.match(f.attrib['bitrate'])
73             if mf:
74                 fdct['width'] = int(mf.group('width'))
75                 fdct['height'] = int(mf.group('height'))
76                 fdct['tbr'] = int_or_none(mf.group('bitrate'))
77             else:
78                 mf = rex.search(f.text)
79                 if mf:
80                     fdct['width'] = int(mf.group('width'))
81                     fdct['height'] = int(mf.group('height'))
82                     fdct['tbr'] = int_or_none(mf.group('bitrate'))
83                 else:
84                     mi = re.match(r'ios_(audio|[0-9]+)$', f.attrib['bitrate'])
85                     if mi:
86                         if mi.group(1) == 'audio':
87                             fdct['vcodec'] = 'none'
88                             fdct['ext'] = 'm4a'
89                         else:
90                             fdct['tbr'] = int(mi.group(1))
91
92             formats.append(fdct)
93
94         self._sort_formats(formats)
95
96         thumbnails = [{
97             'height': int(t.attrib['height']),
98             'width': int(t.attrib['width']),
99             'url': t.text,
100         } for t in info.findall('images/image')]
101
102         metas_el = info.find('metas')
103         upload_date = (
104             metas_el.attrib.get('version') if metas_el is not None else None)
105
106         duration_el = info.find('length')
107         duration = parse_duration(duration_el.text)
108
109         return {
110             'id': info.attrib['id'],
111             'title': info.find('headline').text,
112             'formats': formats,
113             'thumbnails': thumbnails,
114             'description': info.find('description').text,
115             'duration': duration,
116             'upload_date': upload_date,
117         }
118
119
120 class CNNBlogsIE(InfoExtractor):
121     _VALID_URL = r'https?://[^\.]+\.blogs\.cnn\.com/.+'
122     _TEST = {
123         'url': 'http://reliablesources.blogs.cnn.com/2014/02/09/criminalizing-journalism/',
124         'md5': '3e56f97b0b6ffb4b79f4ea0749551084',
125         'info_dict': {
126             'id': 'bestoftv/2014/02/09/criminalizing-journalism.cnn',
127             'ext': 'mp4',
128             'title': 'Criminalizing journalism?',
129             'description': 'Glenn Greenwald responds to comments made this week on Capitol Hill that journalists could be criminal accessories.',
130             'upload_date': '20140209',
131         },
132         'add_ie': ['CNN'],
133     }
134
135     def _real_extract(self, url):
136         webpage = self._download_webpage(url, url_basename(url))
137         cnn_url = self._html_search_regex(r'data-url="(.+?)"', webpage, 'cnn url')
138         return {
139             '_type': 'url',
140             'url': cnn_url,
141             'ie_key': CNNIE.ie_key(),
142         }
143
144
145 class CNNArticleIE(InfoExtractor):
146     _VALID_URL = r'https?://(?:(?:edition|www)\.)?cnn\.com/(?!video/)'
147     _TEST = {
148         'url': 'http://www.cnn.com/2014/12/21/politics/obama-north-koreas-hack-not-war-but-cyber-vandalism/',
149         'md5': '689034c2a3d9c6dc4aa72d65a81efd01',
150         'info_dict': {
151             'id': 'bestoftv/2014/12/21/ip-north-korea-obama.cnn',
152             'ext': 'mp4',
153             'title': 'Obama: Cyberattack not an act of war',
154             'description': 'md5:51ce6750450603795cad0cdfbd7d05c5',
155             'upload_date': '20141221',
156         },
157         'add_ie': ['CNN'],
158     }
159
160     def _real_extract(self, url):
161         webpage = self._download_webpage(url, url_basename(url))
162         cnn_url = self._html_search_regex(r"video:\s*'([^']+)'", webpage, 'cnn url')
163         return {
164             '_type': 'url',
165             'url': 'http://cnn.com/video/?/video/' + cnn_url,
166             'ie_key': CNNIE.ie_key(),
167         }