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