From 4a7416853a63b82567a7e407652d0f6cfc67b519 Mon Sep 17 00:00:00 2001 From: mungai-njoroge Date: Sun, 9 Jul 2023 17:57:14 +0300 Subject: [PATCH] fix album having date = 0 --- app/api/album.py | 5 ++--- app/lib/populate.py | 7 ++++--- app/lib/trackslib.py | 2 +- app/lib/watchdogg.py | 39 ++++++++++++++++++++++++++------------- app/models/album.py | 1 + app/models/track.py | 17 +++++++++++++---- app/settings.py | 6 +++--- app/store/tracks.py | 17 +++++++++++++---- 8 files changed, 63 insertions(+), 31 deletions(-) diff --git a/app/api/album.py b/app/api/album.py index dfd7076..2aebdbb 100644 --- a/app/api/album.py +++ b/app/api/album.py @@ -9,8 +9,7 @@ from flask import Blueprint, request from app.db.sqlite.albums import SQLiteAlbumMethods as adb from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb -from app.db.sqlite.lastfm.similar_artists import \ - SQLiteLastFMSimilarArtists as lastfmdb +from app.db.sqlite.lastfm.similar_artists import SQLiteLastFMSimilarArtists as lastfmdb from app.models import FavType, Track from app.serializers.album import serialize_for_card from app.serializers.track import track_serializer @@ -38,7 +37,7 @@ def get_album_tracks_and_info(): return error_msg, 400 try: - albumhash = data["hash"] + albumhash: str = data["albumhash"] except KeyError: return error_msg, 400 diff --git a/app/lib/populate.py b/app/lib/populate.py index 28a6e9d..b193aa2 100644 --- a/app/lib/populate.py +++ b/app/lib/populate.py @@ -9,7 +9,8 @@ from tqdm import tqdm from app import settings from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb -from app.db.sqlite.lastfm.similar_artists import SQLiteLastFMSimilarArtists as lastfmdb +from app.db.sqlite.lastfm.similar_artists import \ + SQLiteLastFMSimilarArtists as lastfmdb from app.db.sqlite.settings import SettingsSQLMethods as sdb from app.db.sqlite.tracks import SQLiteTrackMethods from app.lib.artistlib import CheckArtistImages @@ -90,7 +91,7 @@ class Populate: tried_to_download_new_images = True try: CheckArtistImages() - except (RequestConnectionError, ReadTimeout): + except (RequestConnectionError, ReadTimeout) as e: log.error( "Internet connection lost. Downloading artist images stopped." ) @@ -123,7 +124,7 @@ class Populate: continue except FileNotFoundError: print(f"File not found: {track.filepath}") - TrackStore.tracks.remove(track) + TrackStore.remove_track_obj(track) remove_tracks_by_filepaths(track.filepath) modified.add(track.filepath) diff --git a/app/lib/trackslib.py b/app/lib/trackslib.py index a423185..8883e50 100644 --- a/app/lib/trackslib.py +++ b/app/lib/trackslib.py @@ -15,5 +15,5 @@ def validate_tracks() -> None: """ for track in tqdm(TrackStore.tracks, desc="Checking for deleted tracks"): if not os.path.exists(track.filepath): - TrackStore.tracks.remove(track) + TrackStore.remove_track_obj(track) tdb.remove_tracks_by_filepaths(track.filepath) diff --git a/app/lib/watchdogg.py b/app/lib/watchdogg.py index 0acea0e..60d8598 100644 --- a/app/lib/watchdogg.py +++ b/app/lib/watchdogg.py @@ -184,6 +184,7 @@ def remove_track(filepath: str) -> None: class Handler(PatternMatchingEventHandler): files_to_process = [] files_to_process_windows = [] + file_sizes = {} root_dirs = [] dir_map = [] @@ -215,6 +216,7 @@ class Handler(PatternMatchingEventHandler): """ self.files_to_process.append(event.src_path) self.files_to_process_windows.append(event.src_path) + self.file_sizes[event.src_path] = os.path.getsize(event.src_path) def on_deleted(self, event): """ @@ -269,18 +271,29 @@ class Handler(PatternMatchingEventHandler): if event.src_path not in self.files_to_process_windows: return - file_size = -1 + # Check if file write operation is complete + current_size = os.path.getsize(event.src_path) + previous_size = self.file_sizes.get(event.src_path, -1) - while file_size != os.path.getsize(event.src_path): - file_size = os.path.getsize(event.src_path) - time.sleep(0.1) + if current_size == previous_size: + # Wait for a short duration to ensure the file write operation is complete + time.sleep(0.5) - try: - os.rename(event.src_path, event.src_path) - path = self.get_abs_path(event.src_path) - remove_track(path) - add_track(path) - self.files_to_process_windows.remove(event.src_path) - except OSError: - # File is locked, skipping - pass + # Check the file size again + current_size = os.path.getsize(event.src_path) + + if current_size == previous_size: + try: + os.rename(event.src_path, event.src_path) + path = self.get_abs_path(event.src_path) + remove_track(path) + add_track(path) + self.files_to_process_windows.remove(event.src_path) + del self.file_sizes[event.src_path] + except OSError: + # File is locked, skipping + pass + return + + # Update the file size for the next iteration + self.file_sizes[event.src_path] = current_size diff --git a/app/models/album.py b/app/models/album.py index 1786747..417162d 100644 --- a/app/models/album.py +++ b/app/models/album.py @@ -191,5 +191,6 @@ class Album: if len(dates) == 0: self.date = 0 + return self.date = datetime.datetime.fromtimestamp(min(dates)).year diff --git a/app/models/track.py b/app/models/track.py index ad63d39..e4f1d6b 100644 --- a/app/models/track.py +++ b/app/models/track.py @@ -2,8 +2,13 @@ from dataclasses import dataclass from app.settings import get_flag, ParserFlags from app.utils.hashing import create_hash -from app.utils.parsers import split_artists, remove_prod, parse_feat_from_title, clean_title, \ - get_base_title_and_versions +from app.utils.parsers import ( + split_artists, + remove_prod, + parse_feat_from_title, + clean_title, + get_base_title_and_versions, +) from .artist import ArtistMinimal @@ -64,7 +69,9 @@ class Track: self.title = new_title if get_flag(ParserFlags.CLEAN_ALBUM_TITLE): - self.album, _ = get_base_title_and_versions(self.album, get_versions=False) + self.album, _ = get_base_title_and_versions( + self.album, get_versions=False + ) if get_flag(ParserFlags.MERGE_ALBUM_VERSIONS): self.recreate_albumhash() @@ -93,7 +100,9 @@ class Track: if self.og_title == self.title and self.og_album == self.album: return - self.trackhash = create_hash(", ".join([a.name for a in self.artist]), self.og_album, self.title) + self.trackhash = create_hash( + ", ".join([a.name for a in self.artist]), self.og_album, self.title + ) def recreate_artists_hash(self): """ diff --git a/app/settings.py b/app/settings.py index 68ae420..f5fc40a 100644 --- a/app/settings.py +++ b/app/settings.py @@ -78,9 +78,9 @@ class Paths: # defaults class Defaults: - THUMB_SIZE = 400 - SM_THUMB_SIZE = 144 - SM_ARTIST_IMG_SIZE = 144 + THUMB_SIZE = 500 + SM_THUMB_SIZE = 64 + SM_ARTIST_IMG_SIZE = 64 """ The size of extracted images in pixels """ diff --git a/app/store/tracks.py b/app/store/tracks.py index d093f4e..f49c3ea 100644 --- a/app/store/tracks.py +++ b/app/store/tracks.py @@ -1,8 +1,8 @@ from tqdm import tqdm -from app.models import Track from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb from app.db.sqlite.tracks import SQLiteTrackMethods as tdb +from app.models import Track from app.utils.bisection import UseBisection from app.utils.remove_duplicates import remove_duplicates @@ -41,6 +41,16 @@ class TrackStore: cls.tracks.extend(tracks) + @classmethod + def remove_track_obj(cls, track: Track): + """ + Removes a single track from the store. + """ + try: + cls.tracks.remove(track) + except ValueError: + pass + @classmethod def remove_track_by_filepath(cls, filepath: str): """ @@ -49,7 +59,7 @@ class TrackStore: for track in cls.tracks: if track.filepath == filepath: - cls.tracks.remove(track) + cls.remove_track_obj(track) break @classmethod @@ -58,10 +68,9 @@ class TrackStore: Removes multiple tracks from the store by their filepaths. """ - for track in cls.tracks: if track.filepath in filepaths: - cls.tracks.remove(track) + cls.remove_track_obj(track) @classmethod def remove_tracks_by_dir_except(cls, dirs: list[str]):