Compare commits
No commits in common. "7b06f62fe72595965f408700eea95e59aa07a435" and "7fa0adbff9f8cf45fe6c52b305b6bc6e1777bd22" have entirely different histories.
7b06f62fe7
...
7fa0adbff9
@ -81,6 +81,7 @@ async def build_library(root_dir):
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
track = mapping.next()
|
track = mapping.next()
|
||||||
|
if track:
|
||||||
tracks.append(track)
|
tracks.append(track)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
break
|
break
|
||||||
@ -88,7 +89,6 @@ async def build_library(root_dir):
|
|||||||
if percent >= prev_percent + 2.5:
|
if percent >= prev_percent + 2.5:
|
||||||
print(f"{percent}%")
|
print(f"{percent}%")
|
||||||
prev_percent = percent
|
prev_percent = percent
|
||||||
tracks = [t for t in tracks if t]
|
|
||||||
if not tracks:
|
if not tracks:
|
||||||
print("No new tracks found!")
|
print("No new tracks found!")
|
||||||
return
|
return
|
||||||
@ -108,38 +108,5 @@ async def build_library(root_dir):
|
|||||||
await cur.executemany(tracks_data)
|
await cur.executemany(tracks_data)
|
||||||
print("Done")
|
print("Done")
|
||||||
|
|
||||||
|
|
||||||
async def prune_dead_tracks(root_dir):
|
|
||||||
"""Removes tracks which exist in the database but not in the library."""
|
|
||||||
print("Scanning library...")
|
|
||||||
|
|
||||||
db_pool = await asyncpg.create_pool(**config.db)
|
|
||||||
|
|
||||||
lib_filepaths = []
|
|
||||||
for dir_path, sub_dirs, files in os.walk(root_dir):
|
|
||||||
for file in files:
|
|
||||||
if not os.path.splitext(file)[1] in MUSIC_EXT:
|
|
||||||
continue
|
|
||||||
filepath = os.path.join(root_dir, dir_path, file)
|
|
||||||
filepath = os.path.relpath(filepath, config.music_dir)
|
|
||||||
lib_filepaths.append(filepath)
|
|
||||||
|
|
||||||
async with db_pool.acquire() as conn:
|
|
||||||
db_filepaths = await conn.fetch("SELECT filepath FROM track")
|
|
||||||
db_filepaths = [track['filepath'] for track in db_filepaths]
|
|
||||||
|
|
||||||
filepaths_to_prune = []
|
|
||||||
for db_filepath in db_filepaths:
|
|
||||||
if db_filepath not in lib_filepaths:
|
|
||||||
filepaths_to_prune.append((db_filepath,))
|
|
||||||
|
|
||||||
print("Found", len(filepaths_to_prune), "dead tracks. Pruning...")
|
|
||||||
async with db_pool.acquire() as conn:
|
|
||||||
stmt = await conn.prepare("DELETE FROM track WHERE filepath = $1")
|
|
||||||
await stmt.executemany(filepaths_to_prune)
|
|
||||||
print("Done")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
#asyncio.run(build_library(config.music_dir))
|
asyncio.run(build_library(config.music_dir))
|
||||||
asyncio.run(prune_dead_tracks(config.music_dir))
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user