Skip to content

Commit 71a3883

Browse files
GaisbergGaisberg
Gaisberg
authored andcommitted
chore: symlink library fixes
1 parent f3fb343 commit 71a3883

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

src/program/program.py

+36-26
Original file line numberDiff line numberDiff line change
@@ -351,46 +351,56 @@ def _init_db_from_symlinks(self):
351351
"""Initialize the database from symlinks."""
352352
start_time = datetime.now()
353353
with db.Session() as session:
354-
res = session.execute(select(func.count(MediaItem.id))).scalar_one()
355-
errors = []
356-
added_items = set()
354+
# Check if database is empty
355+
if not session.execute(select(func.count(MediaItem.id))).scalar_one():
356+
if not settings_manager.settings.map_metadata:
357+
return
357358

358-
if res == 0 and settings_manager.settings.map_metadata:
359359
logger.log("PROGRAM", "Collecting items from symlinks, this may take a while depending on library size")
360360
items = self.services[SymlinkLibrary].run()
361-
progress, console = create_progress_bar(len(items))
361+
errors = []
362+
added_items = set()
362363

364+
progress, console = create_progress_bar(len(items))
363365
task = progress.add_task("Enriching items with metadata", total=len(items), log="")
366+
364367
with Live(progress, console=console, refresh_per_second=10):
365-
workers = os.getenv("SYMLINK_MAX_WORKERS", 4)
366-
with ThreadPoolExecutor(thread_name_prefix="EnhanceSymlinks", max_workers=int(workers)) as executor:
367-
future_to_item = {executor.submit(self._enhance_item, item): item for item in items if isinstance(item, (Movie, Show))}
368+
workers = int(os.getenv("SYMLINK_MAX_WORKERS", 4))
369+
with ThreadPoolExecutor(thread_name_prefix="EnhanceSymlinks", max_workers=workers) as executor:
370+
future_to_item = {
371+
executor.submit(self._enhance_item, item): item
372+
for item in items
373+
if isinstance(item, (Movie, Show))
374+
}
375+
368376
for future in as_completed(future_to_item):
377+
item = future_to_item[future]
378+
log_message = ""
379+
369380
try:
370-
item = future_to_item[future]
371-
if item and item.id not in added_items:
372-
# Check for existing item in the database
373-
existing_item = session.query(MediaItem).filter_by(id=item.id).first()
374-
if existing_item:
375-
errors.append(f"Duplicate item found in database for id: {item.id}")
376-
continue
377-
378-
added_items.add(item.id)
379-
enhanced_item = future.result()
380-
enhanced_item.store_state()
381-
session.add(enhanced_item)
382-
log_message = f"Indexed IMDb Id: {enhanced_item.id} as {enhanced_item.type.title()}: {enhanced_item.log_string}"
383-
else:
381+
if not item or item.imdb_id in added_items:
384382
errors.append(f"Duplicate symlink directory found for {item.log_string}")
385383
continue
384+
385+
# Check for existing item using your db_functions
386+
if db_functions.get_item_by_id(item.id, session=session):
387+
errors.append(f"Duplicate item found in database for id: {item.id}")
388+
continue
389+
390+
enhanced_item = future.result()
391+
enhanced_item.store_state()
392+
session.add(enhanced_item)
393+
added_items.add(item.imdb_id)
394+
395+
log_message = f"Indexed IMDb Id: {enhanced_item.id} as {enhanced_item.type.title()}: {enhanced_item.log_string}"
396+
386397
except Exception as e:
387398
logger.exception(f"Error processing {item.log_string}: {e}")
388399
finally:
389-
progress.update(task, advance=1, log=log_message if "log_message" in locals() else "")
390-
progress.update(task, log="Finished Indexing Symlinks!")
400+
progress.update(task, advance=1, log=log_message)
391401

392-
session.commit()
393-
session.expunge_all()
402+
progress.update(task, log="Finished Indexing Symlinks!")
403+
session.commit()
394404

395405
if errors:
396406
logger.error("Errors encountered during initialization")

0 commit comments

Comments
 (0)