From e8552e642950698939cae0d383ee378d60d11d33 Mon Sep 17 00:00:00 2001 From: SemvdHoeven Date: Mon, 13 Apr 2026 17:12:31 +0200 Subject: [PATCH] Set year tag properly --- make_folders.py | 104 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 34 deletions(-) diff --git a/make_folders.py b/make_folders.py index 3a136ad..1983ffb 100755 --- a/make_folders.py +++ b/make_folders.py @@ -5,7 +5,7 @@ from mutagen.mp3 import MP3 from mutagen.id3 import ID3 from mutagen.wave import WAVE from mutagen.oggvorbis import OggVorbis -from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, COMM, TCOM, TCON, TDRC, TRCK, TPUB, POPM, APIC +from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, COMM, TCOM, TCON, TDRC, TDRL, TRCK, TPUB, POPM, APIC from mutagen.easyid3 import EasyID3 import os from os import listdir @@ -102,23 +102,27 @@ def search_google_images_and_save(x: str, audio): else: 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: - result = google_Crawler.crawl(keyword = google_keyword, max_num = 1) - found_image = True - except Exception as e: - logging.info(f"could not find Google result by album, searching by track and artist: {e}") - # Fallback: if no image found, try searching by song and artist - if not found_image or not any(f.split('.')[-1].lower() in ["jpg","png"] for f in listdir(songpath)): - song_keyword = artist_val + " " + str(audio.get("TIT2", "")) - logging.info("Fallback: searching for song art... keyword is " + song_keyword) + # Skip Google search if artist or album is unknown + if "Unknown Artist" in artist_val or "Unknown Album" in album_val: + logging.info("Artist or album is unknown, skipping Google image search") + else: + 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 = song_keyword, max_num = 1) + result = google_Crawler.crawl(keyword = google_keyword, max_num = 1) found_image = True except Exception as e: - logging.info(f"could not find Google result by song: {e}") + logging.info(f"could not find Google result by album, searching by track and artist: {e}") + # Fallback: if no image found, try searching by song and artist + if not found_image or not any(f.split('.')[-1].lower() in ["jpg","png"] for f in listdir(songpath)): + song_keyword = artist_val + " " + str(audio.get("TIT2", "")) + logging.info("Fallback: searching for song art... keyword is " + song_keyword) + try: + google_Crawler.crawl(keyword = song_keyword, max_num = 1) + found_image = True + except Exception as e: + logging.info(f"could not find Google result by song: {e}") # Rename cover art file if found for f in listdir(songpath): if (isfile(join(songpath,f)) and f.split(".")[-1].lower() in ["jpg","png"]): @@ -129,14 +133,20 @@ def search_google_images_and_save(x: str, audio): songpath = join(".",str(audio.get("TPE2", "Unknown Artist")),str(audio.get("TIT2", "Unknown Title"))) make_folder(songpath) os.replace(join(".",x),join(songpath,x)) - song_keyword = str(audio.get("TPE2", "Unknown Artist")) + " " + str(audio.get("TIT2", "Unknown Title")) - logging.info("Moved file! Now searching for album art... keyword is " + song_keyword) - google_Crawler = GoogleImageCrawler(storage = {'root_dir': songpath}) - try: - google_Crawler.crawl(keyword = song_keyword, max_num = 1) - found_image = True - except Exception as e: - logging.info(f"could not find Google result by track and artist: {e}") + artist_keyword = str(audio.get("TPE2", "Unknown Artist")) + title_keyword = str(audio.get("TIT2", "Unknown Title")) + # Skip Google search if artist or title is unknown + if "Unknown Artist" in artist_keyword or "Unknown Title" in title_keyword: + logging.info("Artist or title is unknown, skipping Google image search") + else: + song_keyword = artist_keyword + " " + title_keyword + logging.info("Moved file! Now searching for album art... keyword is " + song_keyword) + google_Crawler = GoogleImageCrawler(storage = {'root_dir': songpath}) + try: + google_Crawler.crawl(keyword = song_keyword, max_num = 1) + found_image = True + except Exception as e: + logging.info(f"could not find Google result by track and artist: {e}") for f in listdir(songpath): if (isfile(join(songpath,f)) and f.split(".")[-1].lower() in ["jpg","png"]): os.replace(join(songpath,f),join(songpath,"Cover." + f.split(".")[-1].lower())) @@ -176,6 +186,11 @@ def create_ID3_tag(audio, tagname: str, textvalue: str): audio[tagname] = TDRC(encoding=3,text=textvalue) except: pass + elif tagname == "TDRL": + try: + audio[tagname] = TDRL(encoding=3,text=textvalue) + except: + pass elif tagname == "TPUB": audio[tagname] = TPUB(encoding=3,text=textvalue) @@ -440,10 +455,24 @@ def check_spotify_album_and_save(spotify, audio,x: str) -> bool: elif (str(audio["TALB"]) != album_name): audio["TALB"] = TALB(encoding=3,text=album_name) + # Parse release date to extract year + try: + year = str(datetime.strptime(album["release_date"], '%Y-%m-%d').year) + except: + try: + year = str(datetime.strptime(album["release_date"], '%Y-%m').year) + except: + try: + year = str(datetime.strptime(album["release_date"], '%Y').year) + except: + year = str(album["release_date"]) + if (x.endswith(".flac")): - audio["year"] = album["release_date"] + audio["year"] = year + audio["date"] = album["release_date"] else: - audio["TDRC"] = TDRC(encoding=3,text=album["release_date"]) + audio["TDRC"] = TDRC(encoding=3,text=year) + audio["TDRL"] = TDRL(encoding=3,text=album["release_date"]) artist_search = spotify.artist(album['artists'][0]['external_urls']['spotify']) logging.info("genres: " + str(artist_search['genres'])) @@ -593,18 +622,25 @@ def check_spotify_and_save(spotify, audio,x: str) -> bool: else: audio["COMM"] = COMM(encoding=3,text=comment) + # Parse release date to extract year try: year = str(datetime.strptime(album["release_date"], '%Y-%m-%d').year) - - if x.endswith(".flac"): - audio["year"] = year - else: - audio["TDRC"] = TDRC(encoding=3,text=year) - - except Exception as err: - logging.info(err) - year = str(album["release_date"]) + except: + try: + year = str(datetime.strptime(album["release_date"], '%Y-%m').year) + except: + try: + year = str(datetime.strptime(album["release_date"], '%Y').year) + except Exception as err: + logging.info(err) + year = str(album["release_date"]) + + if x.endswith(".flac"): audio["year"] = year + audio["date"] = album["release_date"] + else: + audio["TDRC"] = TDRC(encoding=3,text=year) + audio["TDRL"] = TDRL(encoding=3,text=album["release_date"]) if x.endswith(".flac"):