Some more fixes

This commit is contained in:
2026-01-31 11:44:21 +00:00
parent 879a318e54
commit c82c7b74ac

View File

@@ -1,7 +1,4 @@
from ast import keyword
import sys
import mutagen import mutagen
import eyed3
from eyed3.id3.frames import ImageFrame from eyed3.id3.frames import ImageFrame
from mutagen.flac import FLAC from mutagen.flac import FLAC
from mutagen.mp3 import MP3 from mutagen.mp3 import MP3
@@ -13,6 +10,8 @@ from mutagen.easyid3 import EasyID3
import os import os
from os import listdir from os import listdir
from os.path import isfile, join from os.path import isfile, join
import psutil
import datetime as dt
from icrawler.builtin import GoogleImageCrawler from icrawler.builtin import GoogleImageCrawler
@@ -101,13 +100,15 @@ def search_google_images_and_save(x: str, audio):
os.replace(join(".",x),join(songpath,x)) os.replace(join(".",x),join(songpath,x))
google_keyword = "" google_keyword = ""
if x.endswith(".flac"): if x.endswith(".flac"):
google_keyword = str(audio["artist"]) + " " + str(audio["album"]) + " album" google_keyword = str(audio.get("artist", "Unknown Artist")) + " " + str(audio.get("album", "Unknown Album")) + " album"
else: else:
google_keyword = str(audio["TPE2"]) + " " + str(audio["TALB"]) + " album" artist_val = str(audio.get("TPE2", audio.get("TPE1", "Unknown Artist")))
album_val = str(audio.get("TALB", "Unknown Album"))
google_keyword = artist_val + " " + album_val + " album"
logging.info("Moved file! Now searching for album art... keyword is " + google_keyword) logging.info("Moved file! Now searching for album art... keyword is " + google_keyword)
google_Crawler = GoogleImageCrawler(storage = {'root_dir': songpath}) google_Crawler = GoogleImageCrawler(storage = {'root_dir': songpath})
try: try:
google_Crawler.crawl(keyword = str(audio["TPE2"]) + " " + str(audio["TALB"]) + " album", max_num = 1) google_Crawler.crawl(keyword = google_keyword, max_num = 1)
found_image = True found_image = True
except: except:
logging.info("could not find Google result by album, searching by track and artist") logging.info("could not find Google result by album, searching by track and artist")
@@ -522,16 +523,32 @@ def check_spotify_and_save(spotify, audio,x: str) -> bool:
else: else:
track = str(audio["title"]) track = str(audio["title"])
else: else:
# Prefer 'artist' and 'title' tags if available, fallback to TPE2/TIT2
if "artist" in audio:
if audio["artist"] is not str:
artist = str(audio["artist"][0])
else:
artist = str(audio["artist"])
elif "TPE2" in audio:
if audio["TPE2"] is not str: if audio["TPE2"] is not str:
logging.info("TPE2 tag is not a str but a " + str(type(audio["TPE2"])) + " of value " + str(audio["TPE2"]) + ". Setting artist to " + str(audio["TPE2"][0]))
artist = str(audio["TPE2"][0]) artist = str(audio["TPE2"][0])
else: else:
artist = str(audio["TPE2"]) artist = str(audio["TPE2"])
else:
artist = "Unknown Artist"
if "title" in audio:
if audio["title"] is not str:
track = str(audio["title"][0])
else:
track = str(audio["title"])
elif "TIT2" in audio:
if audio["TIT2"] is not str: if audio["TIT2"] is not str:
track = str(audio["TIT2"][0]) track = str(audio["TIT2"][0])
else: else:
track = str(audio["TIT2"]) track = str(audio["TIT2"])
else:
track = "Unknown Title"
querystring = "artist:{0} track:{1}".format(artist.split("\00")[0],track) querystring = "artist:{0} track:{1}".format(artist.split("\00")[0],track)
logging.info("query string: " + querystring) logging.info("query string: " + querystring)
@@ -562,7 +579,22 @@ def check_spotify_and_save(spotify, audio,x: str) -> bool:
else: else:
audio["TALB"] = TALB(encoding=3,text=str(audio["TIT2"])) audio["TALB"] = TALB(encoding=3,text=str(audio["TIT2"]))
comment ="Spotify ID: {0}. This album was released on: {1}, total tracks in album: {2}. This album has {3} version(s)".format(album["id"],album["release_date"], album["total_tracks"],len(results["tracks"]["items"]))
# Add current date/time and CPU/RAM usage
now = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cpu_percent = psutil.cpu_percent(interval=1)
ram_percent = psutil.virtual_memory().percent
sysinfo = f"This album was downloaded on {now}. The server was using {cpu_percent}% CPU and {ram_percent}% RAM."
# Try to get album description (Spotify API does not provide a direct description, but label is available)
album_label = album.get("label", "")
album_desc = ""
if album_label:
album_desc = f"Label: {album_label}. "
# Some albums may have a 'description' field, but it's rare. If present, add it.
if "description" in album:
album_desc += f"Description: {album['description']} "
comment = "Spotify ID: {0}. This album was released on: {1}, total tracks in album: {2}. This album has {3} version(s). {4} {5}".format(album["id"],album["release_date"], album["total_tracks"],len(results["tracks"]["items"]), album_desc, sysinfo)
logging.info("Comment: " + comment) logging.info("Comment: " + comment)
if x.endswith(".flac"): if x.endswith(".flac"):
audio["comment"] = comment audio["comment"] = comment
@@ -782,9 +814,12 @@ def main():
logging.info("Nothing found on spotify, searching Google Images...") logging.info("Nothing found on spotify, searching Google Images...")
search_google_images_and_save(x, audio) search_google_images_and_save(x, audio)
else: else:
# Only set album to title if TALB is not already set (i.e., not found from Spotify)
if x.endswith(".flac"): if x.endswith(".flac"):
if not audio.get("album"):
audio["album"] = audio["title"][0] audio["album"] = audio["title"][0]
else: else:
if not audio.get("TALB") or (isinstance(audio["TALB"], TALB) and not audio["TALB"].text):
audio["TALB"] = TALB(encoding=3,text=str(audio["TIT2"])) audio["TALB"] = TALB(encoding=3,text=str(audio["TIT2"]))
search_google_images_and_save(x, audio) search_google_images_and_save(x, audio)