]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/audiomack.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / audiomack.py
1 import itertools
2 import time
3
4 from .common import InfoExtractor
5 from .soundcloud import SoundcloudIE
6 from ..compat import compat_str
7 from ..utils import (
8 ExtractorError,
9 url_basename,
10 )
11
12
13 class AudiomackIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?audiomack\.com/(?:song/|(?=.+/song/))(?P<id>[\w/-]+)'
15 IE_NAME = 'audiomack'
16 _TESTS = [
17 # hosted on audiomack
18 {
19 'url': 'http://www.audiomack.com/song/roosh-williams/extraordinary',
20 'info_dict':
21 {
22 'id': '310086',
23 'ext': 'mp3',
24 'uploader': 'Roosh Williams',
25 'title': 'Extraordinary'
26 }
27 },
28 # audiomack wrapper around soundcloud song
29 # Needs new test URL.
30 {
31 'add_ie': ['Soundcloud'],
32 'url': 'http://www.audiomack.com/song/hip-hop-daily/black-mamba-freestyle',
33 'info_dict': {
34 'id': '258901379',
35 'ext': 'mp3',
36 'description': 'mamba day freestyle for the legend Kobe Bryant ',
37 'title': 'Black Mamba Freestyle [Prod. By Danny Wolf]',
38 'uploader': 'ILOVEMAKONNEN',
39 'upload_date': '20160414',
40 },
41 'skip': 'Song has been removed from the site',
42 },
43 ]
44
45 def _real_extract(self, url):
46 # URLs end with [uploader name]/song/[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).replace('/song/', '/')
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 self.url_result(api_response['url'], SoundcloudIE.ie_key())
65
66 return {
67 'id': compat_str(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/|(?=.+/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': 11,
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 'info_dict': {
92 'title': 'PPP (Pistol P Project)',
93 'id': '837572',
94 },
95 'playlist': [{
96 'info_dict': {
97 'title': 'PPP (Pistol P Project) - 8. Real (prod by SYK SENSE )',
98 'id': '837576',
99 'ext': 'mp3',
100 'uploader': 'Lil Herb a.k.a. G Herbo',
101 }
102 }, {
103 'info_dict': {
104 'title': 'PPP (Pistol P Project) - 10. 4 Minutes Of Hell Part 4 (prod by DY OF 808 MAFIA)',
105 'id': '837580',
106 'ext': 'mp3',
107 'uploader': 'Lil Herb a.k.a. G Herbo',
108 }
109 }],
110 }
111 ]
112
113 def _real_extract(self, url):
114 # URLs end with [uploader name]/album/[uploader title]
115 # this title is whatever the user types in, and is rarely
116 # the proper song title. Real metadata is in the api response
117 album_url_tag = self._match_id(url).replace('/album/', '/')
118 result = {'_type': 'playlist', 'entries': []}
119 # There is no one endpoint for album metadata - instead it is included/repeated in each song's metadata
120 # Therefore we don't know how many songs the album has and must infi-loop until failure
121 for track_no in itertools.count():
122 # Get song's metadata
123 api_response = self._download_json(
124 'http://www.audiomack.com/api/music/url/album/%s/%d?extended=1&_=%d'
125 % (album_url_tag, track_no, time.time()), album_url_tag,
126 note='Querying song information (%d)' % (track_no + 1))
127
128 # Total failure, only occurs when url is totally wrong
129 # Won't happen in middle of valid playlist (next case)
130 if 'url' not in api_response or 'error' in api_response:
131 raise ExtractorError('Invalid url for track %d of album url %s' % (track_no, url))
132 # URL is good but song id doesn't exist - usually means end of playlist
133 elif not api_response['url']:
134 break
135 else:
136 # Pull out the album metadata and add to result (if it exists)
137 for resultkey, apikey in [('id', 'album_id'), ('title', 'album_title')]:
138 if apikey in api_response and resultkey not in result:
139 result[resultkey] = compat_str(api_response[apikey])
140 song_id = url_basename(api_response['url']).rpartition('.')[0]
141 result['entries'].append({
142 'id': compat_str(api_response.get('id', song_id)),
143 'uploader': api_response.get('artist'),
144 'title': api_response.get('title', song_id),
145 'url': api_response['url'],
146 })
147 return result