[aol] restrict url regex and improve format extraction
[youtube-dl] / youtube_dl / extractor / aol.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_parse_qs,
9     compat_urllib_parse_urlparse,
10 )
11 from ..utils import (
12     ExtractorError,
13     int_or_none,
14     url_or_none,
15 )
16
17
18 class AolIE(InfoExtractor):
19     IE_NAME = 'aol.com'
20     _VALID_URL = r'(?:aol-video:|https?://(?:www\.)?aol\.com/video/(?:[^/]+/)*)(?P<id>[0-9a-f]+)'
21
22     _TESTS = [{
23         # video with 5min ID
24         'url': 'https://www.aol.com/video/view/u-s--official-warns-of-largest-ever-irs-phone-scam/518167793/',
25         'md5': '18ef68f48740e86ae94b98da815eec42',
26         'info_dict': {
27             'id': '518167793',
28             'ext': 'mp4',
29             'title': 'U.S. Official Warns Of \'Largest Ever\' IRS Phone Scam',
30             'description': 'A major phone scam has cost thousands of taxpayers more than $1 million, with less than a month until income tax returns are due to the IRS.',
31             'timestamp': 1395405060,
32             'upload_date': '20140321',
33             'uploader': 'Newsy Studio',
34         },
35         'params': {
36             # m3u8 download
37             'skip_download': True,
38         }
39     }, {
40         # video with vidible ID
41         'url': 'https://www.aol.com/video/view/netflix-is-raising-rates/5707d6b8e4b090497b04f706/',
42         'info_dict': {
43             'id': '5707d6b8e4b090497b04f706',
44             'ext': 'mp4',
45             'title': 'Netflix is Raising Rates',
46             'description': 'Netflix is rewarding millions of it’s long-standing members with an increase in cost. Veuer’s Carly Figueroa has more.',
47             'upload_date': '20160408',
48             'timestamp': 1460123280,
49             'uploader': 'Veuer',
50         },
51         'params': {
52             # m3u8 download
53             'skip_download': True,
54         }
55     }, {
56         'url': 'https://www.aol.com/video/view/park-bench-season-2-trailer/559a1b9be4b0c3bfad3357a7/',
57         'only_matching': True,
58     }, {
59         'url': 'https://www.aol.com/video/view/donald-trump-spokeswoman-tones-down-megyn-kelly-attacks/519442220/',
60         'only_matching': True,
61     }, {
62         'url': 'aol-video:5707d6b8e4b090497b04f706',
63         'only_matching': True,
64     }, {
65         'url': 'https://www.aol.com/video/playlist/PL8245/5ca79d19d21f1a04035db606/',
66         'only_matching': True,
67     }]
68
69     def _real_extract(self, url):
70         video_id = self._match_id(url)
71
72         response = self._download_json(
73             'https://feedapi.b2c.on.aol.com/v1.0/app/videos/aolon/%s/details' % video_id,
74             video_id)['response']
75         if response['statusText'] != 'Ok':
76             raise ExtractorError('%s said: %s' % (self.IE_NAME, response['statusText']), expected=True)
77
78         video_data = response['data']
79         formats = []
80         m3u8_url = url_or_none(video_data.get('videoMasterPlaylist'))
81         if m3u8_url:
82             formats.extend(self._extract_m3u8_formats(
83                 m3u8_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
84         for rendition in video_data.get('renditions', []):
85             video_url = url_or_none(rendition.get('url'))
86             if not video_url:
87                 continue
88             ext = rendition.get('format')
89             if ext == 'm3u8':
90                 formats.extend(self._extract_m3u8_formats(
91                     video_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
92             else:
93                 f = {
94                     'url': video_url,
95                     'format_id': rendition.get('quality'),
96                 }
97                 mobj = re.search(r'(\d+)x(\d+)', video_url)
98                 if mobj:
99                     f.update({
100                         'width': int(mobj.group(1)),
101                         'height': int(mobj.group(2)),
102                     })
103                 else:
104                     qs = compat_parse_qs(compat_urllib_parse_urlparse(video_url).query)
105                     f.update({
106                         'width': int_or_none(qs.get('w', [None])[0]),
107                         'height': int_or_none(qs.get('h', [None])[0]),
108                     })
109                 formats.append(f)
110         self._sort_formats(formats, ('width', 'height', 'tbr', 'format_id'))
111
112         return {
113             'id': video_id,
114             'title': video_data['title'],
115             'duration': int_or_none(video_data.get('duration')),
116             'timestamp': int_or_none(video_data.get('publishDate')),
117             'view_count': int_or_none(video_data.get('views')),
118             'description': video_data.get('description'),
119             'uploader': video_data.get('videoOwner'),
120             'formats': formats,
121         }