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