]>
Commit | Line | Data |
---|---|---|
eaee21bf | 1 | import re |
2 | ||
3 | from .common import InfoExtractor | |
4 | from ..utils import ( | |
5 | clean_html, | |
6 | extract_attributes, | |
7 | get_element_by_class, | |
8 | get_element_html_by_class, | |
9 | get_element_text_and_html_by_tag, | |
10 | int_or_none, | |
11 | unified_strdate, | |
12 | strip_or_none, | |
13 | traverse_obj, | |
14 | try_call, | |
15 | ) | |
16 | ||
17 | ||
18 | class MonstercatIE(InfoExtractor): | |
19 | _VALID_URL = r'https://www\.monstercat\.com/release/(?P<id>\d+)' | |
20 | _TESTS = [{ | |
21 | 'url': 'https://www.monstercat.com/release/742779548009', | |
22 | 'playlist_count': 20, | |
23 | 'info_dict': { | |
24 | 'title': 'The Secret Language of Trees', | |
25 | 'id': '742779548009', | |
26 | 'thumbnail': 'https://www.monstercat.com/release/742779548009/cover', | |
27 | 'release_year': 2023, | |
28 | 'release_date': '20230711', | |
29 | 'album': 'The Secret Language of Trees', | |
30 | 'album_artist': 'BT', | |
31 | } | |
32 | }] | |
33 | ||
34 | def _extract_tracks(self, table, album_meta): | |
35 | for td in re.findall(r'<tr[^<]*>((?:(?!</tr>)[\w\W])+)', table): # regex by chatgpt due to lack of get_elements_by_tag | |
36 | title = clean_html(try_call( | |
37 | lambda: get_element_by_class('d-inline-flex flex-column', td).partition(' <span')[0])) | |
38 | ids = extract_attributes(try_call(lambda: get_element_html_by_class('btn-play cursor-pointer mr-small', td)) or '') | |
39 | track_id = ids.get('data-track-id') | |
40 | release_id = ids.get('data-release-id') | |
41 | ||
42 | track_number = int_or_none(try_call(lambda: get_element_by_class('py-xsmall', td))) | |
43 | if not track_id or not release_id: | |
44 | self.report_warning(f'Skipping track {track_number}, ID(s) not found') | |
45 | self.write_debug(f'release_id={repr(release_id)} track_id={repr(track_id)}') | |
46 | continue | |
47 | yield { | |
48 | **album_meta, | |
49 | 'title': title, | |
50 | 'track': title, | |
51 | 'track_number': track_number, | |
52 | 'artist': clean_html(try_call(lambda: get_element_by_class('d-block fs-xxsmall', td))), | |
53 | 'url': f'https://www.monstercat.com/api/release/{release_id}/track-stream/{track_id}', | |
54 | 'id': track_id, | |
55 | 'ext': 'mp3' | |
56 | } | |
57 | ||
58 | def _real_extract(self, url): | |
59 | url_id = self._match_id(url) | |
60 | html = self._download_webpage(url, url_id) | |
61 | # wrap all `get_elements` in `try_call`, HTMLParser has problems with site's html | |
62 | tracklist_table = try_call(lambda: get_element_by_class('table table-small', html)) or '' | |
63 | ||
64 | title = try_call(lambda: get_element_text_and_html_by_tag('h1', html)[0]) | |
65 | date = traverse_obj(html, ({lambda html: get_element_by_class('font-italic mb-medium d-tablet-none d-phone-block', | |
66 | html).partition('Released ')}, 2, {strip_or_none}, {unified_strdate})) | |
67 | ||
68 | album_meta = { | |
69 | 'title': title, | |
70 | 'album': title, | |
71 | 'thumbnail': f'https://www.monstercat.com/release/{url_id}/cover', | |
72 | 'album_artist': try_call( | |
73 | lambda: get_element_by_class('h-normal text-uppercase mb-desktop-medium mb-smallish', html)), | |
74 | 'release_year': int_or_none(date[:4]) if date else None, | |
75 | 'release_date': date, | |
76 | } | |
77 | ||
78 | return self.playlist_result( | |
79 | self._extract_tracks(tracklist_table, album_meta), playlist_id=url_id, **album_meta) |