Skip to content

Instantly share code, notes, and snippets.

@dmadisetti
Created September 20, 2021 12:08
Show Gist options
  • Save dmadisetti/ad54143b3620cb526debeda106e6341e to your computer and use it in GitHub Desktop.
Save dmadisetti/ad54143b3620cb526debeda106e6341e to your computer and use it in GitHub Desktop.
move files from zotero to one location, and renames them by the extra tag and builds a bibtex
from pyzotero import zotero
from os import path, environ
from glob import glob
import shutil
from pybtex import database as btex
zot = zotero.Zotero(environ["ZOTERO_ID"], "user", envior["ZOTERO_SECRET"])
items = zot.top()
HOME = "/home/dylan"
ROOT = f"{HOME}/phd/notes"
STORAGE = f"{HOME}/Zotero/storage"
BIBTEX = environ.get("BIBTEX", f"{ROOT}/references/references.bib")
bibtex = btex.parse_file(BIBTEX)
keys = [bibtex.entries[e].key for e in bibtex.entries]
for item in items:
key = item["data"].get("extra", "")
meta = {
kv[0]: kv[1] for x in key.split("\n") if len(kv := x.split(": ")) == 2
}
if key:
citeable = meta.get('tex.key', '').replace('-', '_')
file = f"{ROOT}/literature/{citeable}.pdf"
note = f"{ROOT}/reviews/{citeable}.md"
if path.exists(file) and not item["links"].get("attachment", None):
uploads = [file]
if path.exists(note):
uploads.append(note)
zot.attachment_simple(uploads, item["key"])
else:
attachment = item["links"].get("attachment", None)
if attachment:
title = ''.join(filter(str.isalnum, item['data']['title']))[:15]
file = f"{ROOT}/literature/{title}.pdf"
source = glob(f"{STORAGE}/{attachment['href'].split('/')[-1]}/*.pdf")[0]
print(attachment, source)
if (attachment.get("attachmentType", "") == "application/pdf" and
path.exists(source)):
shutil.copy(source, file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment