]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/cnn.py
[prosiebensat1] Add support for .at domain names (Closes #5786)
[yt-dlp.git] / youtube_dl / extractor / cnn.py
CommitLineData
3798eadc
PH
1from __future__ import unicode_literals
2
1a582dd4 3import re
1a582dd4
JMF
4
5from .common import InfoExtractor
608d11f5
PH
6from ..utils import (
7 int_or_none,
8 parse_duration,
0ae6b019 9 url_basename,
608d11f5 10)
1a582dd4 11
273f603e 12
1a582dd4 13class CNNIE(InfoExtractor):
7fc2cd81 14 _VALID_URL = r'''(?x)https?://(?:(?:edition|www)\.)?cnn\.com/video/(?:data/.+?|\?)/
29713e42 15 (?P<path>.+?/(?P<title>[^/]+?)(?:\.(?:[a-z]{3,5})(?:-ap)?|(?=&)))'''
1a582dd4 16
273f603e 17 _TESTS = [{
3798eadc 18 'url': 'http://edition.cnn.com/video/?/video/sports/2013/06/09/nadal-1-on-1.cnn',
3798eadc
PH
19 'md5': '3e6121ea48df7e2259fe73a0628605c4',
20 'info_dict': {
7fc2cd81 21 'id': 'sports/2013/06/09/nadal-1-on-1.cnn',
acd40f64 22 'ext': 'mp4',
3798eadc
PH
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',
1a582dd4 27 },
9e1a5b84 28 }, {
877bfd69 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",
877bfd69
PH
30 "md5": "b5cc60c60a3477d185af8f19a2a26f4e",
31 "info_dict": {
acd40f64
PH
32 'id': 'us/2013/08/21/sot-student-gives-epic-speech.georgia-institute-of-technology',
33 'ext': 'mp4',
877bfd69
PH
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",
273f603e 37 }
429ddfd3
S
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 }
ecb750a4
PH
48 }, {
49 'url': 'http://cnn.com/video/?/video/politics/2015/03/27/pkg-arizona-senator-church-attendance-mandatory.ktvk',
50 'only_matching': True,
29713e42
PH
51 }, {
52 'url': 'http://cnn.com/video/?/video/us/2015/04/06/dnt-baker-refuses-anti-gay-order.wkmg',
53 'only_matching': True,
273f603e 54 }]
1a582dd4
JMF
55
56 def _real_extract(self, url):
57 mobj = re.match(self._VALID_URL, url)
58 path = mobj.group('path')
59 page_title = mobj.group('title')
efcddaeb 60 info_url = 'http://edition.cnn.com/video/data/3.0/%s/index.xml' % path
e26f8712 61 info = self._download_xml(info_url, page_title)
1a582dd4
JMF
62
63 formats = []
608d11f5
PH
64 rex = re.compile(r'''(?x)
65 (?P<width>[0-9]+)x(?P<height>[0-9]+)
66 (?:_(?P<bitrate>[0-9]+)k)?
67 ''')
1a582dd4 68 for f in info.findall('files/file'):
608d11f5
PH
69 video_url = 'http://ht.cdn.turner.com/cnn/big%s' % (f.text.strip())
70 fdct = {
71 'format_id': f.attrib['bitrate'],
72 'url': video_url,
73 }
74
75 mf = rex.match(f.attrib['bitrate'])
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 mf = rex.search(f.text)
82 if mf:
83 fdct['width'] = int(mf.group('width'))
84 fdct['height'] = int(mf.group('height'))
85 fdct['tbr'] = int_or_none(mf.group('bitrate'))
86 else:
87 mi = re.match(r'ios_(audio|[0-9]+)$', f.attrib['bitrate'])
88 if mi:
89 if mi.group(1) == 'audio':
90 fdct['vcodec'] = 'none'
91 fdct['ext'] = 'm4a'
92 else:
93 fdct['tbr'] = int(mi.group(1))
94
95 formats.append(fdct)
96
97 self._sort_formats(formats)
1a582dd4 98
be6d7229
PH
99 thumbnails = [{
100 'height': int(t.attrib['height']),
101 'width': int(t.attrib['width']),
102 'url': t.text,
103 } for t in info.findall('images/image')]
1a582dd4 104
608d11f5
PH
105 metas_el = info.find('metas')
106 upload_date = (
107 metas_el.attrib.get('version') if metas_el is not None else None)
108
109 duration_el = info.find('length')
110 duration = parse_duration(duration_el.text)
111
112 return {
113 'id': info.attrib['id'],
114 'title': info.find('headline').text,
115 'formats': formats,
be6d7229 116 'thumbnails': thumbnails,
608d11f5
PH
117 'description': info.find('description').text,
118 'duration': duration,
119 'upload_date': upload_date,
120 }
0ae6b019
JMF
121
122
123class CNNBlogsIE(InfoExtractor):
124 _VALID_URL = r'https?://[^\.]+\.blogs\.cnn\.com/.+'
125 _TEST = {
126 'url': 'http://reliablesources.blogs.cnn.com/2014/02/09/criminalizing-journalism/',
127 'md5': '3e56f97b0b6ffb4b79f4ea0749551084',
128 'info_dict': {
129 'id': 'bestoftv/2014/02/09/criminalizing-journalism.cnn',
130 'ext': 'mp4',
131 'title': 'Criminalizing journalism?',
132 'description': 'Glenn Greenwald responds to comments made this week on Capitol Hill that journalists could be criminal accessories.',
133 'upload_date': '20140209',
134 },
135 'add_ie': ['CNN'],
136 }
137
138 def _real_extract(self, url):
139 webpage = self._download_webpage(url, url_basename(url))
140 cnn_url = self._html_search_regex(r'data-url="(.+?)"', webpage, 'cnn url')
141 return {
142 '_type': 'url',
143 'url': cnn_url,
144 'ie_key': CNNIE.ie_key(),
145 }
9532d723
AK
146
147
148class CNNArticleIE(InfoExtractor):
7fc2cd81 149 _VALID_URL = r'https?://(?:(?:edition|www)\.)?cnn\.com/(?!video/)'
9532d723
AK
150 _TEST = {
151 'url': 'http://www.cnn.com/2014/12/21/politics/obama-north-koreas-hack-not-war-but-cyber-vandalism/',
5fe51125 152 'md5': '689034c2a3d9c6dc4aa72d65a81efd01',
9532d723 153 'info_dict': {
5fe51125 154 'id': 'bestoftv/2014/12/21/ip-north-korea-obama.cnn',
9532d723 155 'ext': 'mp4',
5fe51125
JMF
156 'title': 'Obama: Cyberattack not an act of war',
157 'description': 'md5:51ce6750450603795cad0cdfbd7d05c5',
158 'upload_date': '20141221',
9532d723
AK
159 },
160 'add_ie': ['CNN'],
161 }
162
163 def _real_extract(self, url):
164 webpage = self._download_webpage(url, url_basename(url))
165 cnn_url = self._html_search_regex(r"video:\s*'([^']+)'", webpage, 'cnn url')
166 return {
167 '_type': 'url',
7fc2cd81 168 'url': 'http://cnn.com/video/?/video/' + cnn_url,
9532d723
AK
169 'ie_key': CNNIE.ie_key(),
170 }