Add metadata viewer for albums, artists and music, add metadata caching, make music buttons smaller

This commit is contained in:
csd4ni3l
2025-06-29 21:10:13 +02:00
parent cd531324bc
commit 748dcb17c4
5 changed files with 283 additions and 84 deletions

1
.gitignore vendored
View File

@@ -181,3 +181,4 @@ logs/
logs
settings.json
bin/
metadata_cache.json

View File

@@ -2,7 +2,6 @@ import arcade, arcade.gui, os, json
from utils.constants import button_style, audio_extensions
from utils.preload import button_texture, button_hovered_texture
from utils.utils import UIFocusTextureButton
from menus.file_manager import FileManager
from arcade.gui.experimental.focus import UIFocusGroup
@@ -42,10 +41,10 @@ class AddMusic(arcade.gui.UIView):
self.add_music_input = self.box.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text=f'Select File ({self.music_file_selected})', style=button_style, font_name="Roboto", font_size=32, width=self.window.width / 2, height=self.window.height / 10))
self.add_music_input.on_click = lambda event: self.select_file()
self.add_music_button = self.box.add(UIFocusTextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='Add Music', style=button_style, width=self.window.width / 2, height=self.window.height / 10))
self.add_music_button = self.box.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='Add Music', style=button_style, width=self.window.width / 2, height=self.window.height / 10))
self.add_music_button.on_click = lambda event: self.add_music()
self.back_button = self.anchor.add(UIFocusTextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50), anchor_x="left", anchor_y="top", align_x=5, align_y=-5)
self.back_button = self.anchor.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50), anchor_x="left", anchor_y="top", align_x=5, align_y=-5)
self.back_button.on_click = lambda event: self.main_exit()
self.anchor.detect_focusable_widgets()

View File

@@ -263,27 +263,9 @@ class Main(arcade.gui.UIView):
self.shuffle = not self.shuffle
self.update_buttons()
def metadata_button_action(self, action, metadata):
if action != "Close":
webbrowser.open(metadata["uploader_url"] if action == "Uploader" else metadata["source_url"])
def view_metadata(self, file_path):
metadata = self.file_metadata[file_path]
metadata_text = f'''File path: {file_path}
File size: {metadata['file_size']}MiB
Artist: {metadata['artist']}
Title: {metadata['title']}
Upload Year: {metadata['upload_year'] or 'Unknown'}
Amount of times played: {metadata['play_count']}
Last Played: {convert_timestamp_to_time_ago(int(metadata['last_played']))}
Sound length: {convert_seconds_to_date(int(metadata['sound_length']))}
Bitrate: {metadata['bitrate']}Kbps
Sample rate: {metadata['sample_rate']}KHz'''
msgbox = arcade.gui.UIMessageBox(title=f"{metadata['artist']} - {metadata['title']} Metadata", buttons=("Uploader", "Source", "Close"), message_text=metadata_text, width=self.window.width / 2, height=self.window.height / 2)
msgbox.on_action = lambda event, metadata=metadata: self.metadata_button_action(event.action, metadata)
self.anchor.add(msgbox, anchor_x="center", anchor_y="center")
from menus.metadata_viewer import MetadataViewer
self.window.show_view(MetadataViewer(self.pypresence_client, "music", self.file_metadata[file_path], file_path, self.current_mode, self.current_music_name, self.current_length, self.current_music_player, self.queue, self.loaded_sounds, self.shuffle))
def show_content(self, tab):
for music_button in self.music_buttons.values():
@@ -309,7 +291,7 @@ Sample rate: {metadata['sample_rate']}KHz'''
for music_filename in content_to_show:
metadata = self.file_metadata[f"{tab}/{music_filename}"]
self.music_buttons[f"{tab}/{music_filename}"] = self.music_box.add(MusicItem(metadata=metadata, width=self.window.width / 1.2, height=self.window.height / 11))
self.music_buttons[f"{tab}/{music_filename}"] = self.music_box.add(MusicItem(metadata=metadata, width=self.window.width / 1.2, height=self.window.height / 22))
self.music_buttons[f"{tab}/{music_filename}"].button.on_click = lambda event, music_path=f"{tab}/{music_filename}": self.music_button_click(event, music_path)
self.music_buttons[f"{tab}/{music_filename}"].view_metadata_button.on_click = lambda event, music_path=f"{tab}/{music_filename}": self.view_metadata(music_path)
@@ -329,11 +311,11 @@ Sample rate: {metadata['sample_rate']}KHz'''
for music_path in content_to_show:
metadata = self.file_metadata[music_path]
self.music_buttons[music_path] = self.music_box.add(MusicItem(metadata=metadata, width=self.window.width / 1.2, height=self.window.height / 11))
self.music_buttons[music_path] = self.music_box.add(MusicItem(metadata=metadata, width=self.window.width / 1.2, height=self.window.height / 22))
self.music_buttons[music_path].button.on_click = lambda event, music_path=music_path: self.music_button_click(event, music_path)
self.music_buttons[music_path].view_metadata_button.on_click = lambda event, music_path=music_path: self.view_metadata(music_path)
self.music_buttons["add_music"] = self.music_box.add(MusicItem(metadata=None, width=self.window.width / 1.2, height=self.window.height / 11))
self.music_buttons["add_music"] = self.music_box.add(MusicItem(metadata=None, width=self.window.width / 1.2, height=self.window.height / 22))
self.music_buttons["add_music"].button.on_click = lambda event: self.add_music()
self.anchor.detect_focusable_widgets()

145
menus/metadata_viewer.py Normal file
View File

@@ -0,0 +1,145 @@
import arcade, arcade.gui, webbrowser
from arcade.gui.experimental.focus import UIFocusGroup
from arcade.gui.experimental.scroll_area import UIScrollArea, UIScrollBar
from utils.online_metadata import get_music_metadata, get_album_cover_art
from utils.constants import button_style
from utils.preload import button_texture, button_hovered_texture
from utils.utils import convert_seconds_to_date
from utils.music_handling import convert_timestamp_to_time_ago
class MetadataViewer(arcade.gui.UIView):
def __init__(self, pypresence_client, metadata_type="music", metadata_dict=None, file_path=None, *args):
super().__init__()
self.metadata_type = metadata_type
if metadata_type == "music":
self.file_metadata = metadata_dict
self.artist = self.file_metadata["artist"]
self.file_path = file_path
if self.artist == "Unknown":
self.artist = None
self.title = self.file_metadata["title"]
self.online_metadata = get_music_metadata(self.artist, self.title)
elif metadata_type == "artist":
self.artist_metadata = metadata_dict
elif metadata_type == "album":
self.album_metadata = metadata_dict
self.pypresence_client = pypresence_client
self.args = args
self.more_metadata_buttons = []
self.metadata_labels = []
def on_show_view(self):
super().on_show_view()
self.anchor = self.add_widget(UIFocusGroup(size_hint=(1, 1)))
self.back_button = self.anchor.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50), anchor_x="left", anchor_y="top", align_x=5, align_y=-5)
self.back_button.on_click = lambda event: self.main_exit()
self.scroll_area = UIScrollArea(size_hint=(0.6, 0.8)) # center on screen
self.scroll_area.scroll_speed = -50
self.anchor.add(self.scroll_area, anchor_x="center", anchor_y="center")
self.scrollbar = UIScrollBar(self.scroll_area)
self.scrollbar.size_hint = (0.02, 1)
self.anchor.add(self.scrollbar, anchor_x="right", anchor_y="center")
self.box = arcade.gui.UIBoxLayout(space_between=10, align='top')
self.scroll_area.add(self.box)
self.more_metadata_box = self.anchor.add(arcade.gui.UIBoxLayout(space_between=10, vertical=False), anchor_x="left", anchor_y="bottom", align_x=10, align_y=10)
if self.metadata_type == "music":
tags = ', '.join(self.online_metadata[0]['tags'])
albums = ', '.join(list(self.online_metadata[2].keys()))
name = f"{self.file_metadata['artist']} - {self.file_metadata['title']} Metadata"
metadata_text = f'''File path: {self.file_path}
File Artist: {self.file_metadata['artist']}
MusicBrainz Artists: {', '.join([artist for artist in self.online_metadata[1]])}
Title: {self.file_metadata['title']}
MusicBrainz ID: {self.online_metadata[0]['musicbrainz_id']}
ISRC(s): {', '.join(self.online_metadata[0]['isrc-list']) if self.online_metadata[0]['isrc-list'] else "None"}
MusicBrainz Rating: {self.online_metadata[0]['musicbrainz_rating']}
Tags: {tags if tags else 'None'}
Albums: {albums if albums else 'None'}
File size: {self.file_metadata['file_size']}MiB
Upload Year: {self.file_metadata['upload_year'] or 'Unknown'}
Amount of times played: {self.file_metadata['play_count']}
Last Played: {convert_timestamp_to_time_ago(int(self.file_metadata['last_played']))}
Sound length: {convert_seconds_to_date(int(self.file_metadata['sound_length']))}
Bitrate: {self.file_metadata['bitrate']}Kbps
Sample rate: {self.file_metadata['sample_rate']}KHz
'''
self.more_metadata_buttons.append(self.more_metadata_box.add(arcade.gui.UITextureButton(text=f"Artist Metadata", style=button_style, texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 4.25, height=self.window.height / 15)))
self.more_metadata_buttons[-1].on_click = lambda event: self.window.show_view(MetadataViewer(self.pypresence_client, "artist", self.online_metadata[1], None, *self.args))
self.more_metadata_buttons.append(self.more_metadata_box.add(arcade.gui.UITextureButton(text=f"Album Metadata", style=button_style, texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 4.25, height=self.window.height / 15)))
self.more_metadata_buttons[-1].on_click = lambda event: self.window.show_view(MetadataViewer(self.pypresence_client, "album", self.online_metadata[2], None, *self.args))
self.more_metadata_buttons.append(self.more_metadata_box.add(arcade.gui.UITextureButton(text=f"Open Uploader URL", style=button_style, texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 4.25, height=self.window.height / 15)))
self.more_metadata_buttons[-1].on_click = lambda event: webbrowser.open(self.file_metadata["uploader_url"]) if not self.file_metadata.get("uploader_url", "Unknown") == "Unknown" else None
self.more_metadata_buttons.append(self.more_metadata_box.add(arcade.gui.UITextureButton(text=f"Open Source URL", style=button_style, texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 4.25, height=self.window.height / 15)))
self.more_metadata_buttons[-1].on_click = lambda event: webbrowser.open(self.file_metadata["source_url"]) if not self.file_metadata.get("source_url", "Unknown") == "Unknown" else None
metadata_box = self.box.add(arcade.gui.UIBoxLayout(space_between=10, align='left'))
self.metadata_labels.append(metadata_box.add(arcade.gui.UILabel(text=name, font_size=20, font_name="Roboto", multiline=True)))
self.metadata_labels.append(metadata_box.add(arcade.gui.UILabel(text=metadata_text, font_size=18, font_name="Roboto", multiline=True)))
elif self.metadata_type == "artist":
for artist_name, artist_dict in self.artist_metadata.items():
ipi_list = ', '.join(artist_dict['ipi-list'])
isni_list = ', '.join(artist_dict['isni-list'])
tag_list = ','.join(artist_dict['tag-list'])
name = f"{artist_name} Metadata"
metadata_text = f'''Artist MusicBrainz ID: {artist_dict['musicbrainz_id']}
Artist Gender: {artist_dict['gender']}
Artist Tag(s): {tag_list if tag_list else 'None'}
Artist IPI(s): {ipi_list if ipi_list else 'None'}
Artist ISNI(s): {isni_list if isni_list else 'None'}
Artist Born: {artist_dict['born']}
Artist Dead: {'Yes' if artist_dict['dead'] else 'No'}
Artist Comment: {artist_dict['comment']}
'''
metadata_box = self.box.add(arcade.gui.UIBoxLayout(space_between=10, align='left'))
self.metadata_labels.append(metadata_box.add(arcade.gui.UILabel(text=name, font_size=20, font_name="Roboto", multiline=True)))
self.metadata_labels.append(metadata_box.add(arcade.gui.UILabel(text=metadata_text, font_size=18, font_name="Roboto", multiline=True)))
elif self.metadata_type == "album":
if not self.album_metadata:
self.metadata_labels.append(self.anchor.add(arcade.gui.UILabel(text="We couldn't find any albums for this music.", font_size=32, font_name="Roboto"), anchor_x="center", anchor_y="center"))
return
self.cover_art_box = self.box.add(arcade.gui.UIBoxLayout(space_between=100, align="left"))
for album_name, album_dict in self.album_metadata.items():
name = f"{album_name} Metadata"
metadata_text = f'''
MusicBrainz Album ID: {album_dict['musicbrainz_id']}
Album Name: {album_dict['album_name']}
Album Date: {album_dict['album_date']}
Album Country: {album_dict['album_country']}
'''
full_box = self.box.add(arcade.gui.UIBoxLayout(space_between=30, align='center', vertical=False))
metadata_box = full_box.add(arcade.gui.UIBoxLayout(space_between=10, align='center'))
self.metadata_labels.append(metadata_box.add(arcade.gui.UILabel(text=name, font_size=20, font_name="Roboto", multiline=True)))
self.metadata_labels.append(metadata_box.add(arcade.gui.UILabel(text=metadata_text, font_size=18, font_name="Roboto", multiline=True)))
cover_art = get_album_cover_art(album_dict["musicbrainz_id"])
if cover_art:
full_box.add(arcade.gui.UIImage(texture=cover_art, width=self.window.width / 10, height=self.window.height / 6))
else:
full_box.add(arcade.gui.UILabel(text="No cover found.", font_size=18, font_name="Roboto"))
def main_exit(self):
from menus.main import Main
self.window.show_view(Main(self.pypresence_client, *self.args))

View File

@@ -1,8 +1,10 @@
import musicbrainzngs as music_api
from iso3166 import countries
import urllib.request, json
from utils.constants import MUSICBRAINZ_PROJECT_NAME, MUSICBRAINZ_CONTACT, MUSCIBRAINZ_VERSION
import urllib.request, json, os, arcade
WORD_BLACKLIST = ["compilation", "remix", "vs", "cover"]
LRCLIB_BASE_URL = "https://lrclib.net/api/search"
@@ -36,11 +38,91 @@ def get_country(country_code):
return country.name if country else None
def get_artists_metadata(artist_ids):
with open("metadata_cache.json", "r") as file:
metadata_cache = json.load(file)
artist_metadata = {}
for artist_id in artist_ids:
if artist_id in metadata_cache["artist_by_id"]:
data = metadata_cache["artist_by_id"][artist_id]
name = data["name"]
artist_metadata[name] = data
else:
artist_data = music_api.get_artist_by_id(artist_id)["artist"]
artist_metadata[artist_data["name"]] = {
"name": artist_data["name"],
"musicbrainz_id": artist_id,
"gender": artist_data.get("gender", "Unknown"),
"country": get_country(artist_data.get("country", "WZ")) or "Unknown",
"tag-list": [tag["name"] for tag in artist_data.get("tag_list", [])],
"ipi-list": artist_data.get("ipi-list", []),
"isni-list": artist_data.get("isni-list", []),
"born": artist_data.get("life-span", {}).get("begin", "Unknown"),
"dead": artist_data.get("life-span", {}).get("ended", "Unknown").lower() == "true",
"comment": artist_data.get("disambiguation", "None")
}
metadata_cache["artist_by_id"][artist_id] = artist_metadata[artist_data["name"]]
with open("metadata_cache.json", "w") as file:
file.write(json.dumps(metadata_cache))
return artist_metadata
def get_albums_metadata(release_list):
with open("metadata_cache.json", "r") as file:
metadata_cache = json.load(file)
album_metadata = {}
for release in release_list:
release_title = release.get("title", "").lower()
if any(word in release_title for word in ["single", "ep", "maxi"]):
continue
if release.get("status") == "Official":
release_id = release["id"]
if (release_id in metadata_cache["is_release_album_by_id"] and metadata_cache["is_release_album_by_id"][release_id]) or is_release_valid(release_id): # Only do it if the album is official, skipping many API calls
metadata_cache["is_release_album_by_id"][release_id] = True
album_metadata[release.get("title", "")] = {
"musicbrainz_id": release.get("id") if release else "Unknown",
"album_name": release.get("title") if release else "Unknown",
"album_date": release.get("date") if release else "Unknown",
"album_country": (get_country(release.get("country", "WZ")) or "Worldwide") if release else "Unknown",
}
else:
metadata_cache["is_release_album_by_id"][release_id] = False
with open("metadata_cache.json", "w") as file:
file.write(json.dumps(metadata_cache))
return album_metadata
def get_music_metadata(artist, title):
if os.path.exists("metadata_cache.json") and os.path.isfile("metadata_cache.json"):
with open("metadata_cache.json", "r") as file:
metadata_cache = json.load(file)
else:
metadata_cache = {
"query_results": {},
"recording_by_id": {},
"artist_by_id": {},
"is_release_album_by_id": {}
}
music_api.set_useragent(MUSICBRAINZ_PROJECT_NAME, MUSCIBRAINZ_VERSION, MUSICBRAINZ_CONTACT)
if artist:
results = music_api.search_recordings(query=f"{artist} - {title}", limit=100)["recording-list"]
query = f"{artist} - {title}"
else:
query = title
if query in metadata_cache["query_results"]:
recording_id = metadata_cache["query_results"][query]
else:
results = music_api.search_recordings(query=title, limit=100)["recording-list"]
@@ -54,60 +136,41 @@ def get_music_metadata(artist, title):
continue
recording_id = r["id"]
break
try:
metadata_cache["query_results"][query] = recording_id
if recording_id in metadata_cache["recording_by_id"]:
detailed = metadata_cache["recording_by_id"][recording_id]
else:
detailed = music_api.get_recording_by_id(
recording_id,
includes=["artists", "releases", "isrcs", "tags", "ratings"]
)["recording"]
except music_api.ResponseError:
continue
metadata_cache["recording_by_id"][recording_id] = {
"artist-credit": [{"artist": {"id": artist_data["artist"]["id"]}} for artist_data in detailed.get("artist-credit", {}) if isinstance(artist_data, dict)],
"isrc-list": detailed["isrc-list"] if "isrc-list" in detailed else [],
"rating": {"rating": detailed["rating"]["rating"]} if "rating" in detailed else {},
"tags": detailed.get("tag-list", []),
"release-list": [{"id": release["id"], "title": release["title"], "status": release.get("status"), "date": release.get("date"), "country": release.get("country", "WZ")} for release in detailed["release-list"]] if "release-list" in detailed else []
}
release = None
for rel in detailed.get("release-list", []):
release_title = rel.get("title", "").lower()
with open("metadata_cache.json", "w") as file:
file.write(json.dumps(metadata_cache))
if any(word in release_title for word in ["single", "ep", "maxi"]):
continue
artist_ids = [artist_data["artist"]["id"] for artist_data in detailed.get("artist-credit", {}) if isinstance(artist_data, dict)] # isinstance is needed, because sometimes & is included as an artist str
artist_metadata = get_artists_metadata(artist_ids)
album_metadata = get_albums_metadata(detailed.get("release-list", []))
if rel.get("status") == "Official" and is_release_valid(rel["id"]): # Only do it if the album is official, skipping many API calls
release = rel
metadata = {
music_metadata = {
"musicbrainz_id": recording_id,
"isrc": detailed["isrc-list"][0] if "isrc-list" in detailed else "Unknown",
"musicbrainz_album_id": release.get("id") if release else "Unknown",
"album_name": release.get("title") if release else "Unknown",
"album_date": release.get("date") if release else "Unknown",
"album_country": (get_country(release.get("country")) or "Worldwide") if release else "Unknown",
"recording_length": int(detailed["length"]) if "length" in detailed else "Unknown",
"musicbrainz_rating": detailed["rating"]["rating"] if "rating" in detailed else "Unknown",
"isrc-list": detailed["isrc-list"] if "isrc-list" in detailed else [],
"musicbrainz_rating": detailed["rating"]["rating"] if "rating" in detailed.get("rating", {}) else "Unknown",
"tags": [tag["name"] for tag in detailed.get("tag-list", [])]
}
return metadata
return music_metadata, artist_metadata, album_metadata
return None
def get_artist_metadata(artist):
music_api.set_useragent(MUSICBRAINZ_PROJECT_NAME, MUSCIBRAINZ_VERSION, MUSICBRAINZ_CONTACT)
result = music_api.search_artists(query=artist, limit=10)
for r in result["artist-list"]:
if not r["type"] == "Person":
continue
return {
"musicbrainz_id": r["id"],
"gender": r.get("gender", "Unknown"),
"country": get_country(r.get("country")) or "Unknown",
"ipi-list": r.get("ipi-list", "None"),
"isni-list": r.get("isni-list", "None"),
"born": r.get("life-span", {}).get("begin", "Unknown"),
"dead": r.get("life-span", {}).get("ended").lower() == "true",
"comment": r["disambiguation"]
}
def get_lyrics(artist, title):
if artist:
@@ -128,7 +191,16 @@ def get_lyrics(artist, title):
return "Unknown"
def get_album_cover_art(musicbrainz_album_id):
try:
cover_art_bytes = music_api.get_image_front(musicbrainz_album_id)
except music_api.ResponseError:
return None
with open("music_cover_art.jpg", "wb") as file:
file.write(cover_art_bytes)
texture = arcade.load_texture("music_cover_art.jpg")
os.remove("music_cover_art.jpg")
return texture