Add automatic import for missing entries

This commit is contained in:
Marty Oehme 2023-02-23 10:33:54 +01:00
parent 32d57c79d3
commit cb1cfe22ab
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A

View file

@ -7,16 +7,23 @@
# resulting file. # resulting file.
import os import os
import sys import sys
import re
import logging
import subprocess
from typing import Dict, List from typing import Dict, List
import papis.api import papis.api
import papis.pick import papis.pick
import papis.format import papis.format
import papis.commands.edit import papis.commands.edit
import papis.config
import papis.commands.list import papis.commands.list
import papis.commands.add
import papis.config
import papis.database import papis.database
import re import isbnlib
import subprocess import papis.isbn
logger = logging.getLogger("marvin")
logger.setLevel(logging.DEBUG)
def main(fpath, db): def main(fpath, db):
@ -33,24 +40,13 @@ def get_all_annotations(db, csv) -> Dict:
notes = {} notes = {}
note_file = "" note_file = ""
for row in csv: for row in csv:
title_stripped = strip_book_title(row["Title"])
# switch to next book # switch to next book
if not is_same_book(row["Title"]): if not is_same_book(row["Title"]):
documents = get_documents(db, row["Author"], title_stripped) title_stripped = strip_string(row["Title"])
if not documents: doc = get_document(db, row["Author"], title_stripped)
print( if not doc:
f"No papis entry found for Marvin entry - {row['Author']}: {title_stripped}.\nPlease manually create."
)
continue continue
# TODO warn user/ let him pick with picker if multiple docs found note_file = get_notefile(db, doc)
doc = documents[0]
note_file = get_notefile(doc)
if not note_file:
print(
f"Found reference entry but no note file for - {row['Author']}: {title_stripped}."
)
note_file = create_note_file(db, doc)
text = format_entry(row) text = format_entry(row)
@ -61,36 +57,48 @@ def get_all_annotations(db, csv) -> Dict:
return notes return notes
def get_documents(db, author, title) -> List: # TODO warn user/ let him pick with picker if multiple docs found
return db.query(f"author:({author}) title:({title})") def get_document(db, author, title):
res = db.query(f"author:({author}) title:({title})")
if not res:
add_to_database(author, title)
res = db.query(f"author:({author}) title:({title})")
if not res:
logger.warning(
f"Nothing found for {author}: {title}.\nPlease create manually."
)
return res[0]
return res[0]
def get_notefile(document) -> str | None: def add_to_database(author, title, confirm=True, edit=False):
note_file = papis.commands.list.run(notes=True, documents=[document]) print(f"Searching - '{title} {author}'")
if not note_file: data = None
return try:
return str(note_file[0]) data = papis.isbn.get_data(f"{title}")
except isbnlib.ISBNLibException as e:
print(f"ERROR: {e}")
else:
print(f"Found: {data}")
if data:
papis_data = papis.isbn.data_to_papis(data[0])
papis.commands.add.run([], data=papis_data, confirm=confirm, edit=edit)
def create_note_file(db, document): def get_notefile(db, document) -> str | None:
if input(f"Create note file now? [y/N] ") == "y":
if not document.has("notes"): if not document.has("notes"):
notes_name = papis.config.getstring("notes-name") notes_name = papis.config.getstring("notes-name")
document["notes"] = papis.format.format(notes_name, document) document["notes"] = papis.format.format(notes_name, document)
document.save() document.save()
db.update(document) db.update(document)
notes_path = os.path.join( notes_path = os.path.join(str(document.get_main_folder()), document["notes"])
str(document.get_main_folder()), print(f"{document['title']}: {document['notes']}")
document["notes"]
)
if not os.path.exists(notes_path): if not os.path.exists(notes_path):
# TODO reimplement logger: logger.debug("Creating '%s'", notes_path) # TODO reimplement logger: logger.debug("Creating '%s'", notes_path)
papis.commands.edit.create_notes(document, notes_path) papis.commands.edit.create_notes(document, notes_path)
return notes_path return notes_path
else:
return
# TODO implement custom formatting (akin to pubs-extract) # TODO implement custom formatting (akin to pubs-extract)
@ -123,21 +131,21 @@ def write_to_files(notes: Dict):
for f, entries in notes.items(): for f, entries in notes.items():
if f: if f:
with open(f, "a") as note: with open(f, "a") as note:
print(f"Editing {f}...") logger.info(f"Editing {f}...")
num_added = 0 num_added = 0
for entry in entries: for entry in entries:
with open(f) as noteread: with open(f) as noteread:
if entry not in noteread.read(): if entry not in noteread.read():
note.write(f"{entry}\n\n") note.write(f"{entry}\n\n")
num_added += 1 num_added += 1
print(f"Added {num_added} entries to it.") logger.info(f"Added {num_added} entries to it.")
title_strip_pattern = re.compile(r"([^\s\w]|_)+") strip_pattern = re.compile(r"([^\s\w]|_)+")
def strip_book_title(title) -> str: def strip_string(title) -> str:
return title_strip_pattern.sub("", title) return strip_pattern.sub("", title)
if __name__ == "__main__": if __name__ == "__main__":