Initial version of audiomack.py
[youtube-dl] / youtube_dl / extractor / audiomack.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 import datetime
6 import time
7 import urllib.request
8 import json
9
10
11 class AudiomackIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?audiomack\.com/song/(?P<id>[\w/-]+)'
13     _TEST = {
14         'url': 'https://www.audiomack.com/song/crewneckkramer/story-i-tell',
15         'info_dict': {
16             'id': 'story-i-tell',
17             'ext': 'mp3',
18             'title': 'story-i-tell'
19         }
20     }
21
22     def _real_extract(self, url):
23         # TODO more code goes here, for example ...
24         #webpage = self._download_webpage(url, video_id)
25         #title = self._html_search_regex(r'<h1>(.*?)</h1>', webpage, 'title')
26         
27         assert("/song/" in url)
28         songurl = url[url.index("/song/")+5:]
29         title = songurl[songurl.rindex("/")+1:]
30         video_id = title
31         t = int(time.mktime(datetime.datetime.now().timetuple()))
32         s = "http://www.audiomack.com/api/music/url/song"+songurl+"?_="+str(t)
33         f = urllib.request.urlopen(s)
34         j = f.read(1000).decode("utf-8")
35         data = json.loads(j)
36
37         return {
38             'id': video_id,
39             'title': title,
40             'url' : data["url"],
41             'ext' : 'mp3'
42             # TODO more properties (see youtube_dl/extractor/common.py)
43         }