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