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