fix album having date = 0

This commit is contained in:
mungai-njoroge 2023-07-09 17:57:14 +03:00
parent 65d21d07da
commit 4a7416853a
8 changed files with 63 additions and 31 deletions

View File

@ -9,8 +9,7 @@ from flask import Blueprint, request
from app.db.sqlite.albums import SQLiteAlbumMethods as adb from app.db.sqlite.albums import SQLiteAlbumMethods as adb
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
from app.db.sqlite.lastfm.similar_artists import \ from app.db.sqlite.lastfm.similar_artists import SQLiteLastFMSimilarArtists as lastfmdb
SQLiteLastFMSimilarArtists as lastfmdb
from app.models import FavType, Track from app.models import FavType, Track
from app.serializers.album import serialize_for_card from app.serializers.album import serialize_for_card
from app.serializers.track import track_serializer from app.serializers.track import track_serializer
@ -38,7 +37,7 @@ def get_album_tracks_and_info():
return error_msg, 400 return error_msg, 400
try: try:
albumhash = data["hash"] albumhash: str = data["albumhash"]
except KeyError: except KeyError:
return error_msg, 400 return error_msg, 400

View File

@ -9,7 +9,8 @@ from tqdm import tqdm
from app import settings from app import settings
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb 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.settings import SettingsSQLMethods as sdb
from app.db.sqlite.tracks import SQLiteTrackMethods from app.db.sqlite.tracks import SQLiteTrackMethods
from app.lib.artistlib import CheckArtistImages from app.lib.artistlib import CheckArtistImages
@ -90,7 +91,7 @@ class Populate:
tried_to_download_new_images = True tried_to_download_new_images = True
try: try:
CheckArtistImages() CheckArtistImages()
except (RequestConnectionError, ReadTimeout): except (RequestConnectionError, ReadTimeout) as e:
log.error( log.error(
"Internet connection lost. Downloading artist images stopped." "Internet connection lost. Downloading artist images stopped."
) )
@ -123,7 +124,7 @@ class Populate:
continue continue
except FileNotFoundError: except FileNotFoundError:
print(f"File not found: {track.filepath}") print(f"File not found: {track.filepath}")
TrackStore.tracks.remove(track) TrackStore.remove_track_obj(track)
remove_tracks_by_filepaths(track.filepath) remove_tracks_by_filepaths(track.filepath)
modified.add(track.filepath) modified.add(track.filepath)

View File

@ -15,5 +15,5 @@ def validate_tracks() -> None:
""" """
for track in tqdm(TrackStore.tracks, desc="Checking for deleted tracks"): for track in tqdm(TrackStore.tracks, desc="Checking for deleted tracks"):
if not os.path.exists(track.filepath): if not os.path.exists(track.filepath):
TrackStore.tracks.remove(track) TrackStore.remove_track_obj(track)
tdb.remove_tracks_by_filepaths(track.filepath) tdb.remove_tracks_by_filepaths(track.filepath)

View File

@ -184,6 +184,7 @@ def remove_track(filepath: str) -> None:
class Handler(PatternMatchingEventHandler): class Handler(PatternMatchingEventHandler):
files_to_process = [] files_to_process = []
files_to_process_windows = [] files_to_process_windows = []
file_sizes = {}
root_dirs = [] root_dirs = []
dir_map = [] dir_map = []
@ -215,6 +216,7 @@ class Handler(PatternMatchingEventHandler):
""" """
self.files_to_process.append(event.src_path) self.files_to_process.append(event.src_path)
self.files_to_process_windows.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): def on_deleted(self, event):
""" """
@ -269,18 +271,29 @@ class Handler(PatternMatchingEventHandler):
if event.src_path not in self.files_to_process_windows: if event.src_path not in self.files_to_process_windows:
return 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): if current_size == previous_size:
file_size = os.path.getsize(event.src_path) # Wait for a short duration to ensure the file write operation is complete
time.sleep(0.1) time.sleep(0.5)
try: # Check the file size again
os.rename(event.src_path, event.src_path) current_size = os.path.getsize(event.src_path)
path = self.get_abs_path(event.src_path)
remove_track(path) if current_size == previous_size:
add_track(path) try:
self.files_to_process_windows.remove(event.src_path) os.rename(event.src_path, event.src_path)
except OSError: path = self.get_abs_path(event.src_path)
# File is locked, skipping remove_track(path)
pass 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

View File

@ -191,5 +191,6 @@ class Album:
if len(dates) == 0: if len(dates) == 0:
self.date = 0 self.date = 0
return
self.date = datetime.datetime.fromtimestamp(min(dates)).year self.date = datetime.datetime.fromtimestamp(min(dates)).year

View File

@ -2,8 +2,13 @@ from dataclasses import dataclass
from app.settings import get_flag, ParserFlags from app.settings import get_flag, ParserFlags
from app.utils.hashing import create_hash from app.utils.hashing import create_hash
from app.utils.parsers import split_artists, remove_prod, parse_feat_from_title, clean_title, \ from app.utils.parsers import (
get_base_title_and_versions split_artists,
remove_prod,
parse_feat_from_title,
clean_title,
get_base_title_and_versions,
)
from .artist import ArtistMinimal from .artist import ArtistMinimal
@ -64,7 +69,9 @@ class Track:
self.title = new_title self.title = new_title
if get_flag(ParserFlags.CLEAN_ALBUM_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): if get_flag(ParserFlags.MERGE_ALBUM_VERSIONS):
self.recreate_albumhash() self.recreate_albumhash()
@ -93,7 +100,9 @@ class Track:
if self.og_title == self.title and self.og_album == self.album: if self.og_title == self.title and self.og_album == self.album:
return 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): def recreate_artists_hash(self):
""" """

View File

@ -78,9 +78,9 @@ class Paths:
# defaults # defaults
class Defaults: class Defaults:
THUMB_SIZE = 400 THUMB_SIZE = 500
SM_THUMB_SIZE = 144 SM_THUMB_SIZE = 64
SM_ARTIST_IMG_SIZE = 144 SM_ARTIST_IMG_SIZE = 64
""" """
The size of extracted images in pixels The size of extracted images in pixels
""" """

View File

@ -1,8 +1,8 @@
from tqdm import tqdm from tqdm import tqdm
from app.models import Track
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
from app.db.sqlite.tracks import SQLiteTrackMethods as tdb from app.db.sqlite.tracks import SQLiteTrackMethods as tdb
from app.models import Track
from app.utils.bisection import UseBisection from app.utils.bisection import UseBisection
from app.utils.remove_duplicates import remove_duplicates from app.utils.remove_duplicates import remove_duplicates
@ -41,6 +41,16 @@ class TrackStore:
cls.tracks.extend(tracks) 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 @classmethod
def remove_track_by_filepath(cls, filepath: str): def remove_track_by_filepath(cls, filepath: str):
""" """
@ -49,7 +59,7 @@ class TrackStore:
for track in cls.tracks: for track in cls.tracks:
if track.filepath == filepath: if track.filepath == filepath:
cls.tracks.remove(track) cls.remove_track_obj(track)
break break
@classmethod @classmethod
@ -58,10 +68,9 @@ class TrackStore:
Removes multiple tracks from the store by their filepaths. Removes multiple tracks from the store by their filepaths.
""" """
for track in cls.tracks: for track in cls.tracks:
if track.filepath in filepaths: if track.filepath in filepaths:
cls.tracks.remove(track) cls.remove_track_obj(track)
@classmethod @classmethod
def remove_tracks_by_dir_except(cls, dirs: list[str]): def remove_tracks_by_dir_except(cls, dirs: list[str]):