0
0
Fork 0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-12-04 04:07:26 +00:00
yt-dlp/youtube_dl/extractor/hypem.py

68 lines
2.1 KiB
Python
Raw Normal View History

2014-06-09 20:30:57 +00:00
from __future__ import unicode_literals
2013-06-23 20:29:27 +00:00
import json
import time
from .common import InfoExtractor
from ..compat import (
2013-06-23 20:29:27 +00:00
compat_urllib_parse,
compat_urllib_request,
)
from ..utils import (
2013-06-23 20:29:27 +00:00
ExtractorError,
)
class HypemIE(InfoExtractor):
_VALID_URL = r'http://(?:www\.)?hypem\.com/track/(?P<id>[^/]+)/'
2013-06-27 18:46:46 +00:00
_TEST = {
2014-06-09 20:30:57 +00:00
'url': 'http://hypem.com/track/1v6ga/BODYWORK+-+TAME',
'md5': 'b9cc91b5af8995e9f0c1cee04c575828',
'info_dict': {
'id': '1v6ga',
'ext': 'mp3',
'title': 'Tame',
'uploader': 'BODYWORK',
2013-06-27 18:46:46 +00:00
}
}
2013-06-23 20:29:27 +00:00
def _real_extract(self, url):
track_id = self._match_id(url)
2013-06-23 20:29:27 +00:00
data = {'ax': 1, 'ts': time.time()}
2013-06-23 20:29:27 +00:00
data_encoded = compat_urllib_parse.urlencode(data)
complete_url = url + "?" + data_encoded
request = compat_urllib_request.Request(complete_url)
2014-06-09 20:30:57 +00:00
response, urlh = self._download_webpage_handle(
request, track_id, 'Downloading webpage with the url')
2013-06-23 20:29:27 +00:00
cookie = urlh.headers.get('Set-Cookie', '')
2014-06-09 20:30:57 +00:00
html_tracks = self._html_search_regex(
r'(?ms)<script type="application/json" id="displayList-data">\s*(.*?)\s*</script>',
response, 'tracks')
2013-06-23 20:29:27 +00:00
try:
track_list = json.loads(html_tracks)
2014-06-09 20:30:57 +00:00
track = track_list['tracks'][0]
2013-06-23 20:29:27 +00:00
except ValueError:
2014-06-09 20:30:57 +00:00
raise ExtractorError('Hypemachine contained invalid JSON.')
2013-06-23 20:29:27 +00:00
2014-06-09 20:30:57 +00:00
key = track['key']
track_id = track['id']
artist = track['artist']
title = track['song']
2013-06-23 20:29:27 +00:00
2014-06-09 20:30:57 +00:00
serve_url = "http://hypem.com/serve/source/%s/%s" % (track_id, key)
request = compat_urllib_request.Request(
serve_url, '', {'Content-Type': 'application/json'})
2013-06-23 20:29:27 +00:00
request.add_header('cookie', cookie)
2014-06-09 20:30:57 +00:00
song_data = self._download_json(request, track_id, 'Downloading metadata')
final_url = song_data["url"]
2013-06-23 20:29:27 +00:00
2014-06-09 20:30:57 +00:00
return {
'id': track_id,
'url': final_url,
'ext': 'mp3',
'title': title,
'uploader': artist,
}