monstercat.py 3.2 KB

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