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