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