mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-03 20:13:02 +00:00
extract thumbnail with overwrite for modified files
This commit is contained in:
+26
-9
@@ -77,12 +77,14 @@ class Populate:
|
|||||||
for _dir in dirs_to_scan:
|
for _dir in dirs_to_scan:
|
||||||
files = files.union(run_fast_scandir(_dir, full=True)[1])
|
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
|
untagged = files - unmodified
|
||||||
|
|
||||||
if len(untagged) != 0:
|
if len(untagged) != 0:
|
||||||
self.tag_untagged(untagged, key)
|
self.tag_untagged(untagged, key)
|
||||||
|
|
||||||
|
self.extract_thumb_with_overwrite(modified_tracks)
|
||||||
|
|
||||||
ProcessTrackThumbnails()
|
ProcessTrackThumbnails()
|
||||||
ProcessAlbumColors()
|
ProcessAlbumColors()
|
||||||
ProcessArtistColors()
|
ProcessArtistColors()
|
||||||
@@ -116,24 +118,26 @@ class Populate:
|
|||||||
since they were added to the database.
|
since they were added to the database.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
unmodified = set()
|
unmodified_paths = set()
|
||||||
modified = set()
|
modified_tracks: list[Track] = []
|
||||||
|
modified_paths = set()
|
||||||
|
|
||||||
for track in tracks:
|
for track in tracks:
|
||||||
try:
|
try:
|
||||||
if track.last_mod == os.path.getmtime(track.filepath):
|
if track.last_mod == os.path.getmtime(track.filepath):
|
||||||
unmodified.add(track.filepath)
|
unmodified_paths.add(track.filepath)
|
||||||
continue
|
continue
|
||||||
except (FileNotFoundError, OSError) as e:
|
except (FileNotFoundError, OSError) as e:
|
||||||
TrackStore.remove_track_obj(track)
|
TrackStore.remove_track_obj(track)
|
||||||
remove_tracks_by_filepaths(track.filepath)
|
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)
|
TrackStore.remove_tracks_by_filepaths(modified_paths)
|
||||||
remove_tracks_by_filepaths(modified)
|
remove_tracks_by_filepaths(modified_paths)
|
||||||
|
|
||||||
return unmodified
|
return unmodified_paths, modified_tracks
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def tag_untagged(untagged: set[str], key: str):
|
def tag_untagged(untagged: set[str], key: str):
|
||||||
@@ -178,6 +182,19 @@ class Populate:
|
|||||||
|
|
||||||
log.info("Added %s/%s tracks", tagged_count, len(untagged))
|
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):
|
def get_image(album: Album):
|
||||||
"""
|
"""
|
||||||
@@ -259,7 +276,7 @@ class FetchSimilarArtistsLastFM:
|
|||||||
tqdm(
|
tqdm(
|
||||||
pool.imap_unordered(save_similar_artists, artists),
|
pool.imap_unordered(save_similar_artists, artists),
|
||||||
total=len(artists),
|
total=len(artists),
|
||||||
desc="Downloading similar artists",
|
desc="Fetching similar artists",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+13
-5
@@ -24,9 +24,10 @@ def parse_album_art(filepath: str):
|
|||||||
return None
|
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)
|
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)
|
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((tsize, tsize), Image.ANTIALIAS).save(lg_img_path, "webp")
|
||||||
img.resize((sm_tsize, sm_tsize), Image.ANTIALIAS).save(sm_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)
|
img_size = os.path.getsize(lg_img_path)
|
||||||
|
|
||||||
if img_size > 0:
|
if img_size > 0:
|
||||||
@@ -67,7 +68,10 @@ def extract_thumb(filepath: str, webp_path: str) -> bool:
|
|||||||
return False
|
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:
|
try:
|
||||||
date = pendulum.parse(date_str, strict=False)
|
date = pendulum.parse(date_str, strict=False)
|
||||||
return int(date.timestamp())
|
return int(date.timestamp())
|
||||||
@@ -76,6 +80,10 @@ def extract_date(date_str: str | None) -> int | None:
|
|||||||
|
|
||||||
|
|
||||||
def get_tags(filepath: str):
|
def get_tags(filepath: str):
|
||||||
|
"""
|
||||||
|
Returns the tags for a given audio file.
|
||||||
|
"""
|
||||||
|
|
||||||
filetype = filepath.split(".")[-1]
|
filetype = filepath.split(".")[-1]
|
||||||
filename = (filepath.split("/")[-1]).replace(f".{filetype}", "")
|
filename = (filepath.split("/")[-1]).replace(f".{filetype}", "")
|
||||||
|
|
||||||
@@ -149,7 +157,7 @@ def get_tags(filepath: str):
|
|||||||
tags.image = f"{tags.albumhash}.webp"
|
tags.image = f"{tags.albumhash}.webp"
|
||||||
tags.folder = win_replace_slash(os.path.dirname(filepath))
|
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.filepath = win_replace_slash(filepath)
|
||||||
tags.filetype = filetype
|
tags.filetype = filetype
|
||||||
tags.last_mod = last_mod
|
tags.last_mod = last_mod
|
||||||
|
|||||||
@@ -184,7 +184,7 @@ def add_track(filepath: str) -> None:
|
|||||||
if not ArtistStore.artist_exists(artist.artisthash):
|
if not ArtistStore.artist_exists(artist.artisthash):
|
||||||
ArtistStore.add_artist(Artist(artist.name))
|
ArtistStore.add_artist(Artist(artist.name))
|
||||||
|
|
||||||
extract_thumb(filepath, track.image)
|
extract_thumb(filepath, track.image, overwrite=True)
|
||||||
|
|
||||||
|
|
||||||
def remove_track(filepath: str) -> None:
|
def remove_track(filepath: str) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user