Compare commits
10 commits
721e308207
...
b00e7f1770
Author | SHA1 | Date | |
---|---|---|---|
b00e7f1770 | |||
d4cbc2eb77 | |||
82f2e7d7df | |||
a150259538 | |||
94744fb55f | |||
b161413f13 | |||
cb1cfe22ab | |||
32d57c79d3 | |||
46ea119204 | |||
b6a28d8e6a |
2 changed files with 75 additions and 48 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
testfile.csv
|
122
papis-marvin
122
papis-marvin
|
@ -5,13 +5,28 @@
|
||||||
# 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
|
||||||
from typing import List
|
|
||||||
import papis.api
|
|
||||||
import papis.commands.list
|
|
||||||
import papis.database
|
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import logging
|
||||||
|
from typing import Dict
|
||||||
|
import papis.api
|
||||||
|
import papis.pick
|
||||||
|
import papis.format
|
||||||
|
import papis.commands.edit
|
||||||
|
import papis.commands.list
|
||||||
|
import papis.commands.add
|
||||||
|
import papis.notes
|
||||||
|
import papis.config
|
||||||
|
import papis.database
|
||||||
|
import isbnlib
|
||||||
|
import papis.isbn
|
||||||
|
|
||||||
|
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):
|
||||||
|
@ -24,59 +39,74 @@ def main(fpath, db):
|
||||||
write_to_files(notes)
|
write_to_files(notes)
|
||||||
|
|
||||||
|
|
||||||
def get_all_annotations(db, csv):
|
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)
|
doc = get_document(db, row["Author"], row["Title"])
|
||||||
if not documents:
|
if not doc:
|
||||||
print(
|
|
||||||
f"No papis entry found for Marvin entry - {row['Author']}: {title_stripped}.\nPlease manually create."
|
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
note_file = get_notefile(documents)
|
note_file = get_notefile(db, doc)
|
||||||
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 not note_file in notes.keys():
|
if note_file not in notes.keys():
|
||||||
notes[note_file] = []
|
notes[note_file] = []
|
||||||
notes[note_file].append(text)
|
notes[note_file].append(text)
|
||||||
return notes
|
return notes
|
||||||
|
|
||||||
|
|
||||||
# TODO Implement manual note creation
|
def get_document(db, author, title):
|
||||||
def manual_papis_entry():
|
res = query_document(db, author, title)
|
||||||
if input(f"Create note file now? [y/N] ") == "y":
|
if not res:
|
||||||
# i think instead we need to run the papis.edit cmd with note bool true and maybe editor set to none?
|
add_to_database(author, title)
|
||||||
output = subprocess.run(
|
res = query_document(db, author, title)
|
||||||
["papis", "edit", "-n"], capture_output=True, shell=True
|
if not res:
|
||||||
)
|
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:
|
||||||
return
|
logger.warning(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 get_documents(db, author, title) -> List:
|
def get_notefile(db, document) -> str | None:
|
||||||
return db.query(f"author:({author}) title:({title})")
|
if not document.has("notes"):
|
||||||
|
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"])
|
||||||
|
|
||||||
def get_notefile(document) -> str | None:
|
if not os.path.exists(notes_path):
|
||||||
note_file = papis.commands.list.run(notes=True, documents=document)
|
# TODO reimplement logger: logger.debug("Creating '%s'", notes_path)
|
||||||
if not note_file:
|
papis.notes.notes_path_ensured(document)
|
||||||
return
|
return notes_path
|
||||||
return str(note_file[0])
|
|
||||||
|
|
||||||
|
|
||||||
# TODO implement custom formatting (akin to pubs-extract)
|
# TODO implement custom formatting (akin to pubs-extract)
|
||||||
|
@ -104,34 +134,30 @@ def is_same_book(title):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def write_to_files(notes):
|
def write_to_files(notes: Dict):
|
||||||
# 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:
|
||||||
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]|_)+\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__":
|
||||||
# use argument passed to command as file or default file here
|
# use argument passed to command as file or default file here
|
||||||
fpath = (
|
fpath = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_CSV_PATH
|
||||||
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