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