Spaces:
Sleeping
Sleeping
Upload musicbrainz_client.py
Browse files- src/musicbrainz_client.py +64 -0
src/musicbrainz_client.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import musicbrainzngs
|
| 2 |
+
import string
|
| 3 |
+
from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted, red_highlighted
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def get_music_infos(search_string: str) -> tuple[str, str, str, str]:
|
| 7 |
+
print(f"{ULTRASINGER_HEAD} Searching song in {blue_highlighted('musicbrainz')}")
|
| 8 |
+
# https://python-musicbrainzngs.readthedocs.io/en/v0.7.1/usage/#searching
|
| 9 |
+
|
| 10 |
+
musicbrainzngs.set_useragent("UltraSinger", "0.1", "https://github.com/rakuri255/UltraSinger")
|
| 11 |
+
|
| 12 |
+
# search for artist and titel to get release on the first place
|
| 13 |
+
artist = None
|
| 14 |
+
artists = musicbrainzngs.search_artists(search_string)
|
| 15 |
+
if len(artists['artist-list']) != 0:
|
| 16 |
+
artist = artists['artist-list'][0]['name'].strip()
|
| 17 |
+
else:
|
| 18 |
+
print(f"{ULTRASINGER_HEAD} {red_highlighted('No match found')}")
|
| 19 |
+
return None, None, None, None
|
| 20 |
+
|
| 21 |
+
release = None
|
| 22 |
+
release_groups = musicbrainzngs.search_release_groups(search_string, artist=artist)
|
| 23 |
+
if len(release_groups['release-group-list']) != 0:
|
| 24 |
+
release = release_groups['release-group-list'][0]
|
| 25 |
+
|
| 26 |
+
if release is not None and 'artist-credit-phrase' in release:
|
| 27 |
+
artist = release['artist-credit-phrase'].strip()
|
| 28 |
+
|
| 29 |
+
title = None
|
| 30 |
+
if release is not None and 'title' in release:
|
| 31 |
+
clean_search_string = search_string.translate(str.maketrans('', '', string.punctuation)).lower().strip()
|
| 32 |
+
clean_release_title = release['title'].translate(str.maketrans('', '', string.punctuation)).lower().strip()
|
| 33 |
+
clean_artist = artist.translate(str.maketrans('', '', string.punctuation)).lower().strip()
|
| 34 |
+
|
| 35 |
+
# prepare search string when title and artist are the same
|
| 36 |
+
if clean_release_title == clean_artist:
|
| 37 |
+
# remove the first appearance of the artist
|
| 38 |
+
clean_search_string = clean_search_string.replace(clean_artist, "", 1)
|
| 39 |
+
|
| 40 |
+
if clean_release_title in clean_search_string:
|
| 41 |
+
title = release['title'].strip()
|
| 42 |
+
else:
|
| 43 |
+
print(
|
| 44 |
+
f"{ULTRASINGER_HEAD} cant find title {red_highlighted(clean_release_title)} in {red_highlighted(clean_search_string)}")
|
| 45 |
+
|
| 46 |
+
if title is None or artist is None:
|
| 47 |
+
print(f"{ULTRASINGER_HEAD} {red_highlighted('No match found')}")
|
| 48 |
+
return None, None, None, None
|
| 49 |
+
|
| 50 |
+
print(f"{ULTRASINGER_HEAD} Found Titel and Artist {blue_highlighted(title)} by {blue_highlighted(artist)}")
|
| 51 |
+
|
| 52 |
+
year = None
|
| 53 |
+
if 'first-release-date' in release:
|
| 54 |
+
year = release['first-release-date'].strip()
|
| 55 |
+
print(f"{ULTRASINGER_HEAD} Found release year: {blue_highlighted(year)}")
|
| 56 |
+
|
| 57 |
+
genres = None
|
| 58 |
+
if 'tag-list' in release:
|
| 59 |
+
genres = ""
|
| 60 |
+
for tag in release['tag-list']:
|
| 61 |
+
genres += f"{tag['name'].strip()},"
|
| 62 |
+
print(f"{ULTRASINGER_HEAD} Found genres: {blue_highlighted(genres)}")
|
| 63 |
+
|
| 64 |
+
return title, artist, year, genres
|