From d700e3d1735663b90c5563c488f3349b123442b4 Mon Sep 17 00:00:00 2001 From: Adrien Raison Date: Mon, 15 Feb 2021 18:28:27 +0100 Subject: [PATCH] add emailing feature --- src/zotero_joplin_binder/binder.py | 54 +++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/zotero_joplin_binder/binder.py b/src/zotero_joplin_binder/binder.py index 167a158..07e6ec2 100644 --- a/src/zotero_joplin_binder/binder.py +++ b/src/zotero_joplin_binder/binder.py @@ -15,16 +15,52 @@ zotero_key = os.getenv("ZOTERO_API_KEY") joplin_key = os.getenv("JOPLIN_API_KEY") -d = requests.get(f"https://api.zotero.org/groups/2611287/items?key={zotero_key}") -print() - -with open("../../output_shared.json", "w") as outfile: - json.dump(d.json(), outfile) - -current = [] +current_items = [] while True: time.sleep(2) with open("../../output_shared.json", "r") as file: - data = json.load(file) - print(data[0]) + d = requests.get( + f"https://api.zotero.org/groups/2611287/collections?key={zotero_key}" + ) + for col in d.json(): + l = requests.get( + f"""https://api.zotero.org/groups/2611287/collections/{col['key']}/items?key={zotero_key}""" + ) + for item in l.json(): + + couple = (col["key"], item["key"]) + if couple in current_items: + continue + else: + current_items.append(couple) + try: + sender = os.getenv("EMAIL") + password = os.getenv("PASSWORD") + receivers = ["adrienrsn@gmail.com"] + smtp_serv = os.getenv("SERVER") + port = 587 + from email.message import EmailMessage + + msg = EmailMessage() + content = f""" + "Authors : {[ + " ".join([it["firstName"], it["lastName"]]) + for it in item["data"]["creators"] + ]} + Title : {item["data"]["title"]} + Url : {item["data"]["url"]} + """ + + msg[ + "Subject" + ] = f"""A new document {item["data"]["title"]} has been added to our shared library""" + msg["From"] = os.getenv("EMAIL") + msg["To"] = ", ".join(receivers) + msg.set_content(content) + + with smtplib.SMTP(smtp_serv, port) as server: + server.login(sender, password) + server.send_message(msg) + except KeyError: + continue