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