ytdl-nightly/youtube_dl/extractor/hypem.py

69 lines
2.1 KiB
Python
Raw Normal View History

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