2 from __future__ import unicode_literals
4 from .common import InfoExtractor
5 from .soundcloud import SoundcloudIE
6 from ..utils import ExtractorError
11 class AudiomackIE(InfoExtractor):
12 _VALID_URL = r'https?://(?:www\.)?audiomack\.com/(song)/(?P<id>[\w/-]+)'
17 'url': 'http://www.audiomack.com/song/roosh-williams/extraordinary',
22 "artist": "Roosh Williams",
23 'title': 'Extraordinary'
26 # audiomack through soundcloud
28 'add_ie': ['Soundcloud'],
29 'url': 'http://www.audiomack.com/song/xclusiveszone/take-kare',
33 'description': 'md5:1fc3272ed7a635cce5be1568c2822997',
34 'title': 'Young Thug ft Lil Wayne - Take Kare',
35 'uploader': 'Young Thug World',
36 'upload_date': '20141016',
42 def create_song_dictionary(api_response, album_url_tag, track_no=0):
43 # All keys are the same in audiomack api and InfoExtractor format
44 entry = {key: api_response[key] for key in ["title", "artist", "id", "url"] if key in api_response}
45 # Fudge values in the face of missing metadata
47 entry["id"] = track_no
48 if "title" not in entry:
49 entry["title"] = album_url_tag
52 def _real_extract(self, url):
53 # URLs end with [uploader name]/[uploader title]
54 # this title is whatever the user types in, and is rarely
55 # the proper song title. Real metadata is in the api response
56 album_url_tag = self._match_id(url)
58 # Request the extended version of the api for extra fields like artist and title
59 api_response = self._download_json(
60 "http://www.audiomack.com/api/music/url/song/%s?extended=1&_=%d" % (
61 album_url_tag, time.time()),
64 # API is inconsistent with errors
65 if "url" not in api_response or not api_response["url"] or "error" in api_response:
66 raise ExtractorError("Invalid url %s", url)
68 # Audiomack wraps a lot of soundcloud tracks in their branded wrapper
69 # if so, pass the work off to the soundcloud extractor
70 if SoundcloudIE.suitable(api_response["url"]):
71 return {'_type': 'url', 'url': api_response["url"], 'ie_key': 'Soundcloud'}
73 return self.create_song_dictionary(api_response, album_url_tag)
76 class AudiomackAlbumIE(InfoExtractor):
77 _VALID_URL = r'https?://(?:www\.)?audiomack\.com/album/(?P<id>[\w/-]+)'
78 IE_NAME = 'audiomack:album'
80 # Standard album playlist
82 'url': 'http://www.audiomack.com/album/flytunezcom/tha-tour-part-2-mixtape',
87 'title': "Tha Tour: Part 2 (Official Mixtape)"
90 # Album playlist ripped from fakeshoredrive with no metadata
92 "url": "http://www.audiomack.com/album/fakeshoredrive/ppp-pistol-p-project",
97 def _real_extract(self, url):
98 # URLs end with [uploader name]/[uploader title]
99 # this title is whatever the user types in, and is rarely
100 # the proper song title. Real metadata is in the api response
101 album_url_tag = self._match_id(url)
102 result = {"_type": "playlist", "entries": []}
103 # There is no one endpoint for album metadata - instead it is included/repeated in each song's metadata
104 # Therefore we don't know how many songs the album has and must infi-loop until failure
107 # Get song's metadata
108 api_response = self._download_json("http://www.audiomack.com/api/music/url/album/%s/%d?extended=1&_=%d"
109 % (album_url_tag, track_no, time.time()), album_url_tag)
111 # Total failure, only occurs when url is totally wrong
112 # Won't happen in middle of valid playlist (next case)
113 if "url" not in api_response or "error" in api_response:
114 raise ExtractorError("Invalid url for track %d of album url %s" % (track_no, url))
115 # URL is good but song id doesn't exist - usually means end of playlist
116 elif not api_response["url"]:
119 # Pull out the album metadata and add to result (if it exists)
120 for resultkey, apikey in [("id", "album_id"), ("title", "album_title")]:
121 if apikey in api_response and resultkey not in result:
122 result[resultkey] = api_response[apikey]
123 result["entries"].append(AudiomackIE.create_song_dictionary(api_response, album_url_tag, track_no))