]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/eighttracks.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / eighttracks.py
1 import json
2 import random
3
4 from .common import InfoExtractor
5 from ..compat import (
6 compat_str,
7 )
8 from ..utils import (
9 ExtractorError,
10 )
11
12
13 class EightTracksIE(InfoExtractor):
14 IE_NAME = '8tracks'
15 _VALID_URL = r'https?://8tracks\.com/(?P<user>[^/]+)/(?P<id>[^/#]+)(?:#.*)?$'
16 _TEST = {
17 'name': 'EightTracks',
18 'url': 'http://8tracks.com/ytdl/youtube-dl-test-tracks-a',
19 'info_dict': {
20 'id': '1336550',
21 'display_id': 'youtube-dl-test-tracks-a',
22 'description': "test chars: \"'/\\ä↭",
23 'title': "youtube-dl test tracks \"'/\\ä↭<>",
24 },
25 'playlist': [
26 {
27 'md5': '96ce57f24389fc8734ce47f4c1abcc55',
28 'info_dict': {
29 'id': '11885610',
30 'ext': 'm4a',
31 'title': "youtue-dl project<>\"' - youtube-dl test track 1 \"'/\\\u00e4\u21ad",
32 'uploader_id': 'ytdl'
33 }
34 },
35 {
36 'md5': '4ab26f05c1f7291ea460a3920be8021f',
37 'info_dict': {
38 'id': '11885608',
39 'ext': 'm4a',
40 'title': "youtube-dl project - youtube-dl test track 2 \"'/\\\u00e4\u21ad",
41 'uploader_id': 'ytdl'
42 }
43 },
44 {
45 'md5': 'd30b5b5f74217410f4689605c35d1fd7',
46 'info_dict': {
47 'id': '11885679',
48 'ext': 'm4a',
49 'title': "youtube-dl project as well - youtube-dl test track 3 \"'/\\\u00e4\u21ad",
50 'uploader_id': 'ytdl'
51 }
52 },
53 {
54 'md5': '4eb0a669317cd725f6bbd336a29f923a',
55 'info_dict': {
56 'id': '11885680',
57 'ext': 'm4a',
58 'title': "youtube-dl project as well - youtube-dl test track 4 \"'/\\\u00e4\u21ad",
59 'uploader_id': 'ytdl'
60 }
61 },
62 {
63 'md5': '1893e872e263a2705558d1d319ad19e8',
64 'info_dict': {
65 'id': '11885682',
66 'ext': 'm4a',
67 'title': "PH - youtube-dl test track 5 \"'/\\\u00e4\u21ad",
68 'uploader_id': 'ytdl'
69 }
70 },
71 {
72 'md5': 'b673c46f47a216ab1741ae8836af5899',
73 'info_dict': {
74 'id': '11885683',
75 'ext': 'm4a',
76 'title': "PH - youtube-dl test track 6 \"'/\\\u00e4\u21ad",
77 'uploader_id': 'ytdl'
78 }
79 },
80 {
81 'md5': '1d74534e95df54986da7f5abf7d842b7',
82 'info_dict': {
83 'id': '11885684',
84 'ext': 'm4a',
85 'title': "phihag - youtube-dl test track 7 \"'/\\\u00e4\u21ad",
86 'uploader_id': 'ytdl'
87 }
88 },
89 {
90 'md5': 'f081f47af8f6ae782ed131d38b9cd1c0',
91 'info_dict': {
92 'id': '11885685',
93 'ext': 'm4a',
94 'title': "phihag - youtube-dl test track 8 \"'/\\\u00e4\u21ad",
95 'uploader_id': 'ytdl'
96 }
97 }
98 ]
99 }
100
101 def _real_extract(self, url):
102 playlist_id = self._match_id(url)
103
104 webpage = self._download_webpage(url, playlist_id)
105
106 data = self._parse_json(
107 self._search_regex(
108 r"(?s)PAGE\.mix\s*=\s*({.+?});\n", webpage, 'trax information'),
109 playlist_id)
110
111 session = str(random.randint(0, 1000000000))
112 mix_id = data['id']
113 track_count = data['tracks_count']
114 duration = data['duration']
115 avg_song_duration = float(duration) / track_count
116 # duration is sometimes negative, use predefined avg duration
117 if avg_song_duration <= 0:
118 avg_song_duration = 300
119 first_url = 'http://8tracks.com/sets/%s/play?player=sm&mix_id=%s&format=jsonh' % (session, mix_id)
120 next_url = first_url
121 entries = []
122
123 for i in range(track_count):
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:
137 download_tries += 1
138 self._sleep(avg_song_duration, playlist_id)
139
140 api_data = json.loads(api_json)
141 track_data = api_data['set']['track']
142 info = {
143 'id': compat_str(track_data['id']),
144 'url': track_data['track_file_stream_url'],
145 'title': track_data['performer'] + ' - ' + track_data['name'],
146 'raw_title': track_data['name'],
147 'uploader_id': data['user']['login'],
148 'ext': 'm4a',
149 }
150 entries.append(info)
151
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 }