diff --git a/app/lib/populate.py b/app/lib/populate.py index 3ca20946..e648e19e 100644 --- a/app/lib/populate.py +++ b/app/lib/populate.py @@ -77,12 +77,14 @@ class Populate: for _dir in dirs_to_scan: files = files.union(run_fast_scandir(_dir, full=True)[1]) - unmodified = self.remove_modified(tracks) + unmodified, modified_tracks = self.remove_modified(tracks) untagged = files - unmodified if len(untagged) != 0: self.tag_untagged(untagged, key) + self.extract_thumb_with_overwrite(modified_tracks) + ProcessTrackThumbnails() ProcessAlbumColors() ProcessArtistColors() @@ -116,24 +118,26 @@ class Populate: since they were added to the database. """ - unmodified = set() - modified = set() + unmodified_paths = set() + modified_tracks: list[Track] = [] + modified_paths = set() for track in tracks: try: if track.last_mod == os.path.getmtime(track.filepath): - unmodified.add(track.filepath) + unmodified_paths.add(track.filepath) continue except (FileNotFoundError, OSError) as e: TrackStore.remove_track_obj(track) remove_tracks_by_filepaths(track.filepath) - modified.add(track.filepath) + modified_paths.add(track.filepath) + modified_tracks.append(track) - TrackStore.remove_tracks_by_filepaths(modified) - remove_tracks_by_filepaths(modified) + TrackStore.remove_tracks_by_filepaths(modified_paths) + remove_tracks_by_filepaths(modified_paths) - return unmodified + return unmodified_paths, modified_tracks @staticmethod def tag_untagged(untagged: set[str], key: str): @@ -178,6 +182,19 @@ class Populate: log.info("Added %s/%s tracks", tagged_count, len(untagged)) + @staticmethod + def extract_thumb_with_overwrite(tracks: list[Track]): + """ + Extracts the thumbnail from a list of filepaths, + overwriting the existing thumbnail if it exists, + for modified files. + """ + for track in tracks: + try: + extract_thumb(track.filepath, track.image, overwrite=True) + except FileNotFoundError: + continue + def get_image(album: Album): """ @@ -259,7 +276,7 @@ class FetchSimilarArtistsLastFM: tqdm( pool.imap_unordered(save_similar_artists, artists), total=len(artists), - desc="Downloading similar artists", + desc="Fetching similar artists", ) ) diff --git a/app/lib/taglib.py b/app/lib/taglib.py index b63a6897..57a24d65 100644 --- a/app/lib/taglib.py +++ b/app/lib/taglib.py @@ -24,9 +24,10 @@ def parse_album_art(filepath: str): return None -def extract_thumb(filepath: str, webp_path: str) -> bool: +def extract_thumb(filepath: str, webp_path: str, overwrite=False) -> bool: """ - Extracts the thumbnail from an audio file. Returns the path to the thumbnail. + Extracts the thumbnail from an audio file. + Returns the path to the thumbnail. """ original_img_path = os.path.join(Paths.get_original_thumb_path(), webp_path) lg_img_path = os.path.join(Paths.get_lg_thumb_path(), webp_path) @@ -40,7 +41,7 @@ def extract_thumb(filepath: str, webp_path: str) -> bool: img.resize((tsize, tsize), Image.ANTIALIAS).save(lg_img_path, "webp") img.resize((sm_tsize, sm_tsize), Image.ANTIALIAS).save(sm_img_path, "webp") - if os.path.exists(lg_img_path): + if not overwrite and os.path.exists(lg_img_path): img_size = os.path.getsize(lg_img_path) if img_size > 0: @@ -67,7 +68,10 @@ def extract_thumb(filepath: str, webp_path: str) -> bool: return False -def extract_date(date_str: str | None) -> int | None: +def parse_date(date_str: str | None) -> int | None: + """ + Extracts the date from a string and returns a timestamp. + """ try: date = pendulum.parse(date_str, strict=False) return int(date.timestamp()) @@ -76,6 +80,10 @@ def extract_date(date_str: str | None) -> int | None: def get_tags(filepath: str): + """ + Returns the tags for a given audio file. + """ + filetype = filepath.split(".")[-1] filename = (filepath.split("/")[-1]).replace(f".{filetype}", "") @@ -149,7 +157,7 @@ def get_tags(filepath: str): tags.image = f"{tags.albumhash}.webp" tags.folder = win_replace_slash(os.path.dirname(filepath)) - tags.date = extract_date(tags.year) or int(last_mod) + tags.date = parse_date(tags.year) or int(last_mod) tags.filepath = win_replace_slash(filepath) tags.filetype = filetype tags.last_mod = last_mod diff --git a/app/lib/watchdogg.py b/app/lib/watchdogg.py index bb77bf91..a6dce629 100644 --- a/app/lib/watchdogg.py +++ b/app/lib/watchdogg.py @@ -184,7 +184,7 @@ def add_track(filepath: str) -> None: if not ArtistStore.artist_exists(artist.artisthash): ArtistStore.add_artist(Artist(artist.name)) - extract_thumb(filepath, track.image) + extract_thumb(filepath, track.image, overwrite=True) def remove_track(filepath: str) -> None: