]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/hypem.py
Initiate new release.
[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
8 from ..utils import (
9 ExtractorError,
10 sanitized_Request,
11 )
12
13
14 class HypemIE(InfoExtractor):
15 _VALID_URL = r'http://(?: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 data_encoded = compat_urllib_parse.urlencode(data)
32 complete_url = url + "?" + data_encoded
33 request = sanitized_Request(complete_url)
34 response, urlh = self._download_webpage_handle(
35 request, track_id, 'Downloading webpage with the url')
36 cookie = urlh.headers.get('Set-Cookie', '')
37
38 html_tracks = self._html_search_regex(
39 r'(?ms)<script type="application/json" id="displayList-data">\s*(.*?)\s*</script>',
40 response, 'tracks')
41 try:
42 track_list = json.loads(html_tracks)
43 track = track_list['tracks'][0]
44 except ValueError:
45 raise ExtractorError('Hypemachine contained invalid JSON.')
46
47 key = track['key']
48 track_id = track['id']
49 artist = track['artist']
50 title = track['song']
51
52 serve_url = "http://hypem.com/serve/source/%s/%s" % (track_id, key)
53 request = sanitized_Request(
54 serve_url, '', {'Content-Type': 'application/json'})
55 request.add_header('cookie', cookie)
56 song_data = self._download_json(request, track_id, 'Downloading metadata')
57 final_url = song_data["url"]
58
59 return {
60 'id': track_id,
61 'url': final_url,
62 'ext': 'mp3',
63 'title': title,
64 'uploader': artist,
65 }