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.

51 lines
1.9 KiB

  1. import itertools
  2. import json
  3. import random
  4. import re
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. ExtractorError,
  8. )
  9. class EightTracksIE(InfoExtractor):
  10. IE_NAME = '8tracks'
  11. _VALID_URL = r'https?://8tracks.com/(?P<user>[^/]+)/(?P<id>[^/#]+)(?:#.*)?$'
  12. def _real_extract(self, url):
  13. mobj = re.match(self._VALID_URL, url)
  14. if mobj is None:
  15. raise ExtractorError(u'Invalid URL: %s' % url)
  16. playlist_id = mobj.group('id')
  17. webpage = self._download_webpage(url, playlist_id)
  18. json_like = self._search_regex(r"PAGE.mix = (.*?);\n", webpage, u'trax information', flags=re.DOTALL)
  19. data = json.loads(json_like)
  20. session = str(random.randint(0, 1000000000))
  21. mix_id = data['id']
  22. track_count = data['tracks_count']
  23. first_url = 'http://8tracks.com/sets/%s/play?player=sm&mix_id=%s&format=jsonh' % (session, mix_id)
  24. next_url = first_url
  25. res = []
  26. for i in itertools.count():
  27. api_json = self._download_webpage(next_url, playlist_id,
  28. note=u'Downloading song information %s/%s' % (str(i+1), track_count),
  29. errnote=u'Failed to download song information')
  30. api_data = json.loads(api_json)
  31. track_data = api_data[u'set']['track']
  32. info = {
  33. 'id': track_data['id'],
  34. 'url': track_data['track_file_stream_url'],
  35. 'title': track_data['performer'] + u' - ' + track_data['name'],
  36. 'raw_title': track_data['name'],
  37. 'uploader_id': data['user']['login'],
  38. 'ext': 'm4a',
  39. }
  40. res.append(info)
  41. if api_data['set']['at_last_track']:
  42. break
  43. next_url = 'http://8tracks.com/sets/%s/next?player=sm&mix_id=%s&format=jsonh&track_id=%s' % (session, mix_id, track_data['id'])
  44. return res