Not directly calling soundcloud extractor anymore
[youtube-dl] / youtube_dl / extractor / audiomack.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from .soundcloud import SoundcloudIE
6 from ..utils import ExtractorError
7 import datetime
8 import time
9
10
11 class AudiomackIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?audiomack\.com/song/(?P<id>[\w/-]+)'
13     IE_NAME = 'audiomack'
14     _TESTS = [
15         #hosted on audiomack
16         {
17             'url': 'http://www.audiomack.com/song/roosh-williams/extraordinary',
18             'info_dict':
19             {
20                 'id' : 'roosh-williams/extraordinary',
21                 'ext': 'mp3',
22                 'title': 'Roosh Williams - Extraordinary'
23             }
24         },
25         #hosted on soundcloud via audiomack
26         {
27             'url': 'http://www.audiomack.com/song/xclusiveszone/take-kare',
28             'file': '172419696.mp3',
29             'info_dict':
30             {
31                 'ext': 'mp3',
32                 'title': 'Young Thug ft Lil Wayne - Take Kare',
33                 "upload_date": "20141016",
34                 "description": "New track produced by London On Da Track called “Take Kare\"\n\nhttp://instagram.com/theyoungthugworld\nhttps://www.facebook.com/ThuggerThuggerCashMoney\n",
35                 "uploader": "Young Thug World"
36             }
37         }
38     ]
39
40     def _real_extract(self, url):
41         #id is what follows /song/ in url, usually the uploader name + title
42         id = self._match_id(url)
43
44         #Call the api, which gives us a json doc with the real url inside
45         rightnow = int(time.time())
46         apiresponse = self._download_json("http://www.audiomack.com/api/music/url/song/"+id+"?_="+str(rightnow), id)
47
48         if "url" not in apiresponse:
49             raise ExtractorError("Unable to deduce api url of song")
50         realurl = apiresponse["url"]
51
52         #Audiomack wraps a lot of soundcloud tracks in their branded wrapper
53         # - if so, pass the work off to the soundcloud extractor
54         if SoundcloudIE.suitable(realurl):
55             return {'_type': 'url', 'url': realurl, 'ie_key': 'Soundcloud'}
56         else:
57             #Pull out metadata
58             page = self._download_webpage(url, id)
59             artist = self._html_search_regex(r'<span class="artist">(.*)</span>', page, "artist")
60             songtitle = self._html_search_regex(r'<h1 class="profile-title song-title"><span class="artist">.*</span>(.*)</h1>', page, "title")
61             title = artist+" - "+songtitle
62             return {
63                 'id': id,  # ignore id, which is not useful in song name
64                 'title': title,
65                 'url': realurl,
66                 'ext': 'mp3'
67             }