]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/hypem.py
New upstream version 2018.03.14
[youtubedl] / youtube_dl / extractor / hypem.py
1 from __future__ import unicode_literals
2
3 import json
4 import time
5
6 from .common import InfoExtractor
7 from ..compat import compat_urllib_parse_urlencode
8 from ..utils import (
9 ExtractorError,
10 sanitized_Request,
11 )
12
13
14 class HypemIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:www\.)?hypem\.com/track/(?P<id>[^/]+)/'
16 _TEST = {
17 'url': 'http://hypem.com/track/1v6ga/BODYWORK+-+TAME',
18 'md5': 'b9cc91b5af8995e9f0c1cee04c575828',
19 'info_dict': {
20 'id': '1v6ga',
21 'ext': 'mp3',
22 'title': 'Tame',
23 'uploader': 'BODYWORK',
24 }
25 }
26
27 def _real_extract(self, url):
28 track_id = self._match_id(url)
29
30 data = {'ax': 1, 'ts': time.time()}
31 request = sanitized_Request(url + '?' + compat_urllib_parse_urlencode(data))
32 response, urlh = self._download_webpage_handle(
33 request, track_id, 'Downloading webpage with the url')
34
35 html_tracks = self._html_search_regex(
36 r'(?ms)<script type="application/json" id="displayList-data">(.+?)</script>',
37 response, 'tracks')
38 try:
39 track_list = json.loads(html_tracks)
40 track = track_list['tracks'][0]
41 except ValueError:
42 raise ExtractorError('Hypemachine contained invalid JSON.')
43
44 key = track['key']
45 track_id = track['id']
46 title = track['song']
47
48 request = sanitized_Request(
49 'http://hypem.com/serve/source/%s/%s' % (track_id, key),
50 '', {'Content-Type': 'application/json'})
51 song_data = self._download_json(request, track_id, 'Downloading metadata')
52 final_url = song_data['url']
53 artist = track.get('artist')
54
55 return {
56 'id': track_id,
57 'url': final_url,
58 'ext': 'mp3',
59 'title': title,
60 'uploader': artist,
61 }