]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/audiomack.py
8bfe502143a2cbf9c076bdabaad3e7ad5d2090b6
[youtubedl] / youtube_dl / extractor / audiomack.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5 import time
6
7 from .common import InfoExtractor
8 from .soundcloud import SoundcloudIE
9 from ..utils import (
10 ExtractorError,
11 url_basename,
12 )
13
14
15 class AudiomackIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:www\.)?audiomack\.com/song/(?P<id>[\w/-]+)'
17 IE_NAME = 'audiomack'
18 _TESTS = [
19 # hosted on audiomack
20 {
21 'url': 'http://www.audiomack.com/song/roosh-williams/extraordinary',
22 'info_dict':
23 {
24 'id': '310086',
25 'ext': 'mp3',
26 'uploader': 'Roosh Williams',
27 'title': 'Extraordinary'
28 }
29 },
30 # audiomack wrapper around soundcloud song
31 {
32 'add_ie': ['Soundcloud'],
33 'url': 'http://www.audiomack.com/song/xclusiveszone/take-kare',
34 'info_dict': {
35 'id': '172419696',
36 'ext': 'mp3',
37 'description': 'md5:1fc3272ed7a635cce5be1568c2822997',
38 'title': 'Young Thug ft Lil Wayne - Take Kare',
39 'uploader': 'Young Thug World',
40 'upload_date': '20141016',
41 }
42 },
43 ]
44
45 def _real_extract(self, url):
46 # URLs end with [uploader name]/[uploader title]
47 # this title is whatever the user types in, and is rarely
48 # the proper song title. Real metadata is in the api response
49 album_url_tag = self._match_id(url)
50
51 # Request the extended version of the api for extra fields like artist and title
52 api_response = self._download_json(
53 'http://www.audiomack.com/api/music/url/song/%s?extended=1&_=%d' % (
54 album_url_tag, time.time()),
55 album_url_tag)
56
57 # API is inconsistent with errors
58 if 'url' not in api_response or not api_response['url'] or 'error' in api_response:
59 raise ExtractorError('Invalid url %s', url)
60
61 # Audiomack wraps a lot of soundcloud tracks in their branded wrapper
62 # if so, pass the work off to the soundcloud extractor
63 if SoundcloudIE.suitable(api_response['url']):
64 return {'_type': 'url', 'url': api_response['url'], 'ie_key': 'Soundcloud'}
65
66 return {
67 'id': api_response.get('id', album_url_tag),
68 'uploader': api_response.get('artist'),
69 'title': api_response.get('title'),
70 'url': api_response['url'],
71 }
72
73
74 class AudiomackAlbumIE(InfoExtractor):
75 _VALID_URL = r'https?://(?:www\.)?audiomack\.com/album/(?P<id>[\w/-]+)'
76 IE_NAME = 'audiomack:album'
77 _TESTS = [
78 # Standard album playlist
79 {
80 'url': 'http://www.audiomack.com/album/flytunezcom/tha-tour-part-2-mixtape',
81 'playlist_count': 15,
82 'info_dict':
83 {
84 'id': '812251',
85 'title': 'Tha Tour: Part 2 (Official Mixtape)'
86 }
87 },
88 # Album playlist ripped from fakeshoredrive with no metadata
89 {
90 'url': 'http://www.audiomack.com/album/fakeshoredrive/ppp-pistol-p-project',
91 'playlist': [{
92 'info_dict': {
93 'title': '9.-heaven-or-hell-chimaca-ft-zuse-prod-by-dj-fu',
94 'id': '9.-heaven-or-hell-chimaca-ft-zuse-prod-by-dj-fu',
95 'ext': 'mp3',
96 }
97 }],
98 'params': {
99 'playliststart': 8,
100 'playlistend': 8,
101 }
102 }
103 ]
104
105 def _real_extract(self, url):
106 # URLs end with [uploader name]/[uploader title]
107 # this title is whatever the user types in, and is rarely
108 # the proper song title. Real metadata is in the api response
109 album_url_tag = self._match_id(url)
110 result = {'_type': 'playlist', 'entries': []}
111 # There is no one endpoint for album metadata - instead it is included/repeated in each song's metadata
112 # Therefore we don't know how many songs the album has and must infi-loop until failure
113 for track_no in itertools.count():
114 # Get song's metadata
115 api_response = self._download_json(
116 'http://www.audiomack.com/api/music/url/album/%s/%d?extended=1&_=%d'
117 % (album_url_tag, track_no, time.time()), album_url_tag,
118 note='Querying song information (%d)' % (track_no + 1))
119
120 # Total failure, only occurs when url is totally wrong
121 # Won't happen in middle of valid playlist (next case)
122 if 'url' not in api_response or 'error' in api_response:
123 raise ExtractorError('Invalid url for track %d of album url %s' % (track_no, url))
124 # URL is good but song id doesn't exist - usually means end of playlist
125 elif not api_response['url']:
126 break
127 else:
128 # Pull out the album metadata and add to result (if it exists)
129 for resultkey, apikey in [('id', 'album_id'), ('title', 'album_title')]:
130 if apikey in api_response and resultkey not in result:
131 result[resultkey] = api_response[apikey]
132 song_id = url_basename(api_response['url']).rpartition('.')[0]
133 result['entries'].append({
134 'id': api_response.get('id', song_id),
135 'uploader': api_response.get('artist'),
136 'title': api_response.get('title', song_id),
137 'url': api_response['url'],
138 })
139 return result