From cf2d4cf7d8b72aecb6af21c1743f0c6a52c97365 Mon Sep 17 00:00:00 2001 From: "N0\\A" Date: Fri, 31 Oct 2025 23:36:18 +0100 Subject: [PATCH] Implemented the song class for musicbrainz search --- create_venv.bat | 1 + musicdl/__main__.py | 3 +- musicdl/download.py | 7 ++- musicdl/metadata.py | 3 +- musicdl/musicbrainz.py | 16 +++++- musicdl/song.py | 4 +- musicdl/youtube.py | 6 +-- results.json | 116 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 create_venv.bat create mode 100644 results.json diff --git a/create_venv.bat b/create_venv.bat new file mode 100644 index 0000000..af2fd1e --- /dev/null +++ b/create_venv.bat @@ -0,0 +1 @@ +python3 -m venv venv \ No newline at end of file diff --git a/musicdl/__main__.py b/musicdl/__main__.py index fcea6bd..24c0fa6 100644 --- a/musicdl/__main__.py +++ b/musicdl/__main__.py @@ -5,7 +5,8 @@ from musicdl.metadata import search def main(): url = argv[1] title, artist, album = download(url) - results = search(title, artist, album) + result = search(title, artist, album) + print(result.title, result.artist, result.album, result.album_artist, result.track_number) if __name__ == '__main__': main() \ No newline at end of file diff --git a/musicdl/download.py b/musicdl/download.py index 861217e..bf9d8ec 100644 --- a/musicdl/download.py +++ b/musicdl/download.py @@ -1,4 +1,7 @@ -def download(url: str): +def download(url: str) -> tuple[str | None, str | None, str | None]: if "youtu" in url: from musicdl.youtube import download - return download(url) \ No newline at end of file + return download(url) + + + return None, None, None \ No newline at end of file diff --git a/musicdl/metadata.py b/musicdl/metadata.py index 8959bdb..da35cbe 100644 --- a/musicdl/metadata.py +++ b/musicdl/metadata.py @@ -1,6 +1,7 @@ import musicdl.musicbrainz as musicbrainz +import musicdl.song as Song -def search(title: str, artist: str = None, album: str = None): +def search(title: str | None, artist: str | None = None, album: str | None = None) -> Song: results = musicbrainz.search(title, artist, album) return results \ No newline at end of file diff --git a/musicdl/musicbrainz.py b/musicdl/musicbrainz.py index dd1d0bd..fdbeb23 100644 --- a/musicdl/musicbrainz.py +++ b/musicdl/musicbrainz.py @@ -1,4 +1,5 @@ import musicbrainzngs +from musicdl.song import Song musicbrainzngs.set_useragent( "MusicDL", @@ -6,7 +7,7 @@ musicbrainzngs.set_useragent( "https://git.krzak.org/N0VA/musicdl" ) -def search(title: str, artist: str = None, album: str = None): +def search(title: str | None, artist: str | None = None, album: str | None = None) -> Song: result = musicbrainzngs.search_recordings( query=title, artist=artist, @@ -14,4 +15,15 @@ def search(title: str, artist: str = None, album: str = None): limit=1 ) - return result \ No newline at end of file + title: str = result['recording-list'][0]['title'] + artist: str = result['recording-list'][0]['artist-credit'][0]['artist']['name'] + album: str = result['recording-list'][0]['release-list'][0]['title'] + album_artist: str = result['recording-list'][0]['release-list'][0]['artist-credit'][0]['artist']['name'] + track_number = 0 + + for track in result['recording-list'][0]['release-list'][0]["medium-list"][0]["track-list"]: + if track["title"] == title: + track_number = track['number'] + break + + return Song(title, artist, album, album_artist, track_number) \ No newline at end of file diff --git a/musicdl/song.py b/musicdl/song.py index df0e3e8..c3f05f3 100644 --- a/musicdl/song.py +++ b/musicdl/song.py @@ -5,8 +5,8 @@ class Song(): artist: str, album: str, album_artist: str, - track_number: str, - cover: str + track_number: int, + cover: str | None = None ) -> None: self.title = title diff --git a/musicdl/youtube.py b/musicdl/youtube.py index 556c7f2..c83d8af 100644 --- a/musicdl/youtube.py +++ b/musicdl/youtube.py @@ -1,6 +1,6 @@ import yt_dlp -def download(url: str): +def download(url: str) -> tuple[str | None, str | None, str | None]: ydl_opts = { 'format': 'bestaudio/best', 'postprocessors': [{ @@ -13,8 +13,8 @@ def download(url: str): ydl = yt_dlp.YoutubeDL(ydl_opts) #type: ignore # get data info = ydl.extract_info(url, download=False) - title = info['title'] - artist = info['creator'] + title = info['title'] if 'title' in info else None + artist = info['creator'] if 'creator' in info else None album = None # download diff --git a/results.json b/results.json new file mode 100644 index 0000000..5c5c6aa --- /dev/null +++ b/results.json @@ -0,0 +1,116 @@ +{ + "recording-list": [ + { + "id": "0567332d-f973-4a74-9c0c-cf8c8cbf8baa", + "ext:score": "100", + "title": "You Wouldn’t Know", + "length": "197000", + "artist-credit": [ + { + "name": "Jonathan Coulton", + "artist": { + "id": "d8df7087-06d5-4545-9024-831bb8558ad1", + "name": "Jonathan Coulton", + "sort-name": "Coulton, Jonathan", + "alias-list": [ + { + "sort-name": "J. Coulton", + "type": "Search hint", + "alias": "J. Coulton" + }, + { + "sort-name": "Coulton, Jonathan William", + "type": "Legal name", + "alias": "Jonathan William Coulton" + }, + { + "locale": "en", + "sort-name": "Coulton, Jonathan", + "type": "Artist name", + "primary": "primary", + "alias": "Jonathan Coulton" + }, + { "sort-name": "JoCo", "type": "Artist name", "alias": "JoCo" } + ] + } + }, + " feat. ", + { + "name": "Ellen McLain", + "artist": { + "id": "f318585d-b1a6-4127-bac3-c0d4d2019b67", + "name": "Ellen McLain", + "sort-name": "McLain, Ellen" + } + } + ], + "release-list": [ + { + "id": "d381e5c3-5166-4000-8819-bc5b405f8bf1", + "title": "You Wouldn’t Know", + "status": "Official", + "artist-credit": [ + { + "name": "Jonathan Coulton", + "artist": { + "id": "d8df7087-06d5-4545-9024-831bb8558ad1", + "name": "Jonathan Coulton", + "sort-name": "Coulton, Jonathan" + } + }, + " feat. ", + { + "name": "Ellen McLain", + "artist": { + "id": "f318585d-b1a6-4127-bac3-c0d4d2019b67", + "name": "Ellen McLain", + "sort-name": "McLain, Ellen" + } + } + ], + "release-group": { + "id": "fd297dd1-86cc-4c45-aee6-4ee38ddacf84", + "type": "Single", + "title": "You Wouldn’t Know", + "primary-type": "Single", + "secondary-type-list": ["Soundtrack"] + }, + "date": "2015-09-28", + "country": "XW", + "release-event-list": [ + { + "date": "2015-09-28", + "area": { + "id": "525d4e18-3d00-31b9-a58b-a146a916de8f", + "name": "[Worldwide]", + "sort-name": "[Worldwide]", + "iso-3166-1-code-list": ["XW"] + } + } + ], + "medium-list": [ + { + "position": "1", + "format": "Digital Media", + "track-list": [ + { + "id": "4127d181-61a8-4f7c-8383-9e50cd5eadfd", + "number": "1", + "title": "You Wouldn’t Know", + "length": "197000", + "track_or_recording_length": "197000" + } + ], + "track-count": 1 + } + ], + "medium-track-count": 1, + "medium-count": 1, + "artist-credit-phrase": "Jonathan Coulton feat. Ellen McLain" + } + ], + "artist-credit-phrase": "Jonathan Coulton feat. Ellen McLain" + } + ], + "recording-count": 2922944 +}