[newgrounds] Improve formats and uploader extraction (closes #13346)
[youtube-dl] / youtube_dl / extractor / newgrounds.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import int_or_none
5
6
7 class NewgroundsIE(InfoExtractor):
8     _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:audio/listen|portal/view)/(?P<id>[0-9]+)'
9     _TESTS = [{
10         'url': 'https://www.newgrounds.com/audio/listen/549479',
11         'md5': 'fe6033d297591288fa1c1f780386f07a',
12         'info_dict': {
13             'id': '549479',
14             'ext': 'mp3',
15             'title': 'B7 - BusMode',
16             'uploader': 'Burn7',
17         }
18     }, {
19         'url': 'https://www.newgrounds.com/portal/view/673111',
20         'md5': '3394735822aab2478c31b1004fe5e5bc',
21         'info_dict': {
22             'id': '673111',
23             'ext': 'mp4',
24             'title': 'Dancin',
25             'uploader': 'Squirrelman82',
26         },
27     }, {
28         # source format unavailable, additional mp4 formats
29         'url': 'http://www.newgrounds.com/portal/view/689400',
30         'info_dict': {
31             'id': '689400',
32             'ext': 'mp4',
33             'title': 'ZTV News Episode 8',
34             'uploader': 'BennettTheSage',
35         },
36         'params': {
37             'skip_download': True,
38         },
39     }]
40
41     def _real_extract(self, url):
42         media_id = self._match_id(url)
43
44         webpage = self._download_webpage(url, media_id)
45
46         title = self._html_search_regex(
47             r'<title>([^>]+)</title>', webpage, 'title')
48
49         video_url = self._parse_json(self._search_regex(
50             r'"url"\s*:\s*("[^"]+"),', webpage, ''), media_id)
51
52         formats = [{
53             'url': video_url,
54             'format_id': 'source',
55             'quality': 1,
56         }]
57
58         max_resolution = int_or_none(self._search_regex(
59             r'max_resolution["\']\s*:\s*(\d+)', webpage, 'max resolution',
60             default=None))
61         if max_resolution:
62             url_base = video_url.rpartition('.')[0]
63             for resolution in (360, 720, 1080):
64                 if resolution > max_resolution:
65                     break
66                 formats.append({
67                     'url': '%s.%dp.mp4' % (url_base, resolution),
68                     'format_id': '%dp' % resolution,
69                     'height': resolution,
70                 })
71
72         self._check_formats(formats, media_id)
73         self._sort_formats(formats)
74
75         uploader = self._html_search_regex(
76             r'(?:Author|Writer)\s*<a[^>]+>([^<]+)', webpage, 'uploader',
77             fatal=False)
78
79         return {
80             'id': media_id,
81             'title': title,
82             'uploader': uploader,
83             'formats': formats,
84         }