code cleanup

This commit is contained in:
daniel
2020-05-15 09:52:35 +02:00
parent ec8b9e6085
commit 964a5907e1

View File

@@ -30,28 +30,27 @@ if __name__ == "__main__":
# Init connection to remarkable server
rmapy = get_rmapy_client()
# init all services and apis we need
# zotero connection
zot = zotero.Zotero(cfg['zot_user_id'], "user", cfg['zot_api_key'])
rmapy = RMClient()
rmapy.renew_token()
# used for zotero pdf download
# webdav for pdf download
webdav = Client({
"webdav_hostname": cfg['webdav_url'],
"webdav_login": cfg['webdav_user'],
"webdav_password": cfg['webdav_password']
})
# temp dir for file processing
# create a temporary directory where we extract pdfs etc.
tempdir = TemporaryDirectory()
# find the zotero collection key
# find the zotero collection
# TODO fallback to main library?
print("Fetching collection from Zotero... ", end="", flush=True)
zot_collections = zot.collections()
for collection in zot_collections:
if collection['data']['name'] == cfg['zot_collection']:
zot_collection_key = collection['data']['key']
print("Done")
print("Done.")
# get pdf items in collection
print("Fetching item list... ", end="", flush=True)
@@ -60,9 +59,12 @@ if __name__ == "__main__":
if "contentType" in item['data']
and item['data']['contentType'] == "application/pdf"
]
print("Done")
print("Done.")
print(f"Items to sync: {len(zot_items_pdf)}")
if len(zot_items_pdf) == 0:
print("Nothing to do.")
exit(0)
# get existing documents and folders on remarkable
rm_items = rmapy.get_meta_items()
@@ -70,49 +72,51 @@ if __name__ == "__main__":
rm_documents = [f for f in rm_items if isinstance(f, Document)]
rm_existing_names = [f.VissibleName for f in rm_documents]
# find or create correct folder on remarkable
# find or create output folder
folder = [f for f in rm_folders if f.VissibleName == cfg['rm_folder']]
if len(folder) > 0:
folder = folder[0]
else: # folder does not exist yet
print(f"Creating folder {remarkable_folder}")
print(f"Creating folder {remarkable_folder} on RM.")
folder = Folder(cfg['rm_folder'])
rmapy.create_folder(folder)
# Process items
print("\n#### Processing files ####\n")
for item in zot_items_pdf:
title = item['data']['title']
print(title)
print(title, end="", flush=True)
if title[:-4] in rm_existing_names:
print("skip (already exists)")
print(" skipped (already exists).")
else:
# Download via webdav
print("download, ", end="", flush=True)
print(" download,", end="", flush=True)
key = item['key']
local_path = join(tempdir.name, f"{key}.zip")
webdav.download_sync(remote_path=f"{key}.zip", local_path=local_path)
# Unpack file
print("unpack, ", end="", flush=True)
print(" unzip,", end="", flush=True)
with zipfile.ZipFile(local_path, "r") as zip:
zip.extract(title, path=tempdir.name)
file_path = join(tempdir.name, title)
# Upload to RM
print("upload ", end="", flush=True)
print(" upload,", end="", flush=True)
rawDocument = ZipDocument(doc=file_path)
success = rmapy.upload(rawDocument, folder)
if success:
print("done")
print(" done.")
else:
print("Upload failed")
print("FAILED!")
exit(1)
# TODO flag for not deleting files
# Delete files from zotero collection
print("Removing items from zotero collection")
print("\nRemoving uploaded items from zotero collection...", end="", flush=True)
for item in zot_items:
if item['data']['itemType'] == "attachment":
continue
zot.deletefrom_collection(zot_collection_key, item)
print("Done.")
print("Sync complete.")