logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

monstercat.py (3266B)


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