]>
Commit | Line | Data |
---|---|---|
82840042 PH |
1 | import json |
2 | import random | |
82840042 PH |
3 | |
4 | from .common import InfoExtractor | |
1cc79574 | 5 | from ..compat import ( |
0963f92f | 6 | compat_str, |
f71fdb0a PH |
7 | ) |
8 | from ..utils import ( | |
7b61ac3d | 9 | ExtractorError, |
82840042 PH |
10 | ) |
11 | ||
12 | ||
13 | class EightTracksIE(InfoExtractor): | |
14 | IE_NAME = '8tracks' | |
c0ade33e | 15 | _VALID_URL = r'https?://8tracks\.com/(?P<user>[^/]+)/(?P<id>[^/#]+)(?:#.*)?$' |
5c5de1c7 | 16 | _TEST = { |
611c1dd9 | 17 | 'name': 'EightTracks', |
7a5c1cfe | 18 | 'url': 'http://8tracks.com/ytdl/youtube-dl-test-tracks-a', |
611c1dd9 | 19 | 'info_dict': { |
0963f92f | 20 | 'id': '1336550', |
7a5c1cfe | 21 | 'display_id': 'youtube-dl-test-tracks-a', |
611c1dd9 | 22 | 'description': "test chars: \"'/\\ä↭", |
7a5c1cfe | 23 | 'title': "youtube-dl test tracks \"'/\\ä↭<>", |
0963f92f | 24 | }, |
611c1dd9 | 25 | 'playlist': [ |
5c5de1c7 | 26 | { |
611c1dd9 S |
27 | 'md5': '96ce57f24389fc8734ce47f4c1abcc55', |
28 | 'info_dict': { | |
29 | 'id': '11885610', | |
30 | 'ext': 'm4a', | |
7a5c1cfe | 31 | 'title': "youtue-dl project<>\"' - youtube-dl test track 1 \"'/\\\u00e4\u21ad", |
611c1dd9 | 32 | 'uploader_id': 'ytdl' |
5c5de1c7 PH |
33 | } |
34 | }, | |
35 | { | |
611c1dd9 S |
36 | 'md5': '4ab26f05c1f7291ea460a3920be8021f', |
37 | 'info_dict': { | |
38 | 'id': '11885608', | |
39 | 'ext': 'm4a', | |
7a5c1cfe | 40 | 'title': "youtube-dl project - youtube-dl test track 2 \"'/\\\u00e4\u21ad", |
611c1dd9 | 41 | 'uploader_id': 'ytdl' |
5c5de1c7 PH |
42 | } |
43 | }, | |
44 | { | |
611c1dd9 S |
45 | 'md5': 'd30b5b5f74217410f4689605c35d1fd7', |
46 | 'info_dict': { | |
47 | 'id': '11885679', | |
48 | 'ext': 'm4a', | |
7a5c1cfe | 49 | 'title': "youtube-dl project as well - youtube-dl test track 3 \"'/\\\u00e4\u21ad", |
611c1dd9 | 50 | 'uploader_id': 'ytdl' |
5c5de1c7 PH |
51 | } |
52 | }, | |
53 | { | |
611c1dd9 S |
54 | 'md5': '4eb0a669317cd725f6bbd336a29f923a', |
55 | 'info_dict': { | |
56 | 'id': '11885680', | |
57 | 'ext': 'm4a', | |
7a5c1cfe | 58 | 'title': "youtube-dl project as well - youtube-dl test track 4 \"'/\\\u00e4\u21ad", |
611c1dd9 | 59 | 'uploader_id': 'ytdl' |
5c5de1c7 PH |
60 | } |
61 | }, | |
62 | { | |
611c1dd9 S |
63 | 'md5': '1893e872e263a2705558d1d319ad19e8', |
64 | 'info_dict': { | |
65 | 'id': '11885682', | |
66 | 'ext': 'm4a', | |
7a5c1cfe | 67 | 'title': "PH - youtube-dl test track 5 \"'/\\\u00e4\u21ad", |
611c1dd9 | 68 | 'uploader_id': 'ytdl' |
5c5de1c7 PH |
69 | } |
70 | }, | |
71 | { | |
611c1dd9 S |
72 | 'md5': 'b673c46f47a216ab1741ae8836af5899', |
73 | 'info_dict': { | |
74 | 'id': '11885683', | |
75 | 'ext': 'm4a', | |
7a5c1cfe | 76 | 'title': "PH - youtube-dl test track 6 \"'/\\\u00e4\u21ad", |
611c1dd9 | 77 | 'uploader_id': 'ytdl' |
5c5de1c7 PH |
78 | } |
79 | }, | |
80 | { | |
611c1dd9 S |
81 | 'md5': '1d74534e95df54986da7f5abf7d842b7', |
82 | 'info_dict': { | |
83 | 'id': '11885684', | |
84 | 'ext': 'm4a', | |
7a5c1cfe | 85 | 'title': "phihag - youtube-dl test track 7 \"'/\\\u00e4\u21ad", |
611c1dd9 | 86 | 'uploader_id': 'ytdl' |
5c5de1c7 PH |
87 | } |
88 | }, | |
89 | { | |
611c1dd9 S |
90 | 'md5': 'f081f47af8f6ae782ed131d38b9cd1c0', |
91 | 'info_dict': { | |
92 | 'id': '11885685', | |
93 | 'ext': 'm4a', | |
7a5c1cfe | 94 | 'title': "phihag - youtube-dl test track 8 \"'/\\\u00e4\u21ad", |
611c1dd9 | 95 | 'uploader_id': 'ytdl' |
5c5de1c7 PH |
96 | } |
97 | } | |
98 | ] | |
99 | } | |
100 | ||
82840042 | 101 | def _real_extract(self, url): |
a7e01c43 | 102 | playlist_id = self._match_id(url) |
82840042 PH |
103 | |
104 | webpage = self._download_webpage(url, playlist_id) | |
105 | ||
05be67e7 S |
106 | data = self._parse_json( |
107 | self._search_regex( | |
108 | r"(?s)PAGE\.mix\s*=\s*({.+?});\n", webpage, 'trax information'), | |
109 | playlist_id) | |
82840042 PH |
110 | |
111 | session = str(random.randint(0, 1000000000)) | |
112 | mix_id = data['id'] | |
113 | track_count = data['tracks_count'] | |
7b61ac3d | 114 | duration = data['duration'] |
f71fdb0a | 115 | avg_song_duration = float(duration) / track_count |
85741b99 S |
116 | # duration is sometimes negative, use predefined avg duration |
117 | if avg_song_duration <= 0: | |
118 | avg_song_duration = 300 | |
82840042 PH |
119 | first_url = 'http://8tracks.com/sets/%s/play?player=sm&mix_id=%s&format=jsonh' % (session, mix_id) |
120 | next_url = first_url | |
0963f92f | 121 | entries = [] |
7b61ac3d | 122 | |
49a25557 | 123 | for i in range(track_count): |
7b61ac3d | 124 | api_json = None |
125 | download_tries = 0 | |
126 | ||
127 | while api_json is None: | |
128 | try: | |
129 | api_json = self._download_webpage( | |
130 | next_url, playlist_id, | |
131 | note='Downloading song information %d/%d' % (i + 1, track_count), | |
132 | errnote='Failed to download song information') | |
133 | except ExtractorError: | |
134 | if download_tries > 3: | |
135 | raise | |
136 | else: | |
754f0008 | 137 | download_tries += 1 |
f71fdb0a | 138 | self._sleep(avg_song_duration, playlist_id) |
7b61ac3d | 139 | |
82840042 | 140 | api_data = json.loads(api_json) |
0963f92f | 141 | track_data = api_data['set']['track'] |
82840042 | 142 | info = { |
0963f92f | 143 | 'id': compat_str(track_data['id']), |
82840042 | 144 | 'url': track_data['track_file_stream_url'], |
8865bdeb | 145 | 'title': track_data['performer'] + ' - ' + track_data['name'], |
82840042 PH |
146 | 'raw_title': track_data['name'], |
147 | 'uploader_id': data['user']['login'], | |
148 | 'ext': 'm4a', | |
149 | } | |
0963f92f | 150 | entries.append(info) |
7b61ac3d | 151 | |
0963f92f PH |
152 | next_url = 'http://8tracks.com/sets/%s/next?player=sm&mix_id=%s&format=jsonh&track_id=%s' % ( |
153 | session, mix_id, track_data['id']) | |
154 | return { | |
155 | '_type': 'playlist', | |
156 | 'entries': entries, | |
157 | 'id': compat_str(mix_id), | |
158 | 'display_id': playlist_id, | |
159 | 'title': data.get('name'), | |
160 | 'description': data.get('description'), | |
161 | } |