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.

70 lines
2.4 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. _TEST = {
  15. u'url': u'http://hypem.com/track/1v6ga/BODYWORK+-+TAME',
  16. u'file': u'1v6ga.mp3',
  17. u'md5': u'b9cc91b5af8995e9f0c1cee04c575828',
  18. u'info_dict': {
  19. u"title": u"Tame"
  20. }
  21. }
  22. def _real_extract(self, url):
  23. mobj = re.match(self._VALID_URL, url)
  24. if mobj is None:
  25. raise ExtractorError(u'Invalid URL: %s' % url)
  26. track_id = mobj.group(1)
  27. data = { 'ax': 1, 'ts': time.time() }
  28. data_encoded = compat_urllib_parse.urlencode(data)
  29. complete_url = url + "?" + data_encoded
  30. request = compat_urllib_request.Request(complete_url)
  31. response, urlh = self._download_webpage_handle(request, track_id, u'Downloading webpage with the url')
  32. cookie = urlh.headers.get('Set-Cookie', '')
  33. self.report_extraction(track_id)
  34. html_tracks = self._html_search_regex(r'<script type="application/json" id="displayList-data">(.*?)</script>',
  35. response, u'tracks', flags=re.MULTILINE|re.DOTALL).strip()
  36. try:
  37. track_list = json.loads(html_tracks)
  38. track = track_list[u'tracks'][0]
  39. except ValueError:
  40. raise ExtractorError(u'Hypemachine contained invalid JSON.')
  41. key = track[u"key"]
  42. track_id = track[u"id"]
  43. artist = track[u"artist"]
  44. title = track[u"song"]
  45. serve_url = "http://hypem.com/serve/source/%s/%s" % (compat_str(track_id), compat_str(key))
  46. request = compat_urllib_request.Request(serve_url, "" , {'Content-Type': 'application/json'})
  47. request.add_header('cookie', cookie)
  48. song_data_json = self._download_webpage(request, track_id, u'Downloading metadata')
  49. try:
  50. song_data = json.loads(song_data_json)
  51. except ValueError:
  52. raise ExtractorError(u'Hypemachine contained invalid JSON.')
  53. final_url = song_data[u"url"]
  54. return [{
  55. 'id': track_id,
  56. 'url': final_url,
  57. 'ext': "mp3",
  58. 'title': title,
  59. 'artist': artist,
  60. }]