samuelyeewl / zotrm

Tool to sync Zotero papers to the reMarkable tablet.
24 stars 9 forks source link

zotero takes a list of filenames as argument #12

Open einalex opened 3 years ago

einalex commented 3 years ago
Traceback (most recent call last):
  File "/home/user/git/projects/Remarkable/zotrm/zotrm/zotrm.py", line 539, in <module>
    main(args.verbose, args.landscape, args.dry_run)
  File "/home/user/git/projects/Remarkable/zotrm/zotrm/zotrm.py", line 527, in main
    backsync_papers(zot, rmapi, config, verbose=verbose, dry_run=dry_run, exclude=sent_papers)
  File "/home/user/git/projects/Remarkable/zotrm/zotrm/zotrm.py", line 422, in backsync_papers
    pdf_res = add_pdf_to_paper(zot_attach, annotated_files[1], zot, config,
  File "/home/user/git/projects/Remarkable/zotrm/zotrm/zotrm.py", line 198, in add_pdf_to_paper
    res = zot.attachment_simple(pdf_file, attachment['data']['parentItem'])
  File "/home/user/.local/lib/python3.9/site-packages/pyzotero/zotero.py", line 1379, in attachment_simple
    return self._attachment(to_add, parentid)
  File "/home/user/.local/lib/python3.9/site-packages/pyzotero/zotero.py", line 1037, in _attachment
    res = attachment.upload()
  File "/home/user/.local/lib/python3.9/site-packages/pyzotero/zotero.py", line 2019, in upload
    self._create_prelim()
  File "/home/user/.local/lib/python3.9/site-packages/pyzotero/zotero.py", line 1870, in _create_prelim
    self._verify(self.payload)
  File "/home/user/.local/lib/python3.9/site-packages/pyzotero/zotero.py", line 1861, in _verify
    raise ze.FileDoesNotExist(
pyzotero.zotero_errors.FileDoesNotExist: The file at . couldn't be opened or found.

line 198 should be res = zot.attachment_simple([pdf_file], attachment['data']['parentItem'])

einalex commented 3 years ago

I still get another error though:

Traceback (most recent call last):
  File "/home/user/git/projects/Remarkable/zotrm/zotrm/zotrm.py", line 540, in <module>
    main(args.verbose, args.landscape, args.dry_run)
  File "/home/user/git/projects/Remarkable/zotrm/zotrm/zotrm.py", line 528, in main
    backsync_papers(zot, rmapi, config, verbose=verbose, dry_run=dry_run, exclude=sent_papers)
  File "/home/user/git/projects/Remarkable/zotrm/zotrm/zotrm.py", line 423, in backsync_papers
    pdf_res = add_pdf_to_paper(zot_attach, annotated_files[1], zot, config,
  File "/home/user/git/projects/Remarkable/zotrm/zotrm/zotrm.py", line 235, in add_pdf_to_paper
    raise Exception("Failed to upload pdf to Zotero", res)
Exception: ('Failed to upload pdf to Zotero', {'success': [{'itemType': 'attachment', 'linkMode': 'imported_file', 'title': 'Goodrich, Schultz - 2007 - Human-Robot Interaction A Survey _remarks.pdf', 'accessDate': '', 'note': '', 'tags': [], 'collections': [], 'relations': {}, 'contentType': '', 'charset': '', 'filename': './Goodrich, Schultz - 2007 - Human-Robot Interaction A Survey _remarks.pdf', 'md5': None, 'mtime': None, 'parentItem': 'IVTQ359Y', 'key': 'T6JWADR7'}], 'failure': [], 'unchanged': []})

this means the check for success fails. it expects the "success" field to contain 0, but it doesn't.

So what happens is that the remark pdf file is added to zotero, but the actual pdf file is not moved to where zotero expects it. This happens even after correcting the check for success.

einalex commented 3 years ago

        res = zot.attachment_simple([pdf_file], attachment['data']['parentItem'])
        dirname = os.path.join(config['zot_storage_dir'], res["success"][0]["key"])
        os.mkdir(dirname)
        out_pdf_file = os.path.join(dirname, pdf_file[2:])
        shutil.move(pdf_file, out_pdf_file)
``

This does the trick for me, but it might be wrong conceptually. I have no idea why the file isn't put there by the zotero api call.
einalex commented 3 years ago

so, further analysis revealed that the api call uploads the remarks file to zotero's cloud storage even if one is using webdav storage, which leads to the file being inaccessible from the local client and ending up in the wrong location.

samuelyeewl commented 3 years ago

Sorry for this bug, I don't use zotero's webdav storage. Could you use the pyzotero api to look at what the attachment metadata looks like for a .pdf file that is already on webdav? That will give a clue on how to replace / add the annotated file.