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