Compare commits
No commits in common. "b00e7f17702d3fe416f80a4f8c8f85d92745ad81" and "721e308207b46f8a03fd499240ebbd4adefd5a03" have entirely different histories.
b00e7f1770
...
721e308207
2 changed files with 46 additions and 73 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +0,0 @@
|
||||||
testfile.csv
|
|
118
papis-marvin
118
papis-marvin
|
@ -5,28 +5,13 @@
|
||||||
# the iOS application 'Marvin Reader'. In the app, export your
|
# the iOS application 'Marvin Reader'. In the app, export your
|
||||||
# annotations as 'csv' format and then point the script to the
|
# annotations as 'csv' format and then point the script to the
|
||||||
# resulting file.
|
# resulting file.
|
||||||
# https://git.martyoeh.me/Marty/papis-marvin
|
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
import re
|
from typing import List
|
||||||
import logging
|
|
||||||
from typing import Dict
|
|
||||||
import papis.api
|
import papis.api
|
||||||
import papis.pick
|
|
||||||
import papis.format
|
|
||||||
import papis.commands.edit
|
|
||||||
import papis.commands.list
|
import papis.commands.list
|
||||||
import papis.commands.add
|
|
||||||
import papis.notes
|
|
||||||
import papis.config
|
|
||||||
import papis.database
|
import papis.database
|
||||||
import isbnlib
|
import re
|
||||||
import papis.isbn
|
import subprocess
|
||||||
|
|
||||||
logger = logging.getLogger("marvin")
|
|
||||||
logger.setLevel(logging.DEBUG)
|
|
||||||
|
|
||||||
DEFAULT_CSV_PATH = "/home/marty/Nextcloud/Personal/Backups/Journal.csv"
|
|
||||||
|
|
||||||
|
|
||||||
def main(fpath, db):
|
def main(fpath, db):
|
||||||
|
@ -39,74 +24,59 @@ def main(fpath, db):
|
||||||
write_to_files(notes)
|
write_to_files(notes)
|
||||||
|
|
||||||
|
|
||||||
def get_all_annotations(db, csv) -> Dict:
|
def get_all_annotations(db, csv):
|
||||||
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"]):
|
||||||
doc = get_document(db, row["Author"], row["Title"])
|
documents = get_documents(db, row["Author"], title_stripped)
|
||||||
if not doc:
|
if not documents:
|
||||||
|
print(
|
||||||
|
f"No papis entry found for Marvin entry - {row['Author']}: {title_stripped}.\nPlease manually create."
|
||||||
|
)
|
||||||
continue
|
continue
|
||||||
note_file = get_notefile(db, doc)
|
note_file = get_notefile(documents)
|
||||||
|
if not note_file:
|
||||||
|
print(
|
||||||
|
f"Found reference entry but no note file for - {row['Author']}: {title_stripped}."
|
||||||
|
)
|
||||||
|
manual_papis_entry()
|
||||||
|
|
||||||
text = format_entry(row)
|
text = format_entry(row)
|
||||||
|
|
||||||
if note_file and text:
|
if note_file and text:
|
||||||
if note_file not in notes.keys():
|
if not note_file in notes.keys():
|
||||||
notes[note_file] = []
|
notes[note_file] = []
|
||||||
notes[note_file].append(text)
|
notes[note_file].append(text)
|
||||||
return notes
|
return notes
|
||||||
|
|
||||||
|
|
||||||
def get_document(db, author, title):
|
# TODO Implement manual note creation
|
||||||
res = query_document(db, author, title)
|
def manual_papis_entry():
|
||||||
if not res:
|
if input(f"Create note file now? [y/N] ") == "y":
|
||||||
add_to_database(author, title)
|
# i think instead we need to run the papis.edit cmd with note bool true and maybe editor set to none?
|
||||||
res = query_document(db, author, title)
|
output = subprocess.run(
|
||||||
if not res:
|
["papis", "edit", "-n"], capture_output=True, shell=True
|
||||||
logger.warning(f"Nothing found for {author}: {title}.\nPlease create manually.")
|
)
|
||||||
|
print(output)
|
||||||
|
print("NOT IMPLEMENTED: Please create note file manually.")
|
||||||
return
|
return
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
# TODO warn user/ let him pick with picker if multiple docs found
|
|
||||||
def query_document(db, author, title):
|
|
||||||
title = strip_string(title)
|
|
||||||
for query in [f"author:({author}) title:({title})"]:
|
|
||||||
print(f"query: {query}")
|
|
||||||
res = db.query(query)
|
|
||||||
if len(res) >= 1:
|
|
||||||
return res[0]
|
|
||||||
|
|
||||||
|
|
||||||
def add_to_database(author, title, confirm=True, edit=False):
|
|
||||||
logger.info(f"Searching - '{title} {author}'")
|
|
||||||
data = None
|
|
||||||
try:
|
|
||||||
data = papis.isbn.get_data(f"{title}")
|
|
||||||
except isbnlib.ISBNLibException as e:
|
|
||||||
logger.error(e)
|
|
||||||
else:
|
else:
|
||||||
logger.warning(f"Found: {data}")
|
return
|
||||||
if data:
|
|
||||||
papis_data = papis.isbn.data_to_papis(data[0])
|
|
||||||
papis.commands.add.run([], data=papis_data, confirm=confirm, edit=edit)
|
|
||||||
|
|
||||||
|
|
||||||
def get_notefile(db, document) -> str | None:
|
def get_documents(db, author, title) -> List:
|
||||||
if not document.has("notes"):
|
return db.query(f"author:({author}) title:({title})")
|
||||||
notes_name = papis.config.getstring("notes-name")
|
|
||||||
document["notes"] = papis.format.format(notes_name, document)
|
|
||||||
document.save()
|
|
||||||
db.update(document)
|
|
||||||
|
|
||||||
notes_path = os.path.join(str(document.get_main_folder()), document["notes"])
|
|
||||||
|
|
||||||
if not os.path.exists(notes_path):
|
def get_notefile(document) -> str | None:
|
||||||
# TODO reimplement logger: logger.debug("Creating '%s'", notes_path)
|
note_file = papis.commands.list.run(notes=True, documents=document)
|
||||||
papis.notes.notes_path_ensured(document)
|
if not note_file:
|
||||||
return notes_path
|
return
|
||||||
|
return str(note_file[0])
|
||||||
|
|
||||||
|
|
||||||
# TODO implement custom formatting (akin to pubs-extract)
|
# TODO implement custom formatting (akin to pubs-extract)
|
||||||
|
@ -134,30 +104,34 @@ def is_same_book(title):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def write_to_files(notes: Dict):
|
def write_to_files(notes):
|
||||||
# write to notes
|
# write to notes
|
||||||
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:
|
||||||
logger.info(f"Editing {f}...")
|
print(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
|
||||||
logger.info(f"Added {num_added} entries to it.")
|
print(f"Added {num_added} entries to it.")
|
||||||
|
|
||||||
|
|
||||||
strip_pattern = re.compile(r"([^\s\w]|_)+\w*")
|
title_strip_pattern = re.compile(r"([^\s\w]|_)+")
|
||||||
|
|
||||||
|
|
||||||
def strip_string(title) -> str:
|
def strip_book_title(title) -> str:
|
||||||
return strip_pattern.sub("", title)
|
return title_strip_pattern.sub("", title)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# use argument passed to command as file or default file here
|
# use argument passed to command as file or default file here
|
||||||
fpath = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_CSV_PATH
|
fpath = (
|
||||||
|
sys.argv[1]
|
||||||
|
if len(sys.argv) > 1
|
||||||
|
else "/home/marty/Nextcloud/Personal/Backups/Journal.csv"
|
||||||
|
)
|
||||||
|
|
||||||
main(fpath, papis.database.get())
|
main(fpath, papis.database.get())
|
||||||
|
|
Loading…
Reference in a new issue