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