Implemented the song class for musicbrainz search

This commit is contained in:
N0\A
2025-10-31 23:36:18 +01:00
parent 31e4bfb38c
commit cf2d4cf7d8
8 changed files with 145 additions and 11 deletions

1
create_venv.bat Normal file
View File

@@ -0,0 +1 @@
python3 -m venv venv

View File

@@ -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()

View File

@@ -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)
return None, None, None

View File

@@ -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

View File

@@ -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
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)

View File

@@ -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

View File

@@ -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

116
results.json Normal file
View File

@@ -0,0 +1,116 @@
{
"recording-list": [
{
"id": "0567332d-f973-4a74-9c0c-cf8c8cbf8baa",
"ext:score": "100",
"title": "You Wouldnt 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 Wouldnt 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 Wouldnt 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 Wouldnt 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
}