mirror of
https://github.com/rakuri255/UltraSinger.git
synced 2026-12-01 09:37:14 +09:00
- Fix when musicbrainz return same title and artist
This commit is contained in:
1 parent
b755f0bbbc
commit
25518ebe1b
6 files changed
+125
-26
No files matched your search
@@ -85,11 +85,11 @@ class TestCreateUltrastarTxt(unittest.TestCase):
|
||||
f"#{UltrastarTxtTag.ARTIST}:{default_ultrastar_class.artist}\n",
|
||||
f"#{UltrastarTxtTag.TITLE}:{default_ultrastar_class.title}\n",
|
||||
f"#{UltrastarTxtTag.MP3}:{default_ultrastar_class.mp3}\n",
|
||||
f"#{UltrastarTxtTag.VIDEO}:{default_ultrastar_class.video}\n",
|
||||
f"#{UltrastarTxtTag.VIDEO}:{default_ultrastar_class.video}\n", # todo: video is optional
|
||||
f"#{UltrastarTxtTag.BPM}:390.0\n",
|
||||
f"#{UltrastarTxtTag.GAP}:500\n",
|
||||
f"#{UltrastarTxtTag.CREATOR}:{default_ultrastar_class.creator}\n",
|
||||
f"#{UltrastarTxtTag.FIXER}:{default_ultrastar_class.fixer}\n",
|
||||
f"#{UltrastarTxtTag.FIXER}:{default_ultrastar_class.fixer}\n", # todo: fixer is special
|
||||
f"#{UltrastarTxtTag.COMMENT}:{default_ultrastar_class.comment}\n",
|
||||
": 0 52 1 UltraSinger \n",
|
||||
"- 52\n",
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
"""Test the musicbrainz_client module."""
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from src.modules.musicbrainz_client import get_music_infos
|
||||
|
||||
|
||||
class TestGetMusicInfos(unittest.TestCase):
|
||||
|
||||
@patch('musicbrainzngs.search_artists')
|
||||
@patch('musicbrainzngs.search_release_groups')
|
||||
def test_get_music_infos(self, mock_search_release_groups, mock_search_artists):
|
||||
# Arrange
|
||||
artist = 'UltraSinger'
|
||||
title = 'That\'s Rocking!'
|
||||
search = f'{artist} - {title} (UltrStar 2023) FULL HD'
|
||||
|
||||
# Set up mock return values for the MusicBrainz API calls
|
||||
mock_search_artists.return_value = {
|
||||
'artist-list': [
|
||||
{'name': artist}
|
||||
]
|
||||
}
|
||||
|
||||
mock_search_release_groups.return_value = {
|
||||
'release-group-list': [
|
||||
{
|
||||
'title': title,
|
||||
'artist-credit-phrase': artist,
|
||||
'first-release-date': '2023-01-01',
|
||||
'tag-list': [
|
||||
{'name': 'Genre 1'},
|
||||
{'name': 'Genre 2'}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
# Call the function to test
|
||||
title, artist, year, genre = get_music_infos(search)
|
||||
|
||||
# Assert the returned values
|
||||
self.assertEqual(title, 'That\'s Rocking!')
|
||||
self.assertEqual(artist, 'UltraSinger')
|
||||
self.assertEqual(year, '2023-01-01')
|
||||
self.assertEqual(genre, 'Genre 1,Genre 2,')
|
||||
|
||||
@patch('musicbrainzngs.search_artists')
|
||||
@patch('musicbrainzngs.search_release_groups')
|
||||
def test_get_music_infos_when_title_and_artist_are_the_same(self, mock_search_release_groups, mock_search_artists):
|
||||
# Arrange
|
||||
artist = "ArtistIsTitle"
|
||||
title = "ArtistIsTitle"
|
||||
search_not_same = "ArtistIsTitle - ArtistNotTitle"
|
||||
search_is_same = f"{artist} - {title}"
|
||||
|
||||
# Set up mock return values for the MusicBrainz API calls
|
||||
mock_search_artists.return_value = {
|
||||
'artist-list': [
|
||||
{'name': artist}
|
||||
]
|
||||
}
|
||||
|
||||
mock_search_release_groups.return_value = {
|
||||
'release-group-list': [
|
||||
{
|
||||
'title': title,
|
||||
'artist-credit-phrase': artist,
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
# Act search_not_same but musicbrainz returns the same artist and title
|
||||
title, artist, year, genre = get_music_infos(search_not_same)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(title, None)
|
||||
self.assertEqual(artist, None)
|
||||
self.assertEqual(year, None)
|
||||
self.assertEqual(genre, None)
|
||||
|
||||
# Act search_is_same and musicbrainz returns the same artist and title
|
||||
title, artist, year, genre = get_music_infos(search_is_same)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(title, 'ArtistIsTitle')
|
||||
self.assertEqual(artist, 'ArtistIsTitle')
|
||||
self.assertEqual(year, None)
|
||||
self.assertEqual(genre, None)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
+5
-9
@@ -514,7 +514,7 @@ def create_ultrastar_txt_from_automation(
|
||||
title: str,
|
||||
artist: str,
|
||||
year: str,
|
||||
genre: []
|
||||
genre: str
|
||||
):
|
||||
"""Create Ultrastar txt from automation"""
|
||||
ultrastar_header = UltrastarTxtValue()
|
||||
@@ -537,12 +537,8 @@ def create_ultrastar_txt_from_automation(
|
||||
ultrastar_header.artist = artist
|
||||
if year is not None:
|
||||
ultrastar_header.year = year
|
||||
if genre:
|
||||
genre_str = ""
|
||||
for g in genre:
|
||||
genre_str += g + ", "
|
||||
genre_str = genre_str[:-1]
|
||||
ultrastar_header.genre = genre_str
|
||||
if genre is not None:
|
||||
ultrastar_header.genre = genre
|
||||
|
||||
real_bpm = get_bpm_from_file(ultrastar_audio_input_path)
|
||||
ultrastar_file_output = os.path.join(
|
||||
@@ -574,7 +570,7 @@ def create_ultrastar_txt_from_automation(
|
||||
return real_bpm, ultrastar_file_output
|
||||
|
||||
|
||||
def infos_from_audio_input_file() -> tuple[str, str, str, tuple[str, str, str, []]]:
|
||||
def infos_from_audio_input_file() -> tuple[str, str, str, tuple[str, str, str, str]]:
|
||||
"""Infos from audio input file"""
|
||||
basename = os.path.basename(settings.input_file_path)
|
||||
basename_without_ext = os.path.splitext(basename)[0]
|
||||
@@ -620,7 +616,7 @@ def sanitize_filename(fname: str) -> str:
|
||||
return fname
|
||||
|
||||
|
||||
def download_from_youtube() -> tuple[str, str, str, tuple[str, str, str, []]]:
|
||||
def download_from_youtube() -> tuple[str, str, str, tuple[str, str, str, str]]:
|
||||
"""Download from YouTube"""
|
||||
(artist, title) = get_youtube_title(settings.input_file_path)
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ def get_youtube_title(url: str) -> tuple[str, str]:
|
||||
|
||||
if "artist" in result:
|
||||
return result["artist"], result["track"]
|
||||
if " - " in result["title"]:
|
||||
return result["title"].split(" - ")[0], result["title"].split(" - ")[1]
|
||||
if "-" in result["title"]:
|
||||
return result["title"].split("-")[0], result["title"].split("-")[1]
|
||||
return result["channel"], result["title"]
|
||||
|
||||
|
||||
|
||||
@@ -4,14 +4,14 @@ from src.modules.Ultrastar.ultrastar_txt import UltrastarTxtValue
|
||||
|
||||
def real_bpm_to_ultrastar_bpm(real_bpm: float) -> float:
|
||||
"""Converts real BPM to UltraStar BPM"""
|
||||
# The UltraStar BPM info is a fifth beat of the real BPM
|
||||
# The UltraStar BPM info is a fourth beat of the real BPM
|
||||
ultrastar_bpm = real_bpm / 4
|
||||
return ultrastar_bpm
|
||||
|
||||
|
||||
def ultrastar_bpm_to_real_bpm(ultrastar_bpm: float) -> float:
|
||||
"""Converts UltraStar BPM to real BPM"""
|
||||
# The UltraStar BPM info is a fifth beat of the real BPM
|
||||
# The UltraStar BPM info is a fourth beat of the real BPM
|
||||
bpm = ultrastar_bpm * 4
|
||||
return bpm
|
||||
|
||||
|
||||
@@ -3,44 +3,54 @@ import string
|
||||
from src.modules.console_colors import ULTRASINGER_HEAD, blue_highlighted, red_highlighted
|
||||
|
||||
|
||||
def get_music_infos(search_string: str) -> tuple[str, str, str, []]:
|
||||
def get_music_infos(search_string: str) -> tuple[str, str, str, str]:
|
||||
print(f"{ULTRASINGER_HEAD} Searching song in {blue_highlighted('musicbrainz')}")
|
||||
# https://python-musicbrainzngs.readthedocs.io/en/v0.7.1/usage/#searching
|
||||
|
||||
musicbrainzngs.set_useragent("UltraSinger", "0.1", "https://github.com/rakuri255/UltraSinger")
|
||||
|
||||
# search for artist and titel to get release on the first place
|
||||
artists = musicbrainzngs.search_artists(search_string)
|
||||
artist = artists['artist-list'][0]['name']
|
||||
release_groups = musicbrainzngs.search_release_groups(search_string, artist=artist)
|
||||
release = release_groups['release-group-list'][0]
|
||||
|
||||
if 'artist-credit-phrase' in release:
|
||||
artist = release['artist-credit-phrase']
|
||||
|
||||
title = None
|
||||
if 'title' in release:
|
||||
clean_search_string = search_string.translate(str.maketrans('', '', string.punctuation)).lower()
|
||||
clean_release_title = release['title'].translate(str.maketrans('', '', string.punctuation)).lower()
|
||||
clean_artist = artist.translate(str.maketrans('', '', string.punctuation)).lower()
|
||||
|
||||
# prepare search string when title and artist are the same
|
||||
if clean_release_title == clean_artist:
|
||||
# remmove the first acurance of the artist
|
||||
clean_search_string = clean_search_string.replace(clean_artist, "", 1)
|
||||
|
||||
if clean_release_title in clean_search_string:
|
||||
title = release['title']
|
||||
else:
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} cant find title {red_highlighted(release['title'])} in {red_highlighted(search_string)}")
|
||||
|
||||
if 'artist-credit-phrase' in release:
|
||||
artist = release['artist-credit-phrase']
|
||||
f"{ULTRASINGER_HEAD} cant find title {red_highlighted(clean_release_title)} in {red_highlighted(clean_search_string)}")
|
||||
|
||||
if title is None or artist is None:
|
||||
print(f"{ULTRASINGER_HEAD} {red_highlighted('No match found')}")
|
||||
return None, None, None, []
|
||||
return None, None, None, None
|
||||
|
||||
print(f"{ULTRASINGER_HEAD} Found and using {blue_highlighted(title)} by {blue_highlighted(artist)}")
|
||||
print(f"{ULTRASINGER_HEAD} Found Titel and Artist {blue_highlighted(title)} by {blue_highlighted(artist)}")
|
||||
|
||||
year = None
|
||||
if 'first-release-date' in release:
|
||||
year = release['first-release-date']
|
||||
print(f"{ULTRASINGER_HEAD} Release year: {blue_highlighted(year)}")
|
||||
print(f"{ULTRASINGER_HEAD} Found release year: {blue_highlighted(year)}")
|
||||
|
||||
genre = []
|
||||
genres = None
|
||||
if 'tag-list' in release:
|
||||
genres = ""
|
||||
for tag in release['tag-list']:
|
||||
genre.append(tag['name'])
|
||||
genres += f"{tag['name']},"
|
||||
print(f"{ULTRASINGER_HEAD} Found genres: {blue_highlighted(genres)}")
|
||||
|
||||
return title, artist, year, genre
|
||||
return title, artist, year, genres
|
||||
Reference in new issue
Block a user