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