[audiomack] Modernize test definition
[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
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             {
31                 'id': '172419696',
32                 'ext': 'mp3',
33                 'description': 'md5:1fc3272ed7a635cce5be1568c2822997',
34                 'title': 'Young Thug ft Lil Wayne - Take Kare',
35                 'uploader':'Young Thug World',
36                 'upload_date':'20141016',
37             }
38         },
39     ]
40
41     def _real_extract(self, url):
42         video_id = self._match_id(url)
43
44         api_response = self._download_json(
45             "http://www.audiomack.com/api/music/url/song/%s?_=%d" % (
46                 video_id, time.time()),
47             video_id)
48
49         if "url" not in api_response:
50             raise ExtractorError("Unable to deduce api url of song")
51         realurl = api_response["url"]
52
53         # Audiomack wraps a lot of soundcloud tracks in their branded wrapper
54         # - if so, pass the work off to the soundcloud extractor
55         if SoundcloudIE.suitable(realurl):
56             return {'_type': 'url', 'url': realurl, 'ie_key': 'Soundcloud'}
57
58         webpage = self._download_webpage(url, video_id)
59         artist = self._html_search_regex(
60             r'<span class="artist">(.*?)</span>', webpage, "artist")
61         songtitle = self._html_search_regex(
62             r'<h1 class="profile-title song-title"><span class="artist">.*?</span>(.*?)</h1>',
63             webpage, "title")
64         title = artist + " - " + songtitle
65
66         return {
67             'id': video_id,
68             'title': title,
69             'url': realurl,
70         }