[newgrounds] Simplify
[youtube-dl] / youtube_dl / extractor / newgrounds.py
1 from __future__ import unicode_literals
2
3 import json
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import determine_ext
8
9
10 class NewgroundsIE(InfoExtractor):
11     _VALID_URL = r'(?:https?://)?(?:www\.)?newgrounds\.com/audio/listen/(?P<id>\d+)'
12     _TEST = {
13         'url': 'http://www.newgrounds.com/audio/listen/549479',
14         'file': '549479.mp3',
15         'md5': 'fe6033d297591288fa1c1f780386f07a',
16         'info_dict': {
17             "title": "B7 - BusMode",
18             "uploader": "Burn7",
19         }
20     }
21
22     def _real_extract(self, url):
23         mobj = re.match(self._VALID_URL, url)
24         music_id = mobj.group('id')
25         webpage = self._download_webpage(url, music_id)
26         
27         title = self._html_search_regex(
28             r',"name":"([^"]+)",', webpage, 'music title')
29         uploader = self._html_search_regex(
30             r',"artist":"([^"]+)",', webpage, 'music uploader')
31         
32         music_url_json_string = self._html_search_regex(
33             r'({"url":"[^"]+"),', webpage, 'music url') + '}'
34         music_url_json = json.loads(music_url_json_string)
35         music_url = music_url_json['url']
36
37         return {
38             'id': music_id,
39             'title': title,
40             'url': music_url,
41             'uploader': uploader,
42         }