]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/audiomack.py
debian/patch: Refresh all patches to avoid FTBFS.
[youtubedl] / youtube_dl / extractor / audiomack.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5 import time
6
7 from .common import InfoExtractor
8 from .soundcloud import SoundcloudIE
9 from ..compat import compat_str
10 from ..utils import (
11 ExtractorError,
12 url_basename,
13 )
14
15
16 class AudiomackIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?audiomack\.com/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 },
44 ]
45
46 def _real_extract(self, url):
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)
51
52 # Request the extended version of the api for extra fields like artist and title
53 api_response = self._download_json(
54 'http://www.audiomack.com/api/music/url/song/%s?extended=1&_=%d' % (
55 album_url_tag, time.time()),
56 album_url_tag)
57
58 # API is inconsistent with errors
59 if 'url' not in api_response or not api_response['url'] or 'error' in api_response:
60 raise ExtractorError('Invalid url %s' % url)
61
62 # Audiomack wraps a lot of soundcloud tracks in their branded wrapper
63 # if so, pass the work off to the soundcloud extractor
64 if SoundcloudIE.suitable(api_response['url']):
65 return {'_type': 'url', 'url': api_response['url'], 'ie_key': 'Soundcloud'}
66
67 return {
68 'id': compat_str(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 }
73
74
75 class 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',
82 'playlist_count': 15,
83 'info_dict':
84 {
85 'id': '812251',
86 'title': 'Tha Tour: Part 2 (Official Mixtape)'
87 }
88 },
89 # Album playlist ripped from fakeshoredrive with no metadata
90 {
91 'url': 'http://www.audiomack.com/album/fakeshoredrive/ppp-pistol-p-project',
92 'info_dict': {
93 'title': 'PPP (Pistol P Project)',
94 'id': '837572',
95 },
96 'playlist': [{
97 'info_dict': {
98 'title': 'PPP (Pistol P Project) - 9. Heaven or Hell (CHIMACA) ft Zuse (prod by DJ FU)',
99 'id': '837577',
100 'ext': 'mp3',
101 'uploader': 'Lil Herb a.k.a. G Herbo',
102 }
103 }],
104 'params': {
105 'playliststart': 9,
106 'playlistend': 9,
107 }
108 }
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)
116 result = {'_type': 'playlist', 'entries': []}
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
119 for track_no in itertools.count():
120 # Get song's metadata
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))
125
126 # Total failure, only occurs when url is totally wrong
127 # Won't happen in middle of valid playlist (next case)
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))
130 # URL is good but song id doesn't exist - usually means end of playlist
131 elif not api_response['url']:
132 break
133 else:
134 # Pull out the album metadata and add to result (if it exists)
135 for resultkey, apikey in [('id', 'album_id'), ('title', 'album_title')]:
136 if apikey in api_response and resultkey not in result:
137 result[resultkey] = api_response[apikey]
138 song_id = url_basename(api_response['url']).rpartition('.')[0]
139 result['entries'].append({
140 'id': compat_str(api_response.get('id', song_id)),
141 'uploader': api_response.get('artist'),
142 'title': api_response.get('title', song_id),
143 'url': api_response['url'],
144 })
145 return result