[options] PEP8 and simpler --merge-output-format handling (#4673)
[ytdl] / 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
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             'add_ie': ['Soundcloud'],
28             'url': 'http://www.audiomack.com/song/xclusiveszone/take-kare',
29             'info_dict': {
30                 'id': '172419696',
31                 'ext': 'mp3',
32                 'description': 'md5:1fc3272ed7a635cce5be1568c2822997',
33                 'title': 'Young Thug ft Lil Wayne - Take Kare',
34                 'uploader': 'Young Thug World',
35                 'upload_date': '20141016',
36             }
37         },
38     ]
39
40     def _real_extract(self, url):
41         video_id = self._match_id(url)
42
43         api_response = self._download_json(
44             "http://www.audiomack.com/api/music/url/song/%s?_=%d" % (
45                 video_id, time.time()),
46             video_id)
47
48         if "url" not in api_response:
49             raise ExtractorError("Unable to deduce api url of song")
50         realurl = api_response["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
57         webpage = self._download_webpage(url, video_id)
58         artist = self._html_search_regex(
59             r'<span class="artist">(.*?)</span>', webpage, "artist")
60         songtitle = self._html_search_regex(
61             r'<h1 class="profile-title song-title"><span class="artist">.*?</span>(.*?)</h1>',
62             webpage, "title")
63         title = artist + " - " + songtitle
64
65         return {
66             'id': video_id,
67             'title': title,
68             'url': realurl,
69         }