[hearthisat] Add new extractor (Closes #4743)
[youtube-dl] / youtube_dl / extractor / hearthisat.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_urllib_request
8 from ..utils import (
9     str_to_int,
10     urlencode_postdata,
11 )
12
13
14 class HearThisAtIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?hearthis\.at/(?P<artist>[^/]+)/(?P<title>[A-Za-z0-9\-]+)/?$'
16     _PLAYLIST_URL = 'https://hearthis.at/playlist.php'
17     _TEST = {
18         'url': 'https://hearthis.at/moofi/dr-kreep',
19         'md5': 'd594c573227a89f4256f0b03e68c80cc',
20         'info_dict': {
21             'id': '150939',
22             'ext': 'mp3',
23             'title': 'Moofi - Dr. Kreep',
24             'thumbnail': 're:^https?://.*\.jpg$',
25             'timestamp': 1421564134,
26             'description': 'Creepy Patch. Mutable Instruments Braids Vowel + Formant Mode.',
27             'upload_date': '20150118',
28             'comment_count': int,
29             'view_count': int,
30             'like_count': int,
31             'duration': 71,
32             'categories': ['Experimental'],
33         }
34     }
35
36     def _real_extract(self, url):
37         m = re.match(self._VALID_URL, url)
38         display_id = '{artist:s} - {title:s}'.format(**m.groupdict())
39
40         webpage = self._download_webpage(url, display_id)
41         track_id = self._search_regex(
42             r'intTrackId\s*=\s*(\d+)', webpage, 'track ID')
43
44         payload = urlencode_postdata({'tracks[]': track_id})
45         req = compat_urllib_request.Request(self._PLAYLIST_URL, payload)
46         req.add_header('Content-type', 'application/x-www-form-urlencoded')
47
48         track = self._download_json(req, track_id, 'Downloading playlist')[0]
49         title = '{artist:s} - {title:s}'.format(**track)
50
51         categories = None
52         if track.get('category'):
53             categories = [track['category']]
54
55         description = self._og_search_description(webpage)
56         thumbnail = self._og_search_thumbnail(webpage)
57
58         meta_span = r'<span[^>]+class="%s".*?</i>([^<]+)</span>'
59         view_count = str_to_int(self._search_regex(
60             meta_span % 'plays_count', webpage, 'view count', fatal=False))
61         like_count = str_to_int(self._search_regex(
62             meta_span % 'likes_count', webpage, 'like count', fatal=False))
63         comment_count = str_to_int(self._search_regex(
64             meta_span % 'comment_count', webpage, 'comment count', fatal=False))
65         duration = str_to_int(self._search_regex(
66             r'data-length="(\d+)', webpage, 'duration', fatal=False))
67         timestamp = str_to_int(self._search_regex(
68             r'<span[^>]+class="calctime"[^>]+data-time="(\d+)', webpage, 'timestamp', fatal=False))
69
70         track_url = self._search_regex(
71             r'<a[^>]+data-mp3="([^"]+)"', webpage, 'track URL')
72
73         formats = [{
74             'format_id': 'mp3',
75             'url': track_url,
76             'vcodec': 'none',
77         }]
78
79         return {
80             'id': track_id,
81             'display-id': display_id,
82             'title': title,
83             'formats': formats,
84             'thumbnail': thumbnail,
85             'description': description,
86             'duration': duration,
87             'timestamp': timestamp,
88             'view_count': view_count,
89             'comment_count': comment_count,
90             'like_count': like_count,
91             'categories': categories,
92         }