attempt to cleanup library

This commit is contained in:
j 2019-01-12 23:11:33 +05:30
parent 7a000bf199
commit 56df24baad

View file

@ -132,6 +132,7 @@ def run_scan():
prefix = get_prefix() prefix = get_prefix()
books = collect_books(prefix) books = collect_books(prefix)
remove_missing(books) remove_missing(books)
ids = set()
added = 0 added = 0
@ -143,6 +144,16 @@ def run_scan():
if os.path.exists(f): if os.path.exists(f):
id = media.get_id(f) id = media.get_id(f)
file = File.get(id) file = File.get(id)
if id in ids:
logger.debug('file exists in multiple locations %s', id)
if file:
f1 = file.fullpath()
f2 = os.path.join(prefix, f)
if f1 != f2 and os.path.exists(f1) and os.path.exists(f2):
logger.debug('"%s" vs "%s"', f1, f2)
else:
ids.add(id)
if not file: if not file:
file = add_file(id, f, prefix, f) file = add_file(id, f, prefix, f)
added += 1 added += 1
@ -154,9 +165,24 @@ def run_scan():
add_record('edititem', item.id, item.meta) add_record('edititem', item.id, item.meta)
item.update() item.update()
added += 1 added += 1
library_items = len(user.library.items)
if added: if added:
trigger_event('change', {}) trigger_event('change', {})
logger.debug('imported %s unknown books', added) logger.debug('imported %s unknown books', added)
if len(ids) != len(books):
logger.debug('number of books %s vs number of ids %s', len(books), len(ids))
if library_items != len(books):
logger.debug('number of books %s vs number of items in library %s', len(books), library_items)
library_items = set([str(i) for i in user.library.items])
gone = library_items - ids
if gone:
logger.debug('cleaning up %s deleted records', len(gone))
for id in gone:
i = Item.get(id)
i.remove_file()
missing = ids - library_items
if missing:
logger.debug('%s items in library without a record', len(missing))
def change_path(old, new): def change_path(old, new):
new_books = os.path.join(new, 'Books') new_books = os.path.join(new, 'Books')