[ie/dropout] Fix extraction (#12102)
[yt-dlp.git] / yt_dlp / extractor / monstercat.py
blobf17b91f5a2a66fb95d4f69f0ee31121744fdfced
1 import re
3 from .common import InfoExtractor
4 from ..utils import (
5 clean_html,
6 extract_attributes,
7 int_or_none,
8 strip_or_none,
9 unified_strdate,
11 from ..utils.traversal import find_element, traverse_obj
14 class MonstercatIE(InfoExtractor):
15 _VALID_URL = r'https?://www\.monstercat\.com/release/(?P<id>\d+)'
16 _TESTS = [{
17 'url': 'https://www.monstercat.com/release/742779548009',
18 'playlist_count': 20,
19 'info_dict': {
20 'title': 'The Secret Language of Trees',
21 'id': '742779548009',
22 'thumbnail': 'https://www.monstercat.com/release/742779548009/cover',
23 'release_date': '20230711',
24 'album': 'The Secret Language of Trees',
25 'album_artists': ['BT'],
29 def _extract_tracks(self, table, album_meta):
30 for td in re.findall(r'<tr[^<]*>((?:(?!</tr>)[\w\W])+)', table): # regex by chatgpt due to lack of get_elements_by_tag
31 title = traverse_obj(td, (
32 {find_element(cls='d-inline-flex flex-column')},
33 {lambda x: x.partition(' <span')}, 0, {clean_html}))
34 ids = traverse_obj(td, (
35 {find_element(cls='btn-play cursor-pointer mr-small', html=True)}, {extract_attributes})) or {}
36 track_id = ids.get('data-track-id')
37 release_id = ids.get('data-release-id')
39 track_number = traverse_obj(td, ({find_element(cls='py-xsmall')}, {int_or_none}))
40 if not track_id or not release_id:
41 self.report_warning(f'Skipping track {track_number}, ID(s) not found')
42 self.write_debug(f'release_id={release_id!r} track_id={track_id!r}')
43 continue
44 yield {
45 **album_meta,
46 'title': title,
47 'track': title,
48 'track_number': track_number,
49 'artists': traverse_obj(td, ({find_element(cls='d-block fs-xxsmall')}, {clean_html}, all)),
50 'url': f'https://www.monstercat.com/api/release/{release_id}/track-stream/{track_id}',
51 'id': track_id,
52 'ext': 'mp3',
55 def _real_extract(self, url):
56 url_id = self._match_id(url)
57 html = self._download_webpage(url, url_id)
58 # NB: HTMLParser may choke on this html; use {find_element} or try_call(lambda: get_element...)
59 tracklist_table = traverse_obj(html, {find_element(cls='table table-small')}) or ''
60 title = traverse_obj(html, ({find_element(tag='h1')}, {clean_html}))
62 album_meta = {
63 'title': title,
64 'album': title,
65 'thumbnail': f'https://www.monstercat.com/release/{url_id}/cover',
66 'album_artists': traverse_obj(html, (
67 {find_element(cls='h-normal text-uppercase mb-desktop-medium mb-smallish')}, {clean_html}, all)),
68 'release_date': traverse_obj(html, (
69 {find_element(cls='font-italic mb-medium d-tablet-none d-phone-block')},
70 {lambda x: x.partition('Released ')}, 2, {strip_or_none}, {unified_strdate})),
73 return self.playlist_result(
74 self._extract_tracks(tracklist_table, album_meta), playlist_id=url_id, **album_meta)