Repository: yt-dlp/yt-dlp

Issue:
Add support for monstercat.com
### DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE

- [X] I understand that I will be **blocked** if I *intentionally* remove or skip any mandatory\* field

### Checklist

- [X] I'm reporting a new site support request
- [X] I've verified that I'm running yt-dlp version **2023.07.06** ([update instructions](https://github.com/yt-dlp/yt-dlp#update)) or later (specify commit)
- [X] I've checked that all provided URLs are playable in a browser with the same IP and same login details
- [X] I've checked that none of provided URLs [violate any copyrights](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#is-the-website-primarily-used-for-piracy) or contain any [DRM](https://en.wikipedia.org/wiki/Digital_rights_management) to the best of my knowledge
- [X] I've searched [known issues](https://github.com/yt-dlp/yt-dlp/issues/3766) and the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues **including closed ones**. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)
- [ ] I've read about [sharing account credentials](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#are-you-willing-to-share-account-details-if-needed) and am willing to share it if required

### Region

India

### Example URLs

https://www.monstercat.com/release/742779549488
https://www.monstercat.com/release/742779549334

### Provide a description that is worded well enough to be understood

Monstercat is one of the most influential music record labels.

### Provide verbose output that clearly demonstrates the problem

- [X] Run **your** yt-dlp command with **-vU** flag added (`yt-dlp -vU <your command line>`)
- [ ] If using API, add `'verbose': True` to `YoutubeDL` params instead
- [X] Copy the WHOLE output (starting with `[debug] Command-line config`) and insert it below

### Complete Verbose Output

```shell
[debug] Command-line config: ['-vU', 'https://www.monstercat.com/release/742779549488']
[debug] User config "/home/trz/.config/yt-dlp/config": ['--embed-metadata']
[debug] Encodings: locale UTF-8, fs utf-8, pref UTF-8, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version stable@2023.07.06 [b532a3481] (zip)
[debug] Python 3.11.5 (CPython x86_64 64bit) - Linux-6.4.12-arch1-1-x86_64-with-glibc2.38 (OpenSSL 3.1.2 1 Aug 2023, glibc 2.38)
[debug] exe versions: ffmpeg 6.0 (setts), ffprobe 6.0, rtmpdump 2.4
[debug] Optional libraries: Cryptodome-3.17, certifi-2022.12.07, sqlite3-2.6.0, websockets-10.4
[debug] Proxy map: {}
[debug] Loaded 1873 extractors
[generic] Extracting URL: https://www.monstercat.com/release/742779549488
[generic] 742779549488: Downloading webpage
WARNING: [generic] Falling back on generic information extractor
[generic] 742779549488: Extracting information
[debug] Looking for embeds
ERROR: Unsupported URL: https://www.monstercat.com/release/742779549488
Traceback (most recent call last):
  File "/usr/local/bin/yt-dlp/yt_dlp/YoutubeDL.py", line 1575, in wrapper
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/bin/yt-dlp/yt_dlp/YoutubeDL.py", line 1710, in __extract_info
    ie_result = ie.extract(url)
                ^^^^^^^^^^^^^^^
  File "/usr/local/bin/yt-dlp/yt_dlp/extractor/common.py", line 715, in extract
    ie_result = self._real_extract(url)
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/bin/yt-dlp/yt_dlp/extractor/generic.py", line 2568, in _real_extract
    raise UnsupportedError(url)
yt_dlp.utils.UnsupportedError: Unsupported URL: https://www.monstercat.com/release/742779549488
```



Discussion:
```js
getUrlForSong(releaseId, songId) {
        return `/api/release/${releaseId}/track-stream/${songId}`;
    }
```
from https://www.monstercat.com/js/utils/player.js

there are various elements with `data-release-id` and `data-track-id` attributes
`data-release-id` -> `${releaseId}`, `data-track-id` -> `${songId}`, that redirects to a storage.googleapis.com url

then its just a matter of getting the metadata from the html

there is also a paid service on https://player.monstercat.app/, but i'll assume thats not part of the request 

https://www.monstercat.com/release/742779548009 - one with multiple tracks

Resolving patch:
diff --git a/yt_dlp/extractor/_extractors.py b/yt_dlp/extractor/_extractors.py
--- a/yt_dlp/extractor/_extractors.py
+++ b/yt_dlp/extractor/_extractors.py
@@ -1119,6 +1119,7 @@
     MofosexEmbedIE,
 )
 from .mojvideo import MojvideoIE
+from .monstercat import MonstercatIE
 from .morningstar import MorningstarIE
 from .motherless import (
     MotherlessIE,
diff --git a/yt_dlp/extractor/monstercat.py b/yt_dlp/extractor/monstercat.py
new file mode 100644
--- /dev/null
+++ b/yt_dlp/extractor/monstercat.py
@@ -0,0 +1,79 @@
+import re
+
+from .common import InfoExtractor
+from ..utils import (
+    clean_html,
+    extract_attributes,
+    get_element_by_class,
+    get_element_html_by_class,
+    get_element_text_and_html_by_tag,
+    int_or_none,
+    unified_strdate,
+    strip_or_none,
+    traverse_obj,
+    try_call,
+)
+
+
+class MonstercatIE(InfoExtractor):
+    _VALID_URL = r'https://www\.monstercat\.com/release/(?P<id>\d+)'
+    _TESTS = [{
+        'url': 'https://www.monstercat.com/release/742779548009',
+        'playlist_count': 20,
+        'info_dict': {
+            'title': 'The Secret Language of Trees',
+            'id': '742779548009',
+            'thumbnail': 'https://www.monstercat.com/release/742779548009/cover',
+            'release_year': 2023,
+            'release_date': '20230711',
+            'album': 'The Secret Language of Trees',
+            'album_artist': 'BT',
+        }
+    }]
+
+    def _extract_tracks(self, table, album_meta):
+        for td in re.findall(r'<tr[^<]*>((?:(?!</tr>)[\w\W])+)', table):  # regex by chatgpt due to lack of get_elements_by_tag
+            title = clean_html(try_call(
+                lambda: get_element_by_class('d-inline-flex flex-column', td).partition(' <span')[0]))
+            ids = extract_attributes(try_call(lambda: get_element_html_by_class('btn-play cursor-pointer mr-small', td)) or '')
+            track_id = ids.get('data-track-id')
+            release_id = ids.get('data-release-id')
+
+            track_number = int_or_none(try_call(lambda: get_element_by_class('py-xsmall', td)))
+            if not track_id or not release_id:
+                self.report_warning(f'Skipping track {track_number}, ID(s) not found')
+                self.write_debug(f'release_id={repr(release_id)} track_id={repr(track_id)}')
+                continue
+            yield {
+                **album_meta,
+                'title': title,
+                'track': title,
+                'track_number': track_number,
+                'artist': clean_html(try_call(lambda: get_element_by_class('d-block fs-xxsmall', td))),
+                'url': f'https://www.monstercat.com/api/release/{release_id}/track-stream/{track_id}',
+                'id': track_id,
+                'ext': 'mp3'
+            }
+
+    def _real_extract(self, url):
+        url_id = self._match_id(url)
+        html = self._download_webpage(url, url_id)
+        # wrap all `get_elements` in `try_call`, HTMLParser has problems with site's html
+        tracklist_table = try_call(lambda: get_element_by_class('table table-small', html)) or ''
+
+        title = try_call(lambda: get_element_text_and_html_by_tag('h1', html)[0])
+        date = traverse_obj(html, ({lambda html: get_element_by_class('font-italic mb-medium d-tablet-none d-phone-block',
+                            html).partition('Released ')}, 2, {strip_or_none}, {unified_strdate}))
+
+        album_meta = {
+            'title': title,
+            'album': title,
+            'thumbnail': f'https://www.monstercat.com/release/{url_id}/cover',
+            'album_artist': try_call(
+                lambda: get_element_by_class('h-normal text-uppercase mb-desktop-medium mb-smallish', html)),
+            'release_year': int_or_none(date[:4]) if date else None,
+            'release_date': date,
+        }
+
+        return self.playlist_result(
+            self._extract_tracks(tracklist_table, album_meta), playlist_id=url_id, **album_meta)


Test patch: