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