adding cleanup of stale track entries

This commit is contained in:
evilchili
2022-12-10 10:14:06 -08:00
parent 228d44ce98
commit 1a2506742f
9 changed files with 64 additions and 28 deletions
+23 -2
View File
@@ -5,7 +5,7 @@ import music_tag
from pathlib import Path
from typing import Callable, Union, Iterable
from sqlalchemy import func
from sqlalchemy import func, delete
import groove.db
import groove.path
@@ -40,10 +40,31 @@ class MediaScanner:
async def _do_import():
logging.debug("Scanning filesystem (this may take a minute)...")
for path in sources:
asyncio.create_task(self._import_one_track(path))
if path.exists() and not path.is_dir():
asyncio.create_task(self._import_one_track(path))
asyncio.run(_do_import())
self.db.commit()
def cleanup(self) -> int:
"""
Check for the existence of every track in the databse.
"""
async def _del(track):
path = self.root / Path(track.relpath)
if path.exists():
return
logging.info(f"Deleting missing track {track.relpath}")
self.db.execute(
delete(groove.db.track).where(groove.db.track.c.id == track.id)
)
async def _do_cleanup():
logging.debug("Locating stale track definitions in the database...")
for track in self.db.query(groove.db.track).all():
asyncio.create_task(_del(track))
asyncio.run(_do_cleanup())
self.db.commit()
def _get_tags(self, path): # pragma: no cover
tags = music_tag.load_file(path)
return {