Import from custom db to zotero
hello I made a small script that should import my books from TinyDB to Zotero.
Almost everything works. The problem seems to be attaching the file with relative path because I would like to use the records on both Windows and Linux.
This is the piece of script to attach the file that gives me problems
# --- Allega il PDF (se presente) ---
pdf_path = self.get_pdf_path(record)
if pdf_path:
# Verifica esistenza PRIMA di chiamare Zotero API
if os.path.exists(pdf_path):
print_status(f" Tentativo di allegare: {os.path.basename(pdf_path)}", "info")
try:
# Usa attachment_simple per caricare una COPIA del file
attach_response = self.zot.attachment_simple([item_key], pdf_path)
# Verifica risposta: successo รจ chiave alfanumerica 8 caratteri
is_successful_attach = isinstance(attach_response, str) and len(attach_response) == 8 and attach_response.isalnum()
if not is_successful_attach:
error_msg_attach = attach_response if isinstance(attach_response, str) else "Risposta inattesa per allegato"
# Logga come warning, non fallire l'intero record
print_status(f" Fallito allegato PDF: {error_msg_attach}", "warning")
self.log_error(f"{title_short} (Allegato)", f"Fallito allegato PDF: {error_msg_attach}")
else:
print_status(f" Allegato PDF '{os.path.basename(pdf_path)}' creato con key: {attach_response}", "success")
except Exception as attach_err:
# Logga eccezione generica dell'allegato come warning
print_status(f" Errore durante l'allegato di '{os.path.basename(pdf_path)}': {attach_err}", "warning")
self.log_error(f"{title_short} (Allegato)", str(attach_err))
Almost everything works. The problem seems to be attaching the file with relative path because I would like to use the records on both Windows and Linux.
This is the piece of script to attach the file that gives me problems
# --- Allega il PDF (se presente) ---
pdf_path = self.get_pdf_path(record)
if pdf_path:
# Verifica esistenza PRIMA di chiamare Zotero API
if os.path.exists(pdf_path):
print_status(f" Tentativo di allegare: {os.path.basename(pdf_path)}", "info")
try:
# Usa attachment_simple per caricare una COPIA del file
attach_response = self.zot.attachment_simple([item_key], pdf_path)
# Verifica risposta: successo รจ chiave alfanumerica 8 caratteri
is_successful_attach = isinstance(attach_response, str) and len(attach_response) == 8 and attach_response.isalnum()
if not is_successful_attach:
error_msg_attach = attach_response if isinstance(attach_response, str) else "Risposta inattesa per allegato"
# Logga come warning, non fallire l'intero record
print_status(f" Fallito allegato PDF: {error_msg_attach}", "warning")
self.log_error(f"{title_short} (Allegato)", f"Fallito allegato PDF: {error_msg_attach}")
else:
print_status(f" Allegato PDF '{os.path.basename(pdf_path)}' creato con key: {attach_response}", "success")
except Exception as attach_err:
# Logga eccezione generica dell'allegato come warning
print_status(f" Errore durante l'allegato di '{os.path.basename(pdf_path)}': {attach_err}", "warning")
self.log_error(f"{title_short} (Allegato)", str(attach_err))