]>
Commit | Line | Data |
---|---|---|
e5763a7a NJ |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import re | |
5 | ||
6 | from .common import InfoExtractor | |
5c2266df | 7 | from ..compat import compat_urlparse |
e5763a7a | 8 | from ..utils import ( |
b55ee18f | 9 | HEADRequest, |
cec9727c | 10 | KNOWN_EXTENSIONS, |
5c2266df | 11 | sanitized_Request, |
e5763a7a NJ |
12 | str_to_int, |
13 | urlencode_postdata, | |
b55ee18f | 14 | urlhandle_detect_ext, |
e5763a7a NJ |
15 | ) |
16 | ||
17 | ||
18 | class HearThisAtIE(InfoExtractor): | |
19 | _VALID_URL = r'https?://(?:www\.)?hearthis\.at/(?P<artist>[^/]+)/(?P<title>[A-Za-z0-9\-]+)/?$' | |
20 | _PLAYLIST_URL = 'https://hearthis.at/playlist.php' | |
cec9727c | 21 | _TESTS = [{ |
e5763a7a | 22 | 'url': 'https://hearthis.at/moofi/dr-kreep', |
b55ee18f | 23 | 'md5': 'ab6ec33c8fed6556029337c7885eb4e0', |
e5763a7a NJ |
24 | 'info_dict': { |
25 | 'id': '150939', | |
b55ee18f | 26 | 'ext': 'wav', |
e5763a7a | 27 | 'title': 'Moofi - Dr. Kreep', |
ec85ded8 | 28 | 'thumbnail': r're:^https?://.*\.jpg$', |
e5763a7a | 29 | 'timestamp': 1421564134, |
5572d598 | 30 | 'description': 'Listen to Dr. Kreep by Moofi on hearthis.at - Modular, Eurorack, Mutable Intruments Braids, Valhalla-DSP', |
e5763a7a NJ |
31 | 'upload_date': '20150118', |
32 | 'comment_count': int, | |
33 | 'view_count': int, | |
34 | 'like_count': int, | |
35 | 'duration': 71, | |
36 | 'categories': ['Experimental'], | |
37 | } | |
cec9727c YCH |
38 | }, { |
39 | # 'download' link redirects to the original webpage | |
40 | 'url': 'https://hearthis.at/twitchsf/dj-jim-hopkins-totally-bitchin-80s-dance-mix/', | |
41 | 'md5': '5980ceb7c461605d30f1f039df160c6e', | |
42 | 'info_dict': { | |
43 | 'id': '811296', | |
44 | 'ext': 'mp3', | |
45 | 'title': 'TwitchSF - DJ Jim Hopkins - Totally Bitchin\' 80\'s Dance Mix!', | |
46 | 'description': 'Listen to DJ Jim Hopkins - Totally Bitchin\' 80\'s Dance Mix! by TwitchSF on hearthis.at - Dance', | |
47 | 'upload_date': '20160328', | |
48 | 'timestamp': 1459186146, | |
ec85ded8 | 49 | 'thumbnail': r're:^https?://.*\.jpg$', |
cec9727c YCH |
50 | 'comment_count': int, |
51 | 'view_count': int, | |
52 | 'like_count': int, | |
53 | 'duration': 4360, | |
54 | 'categories': ['Dance'], | |
55 | }, | |
56 | }] | |
e5763a7a NJ |
57 | |
58 | def _real_extract(self, url): | |
59 | m = re.match(self._VALID_URL, url) | |
60 | display_id = '{artist:s} - {title:s}'.format(**m.groupdict()) | |
61 | ||
62 | webpage = self._download_webpage(url, display_id) | |
63 | track_id = self._search_regex( | |
64 | r'intTrackId\s*=\s*(\d+)', webpage, 'track ID') | |
65 | ||
66 | payload = urlencode_postdata({'tracks[]': track_id}) | |
5c2266df | 67 | req = sanitized_Request(self._PLAYLIST_URL, payload) |
e5763a7a NJ |
68 | req.add_header('Content-type', 'application/x-www-form-urlencoded') |
69 | ||
70 | track = self._download_json(req, track_id, 'Downloading playlist')[0] | |
71 | title = '{artist:s} - {title:s}'.format(**track) | |
72 | ||
73 | categories = None | |
74 | if track.get('category'): | |
75 | categories = [track['category']] | |
76 | ||
77 | description = self._og_search_description(webpage) | |
78 | thumbnail = self._og_search_thumbnail(webpage) | |
79 | ||
80 | meta_span = r'<span[^>]+class="%s".*?</i>([^<]+)</span>' | |
81 | view_count = str_to_int(self._search_regex( | |
82 | meta_span % 'plays_count', webpage, 'view count', fatal=False)) | |
83 | like_count = str_to_int(self._search_regex( | |
84 | meta_span % 'likes_count', webpage, 'like count', fatal=False)) | |
85 | comment_count = str_to_int(self._search_regex( | |
86 | meta_span % 'comment_count', webpage, 'comment count', fatal=False)) | |
87 | duration = str_to_int(self._search_regex( | |
88 | r'data-length="(\d+)', webpage, 'duration', fatal=False)) | |
89 | timestamp = str_to_int(self._search_regex( | |
90 | r'<span[^>]+class="calctime"[^>]+data-time="(\d+)', webpage, 'timestamp', fatal=False)) | |
91 | ||
b55ee18f PH |
92 | formats = [] |
93 | mp3_url = self._search_regex( | |
94 | r'(?s)<a class="player-link"\s+(?:[a-zA-Z0-9_:-]+="[^"]+"\s+)*?data-mp3="([^"]+)"', | |
7932de63 | 95 | webpage, 'mp3 URL', fatal=False) |
b55ee18f PH |
96 | if mp3_url: |
97 | formats.append({ | |
98 | 'format_id': 'mp3', | |
99 | 'vcodec': 'none', | |
100 | 'acodec': 'mp3', | |
101 | 'url': mp3_url, | |
102 | }) | |
103 | download_path = self._search_regex( | |
104 | r'<a class="[^"]*download_fct[^"]*"\s+href="([^"]+)"', | |
105 | webpage, 'download URL', default=None) | |
106 | if download_path: | |
107 | download_url = compat_urlparse.urljoin(url, download_path) | |
108 | ext_req = HEADRequest(download_url) | |
109 | ext_handle = self._request_webpage( | |
110 | ext_req, display_id, note='Determining extension') | |
111 | ext = urlhandle_detect_ext(ext_handle) | |
cec9727c YCH |
112 | if ext in KNOWN_EXTENSIONS: |
113 | formats.append({ | |
114 | 'format_id': 'download', | |
115 | 'vcodec': 'none', | |
116 | 'ext': ext, | |
117 | 'url': download_url, | |
118 | 'preference': 2, # Usually better quality | |
119 | }) | |
b55ee18f | 120 | self._sort_formats(formats) |
e5763a7a NJ |
121 | |
122 | return { | |
123 | 'id': track_id, | |
b55ee18f | 124 | 'display_id': display_id, |
e5763a7a NJ |
125 | 'title': title, |
126 | 'formats': formats, | |
127 | 'thumbnail': thumbnail, | |
128 | 'description': description, | |
129 | 'duration': duration, | |
130 | 'timestamp': timestamp, | |
131 | 'view_count': view_count, | |
132 | 'comment_count': comment_count, | |
133 | 'like_count': like_count, | |
134 | 'categories': categories, | |
135 | } |