Release 2024.12.23
[yt-dlp.git] / yt_dlp / extractor / eighttracks.py
blob3ac4c56ae0fd8020fc748ec5a149cc57dd19226a
1 import json
2 import random
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
10 class EightTracksIE(InfoExtractor):
11 IE_NAME = '8tracks'
12 _VALID_URL = r'https?://8tracks\.com/(?P<user>[^/]+)/(?P<id>[^/#]+)(?:#.*)?$'
13 _TEST = {
14 'name': 'EightTracks',
15 'url': 'http://8tracks.com/ytdl/youtube-dl-test-tracks-a',
16 'info_dict': {
17 'id': '1336550',
18 'display_id': 'youtube-dl-test-tracks-a',
19 'description': "test chars: \"'/\\ä↭",
20 'title': "youtube-dl test tracks \"'/\\ä↭<>",
22 'playlist': [
24 'md5': '96ce57f24389fc8734ce47f4c1abcc55',
25 'info_dict': {
26 'id': '11885610',
27 'ext': 'm4a',
28 'title': "youtue-dl project<>\"' - youtube-dl test track 1 \"'/\\\u00e4\u21ad",
29 'uploader_id': 'ytdl',
33 'md5': '4ab26f05c1f7291ea460a3920be8021f',
34 'info_dict': {
35 'id': '11885608',
36 'ext': 'm4a',
37 'title': "youtube-dl project - youtube-dl test track 2 \"'/\\\u00e4\u21ad",
38 'uploader_id': 'ytdl',
42 'md5': 'd30b5b5f74217410f4689605c35d1fd7',
43 'info_dict': {
44 'id': '11885679',
45 'ext': 'm4a',
46 'title': "youtube-dl project as well - youtube-dl test track 3 \"'/\\\u00e4\u21ad",
47 'uploader_id': 'ytdl',
51 'md5': '4eb0a669317cd725f6bbd336a29f923a',
52 'info_dict': {
53 'id': '11885680',
54 'ext': 'm4a',
55 'title': "youtube-dl project as well - youtube-dl test track 4 \"'/\\\u00e4\u21ad",
56 'uploader_id': 'ytdl',
60 'md5': '1893e872e263a2705558d1d319ad19e8',
61 'info_dict': {
62 'id': '11885682',
63 'ext': 'm4a',
64 'title': "PH - youtube-dl test track 5 \"'/\\\u00e4\u21ad",
65 'uploader_id': 'ytdl',
69 'md5': 'b673c46f47a216ab1741ae8836af5899',
70 'info_dict': {
71 'id': '11885683',
72 'ext': 'm4a',
73 'title': "PH - youtube-dl test track 6 \"'/\\\u00e4\u21ad",
74 'uploader_id': 'ytdl',
78 'md5': '1d74534e95df54986da7f5abf7d842b7',
79 'info_dict': {
80 'id': '11885684',
81 'ext': 'm4a',
82 'title': "phihag - youtube-dl test track 7 \"'/\\\u00e4\u21ad",
83 'uploader_id': 'ytdl',
87 'md5': 'f081f47af8f6ae782ed131d38b9cd1c0',
88 'info_dict': {
89 'id': '11885685',
90 'ext': 'm4a',
91 'title': "phihag - youtube-dl test track 8 \"'/\\\u00e4\u21ad",
92 'uploader_id': 'ytdl',
98 def _real_extract(self, url):
99 playlist_id = self._match_id(url)
101 webpage = self._download_webpage(url, playlist_id)
103 data = self._parse_json(
104 self._search_regex(
105 r'(?s)PAGE\.mix\s*=\s*({.+?});\n', webpage, 'trax information'),
106 playlist_id)
108 session = str(random.randint(0, 1000000000))
109 mix_id = data['id']
110 track_count = data['tracks_count']
111 duration = data['duration']
112 avg_song_duration = float(duration) / track_count
113 # duration is sometimes negative, use predefined avg duration
114 if avg_song_duration <= 0:
115 avg_song_duration = 300
116 first_url = f'http://8tracks.com/sets/{session}/play?player=sm&mix_id={mix_id}&format=jsonh'
117 next_url = first_url
118 entries = []
120 for i in range(track_count):
121 api_json = None
122 download_tries = 0
124 while api_json is None:
125 try:
126 api_json = self._download_webpage(
127 next_url, playlist_id,
128 note='Downloading song information %d/%d' % (i + 1, track_count),
129 errnote='Failed to download song information')
130 except ExtractorError:
131 if download_tries > 3:
132 raise
133 else:
134 download_tries += 1
135 self._sleep(avg_song_duration, playlist_id)
137 api_data = json.loads(api_json)
138 track_data = api_data['set']['track']
139 info = {
140 'id': str(track_data['id']),
141 'url': track_data['track_file_stream_url'],
142 'title': track_data['performer'] + ' - ' + track_data['name'],
143 'raw_title': track_data['name'],
144 'uploader_id': data['user']['login'],
145 'ext': 'm4a',
147 entries.append(info)
149 next_url = 'http://8tracks.com/sets/{}/next?player=sm&mix_id={}&format=jsonh&track_id={}'.format(
150 session, mix_id, track_data['id'])
151 return {
152 '_type': 'playlist',
153 'entries': entries,
154 'id': str(mix_id),
155 'display_id': playlist_id,
156 'title': data.get('name'),
157 'description': data.get('description'),