diff --git a/make_folders.py b/make_folders.py index f207e68..2af0416 100755 --- a/make_folders.py +++ b/make_folders.py @@ -1,7 +1,4 @@ -from ast import keyword -import sys import mutagen -import eyed3 from eyed3.id3.frames import ImageFrame from mutagen.flac import FLAC from mutagen.mp3 import MP3 @@ -13,6 +10,8 @@ from mutagen.easyid3 import EasyID3 import os from os import listdir from os.path import isfile, join +import psutil +import datetime as dt 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)) google_keyword = "" 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: - 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) google_Crawler = GoogleImageCrawler(storage = {'root_dir': songpath}) 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 except: 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: track = str(audio["title"]) else: - 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]) + # 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: + artist = str(audio["TPE2"][0]) + else: + artist = str(audio["TPE2"]) else: - artist = str(audio["TPE2"]) - - if audio["TIT2"] is not str: - track = str(audio["TIT2"][0]) + 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: + track = str(audio["TIT2"][0]) + else: + track = str(audio["TIT2"]) else: - track = str(audio["TIT2"]) + track = "Unknown Title" querystring = "artist:{0} track:{1}".format(artist.split("\00")[0],track) logging.info("query string: " + querystring) @@ -562,7 +579,22 @@ def check_spotify_and_save(spotify, audio,x: str) -> bool: else: 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) if x.endswith(".flac"): audio["comment"] = comment @@ -782,10 +814,13 @@ def main(): logging.info("Nothing found on spotify, searching Google Images...") search_google_images_and_save(x, audio) else: + # Only set album to title if TALB is not already set (i.e., not found from Spotify) if x.endswith(".flac"): - audio["album"] = audio["title"][0] + if not audio.get("album"): + audio["album"] = audio["title"][0] else: - audio["TALB"] = TALB(encoding=3,text=str(audio["TIT2"])) + if not audio.get("TALB") or (isinstance(audio["TALB"], TALB) and not audio["TALB"].text): + audio["TALB"] = TALB(encoding=3,text=str(audio["TIT2"])) search_google_images_and_save(x, audio) logging.info("------------------------------------------------")