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