You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.2 KiB

  1. import json
  2. import re
  3. import time
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_str,
  7. compat_urllib_parse,
  8. compat_urllib_request,
  9. ExtractorError,
  10. )
  11. class HypemIE(InfoExtractor):
  12. """Information Extractor for hypem"""
  13. _VALID_URL = r'(?:http://)?(?:www\.)?hypem\.com/track/([^/]+)/([^/]+)'
  14. def _real_extract(self, url):
  15. mobj = re.match(self._VALID_URL, url)
  16. if mobj is None:
  17. raise ExtractorError(u'Invalid URL: %s' % url)
  18. track_id = mobj.group(1)
  19. data = { 'ax': 1, 'ts': time.time() }
  20. data_encoded = compat_urllib_parse.urlencode(data)
  21. complete_url = url + "?" + data_encoded
  22. request = compat_urllib_request.Request(complete_url)
  23. response, urlh = self._download_webpage_handle(request, track_id, u'Downloading webpage with the url')
  24. cookie = urlh.headers.get('Set-Cookie', '')
  25. self.report_extraction(track_id)
  26. html_tracks = self._html_search_regex(r'<script type="application/json" id="displayList-data">(.*?)</script>',
  27. response, u'tracks', flags=re.MULTILINE|re.DOTALL).strip()
  28. try:
  29. track_list = json.loads(html_tracks)
  30. track = track_list[u'tracks'][0]
  31. except ValueError:
  32. raise ExtractorError(u'Hypemachine contained invalid JSON.')
  33. key = track[u"key"]
  34. track_id = track[u"id"]
  35. artist = track[u"artist"]
  36. title = track[u"song"]
  37. serve_url = "http://hypem.com/serve/source/%s/%s" % (compat_str(track_id), compat_str(key))
  38. request = compat_urllib_request.Request(serve_url, "" , {'Content-Type': 'application/json'})
  39. request.add_header('cookie', cookie)
  40. song_data_json = self._download_webpage(request, track_id, u'Downloading metadata')
  41. try:
  42. song_data = json.loads(song_data_json)
  43. except ValueError:
  44. raise ExtractorError(u'Hypemachine contained invalid JSON.')
  45. final_url = song_data[u"url"]
  46. return [{
  47. 'id': track_id,
  48. 'url': final_url,
  49. 'ext': "mp3",
  50. 'title': title,
  51. 'artist': artist,
  52. }]