adhd-reminder/src/adhd_reminder/__main__.py

106 lines
4.0 KiB
Python

import time
import requests as r
import deltachat
from deltachat.tracker import ConfigureFailed
from time import sleep
import tempfile
import os
import configargparse
import pkg_resources
def setup_account(addr: str, app_pw: str, data_dir: str, debug: bool) -> deltachat.Account:
"""Create an deltachat account with a given addr/password combination.
:param addr: the email address of the account.
:param app_pw: the SMTP/IMAP password of the accont.
:param data_dir: the directory where the data(base) is stored.
:param debug: whether to show log messages for the account.
:return: the deltachat account object.
"""
try:
os.mkdir(os.path.join(data_dir, addr))
except FileExistsError:
pass
db_path = os.path.join(data_dir, addr, "db.sqlite")
ac = deltachat.Account(db_path)
if debug:
ac.add_account_plugin(deltachat.events.FFIEventLogger(ac))
if not ac.is_configured():
ac.set_config("addr", addr)
ac.set_config("mvbox_move", "0")
ac.set_config("sentbox_watch", "0")
ac.set_config("bot", "1")
ac.set_config("mdns_enabled", "0")
if app_pw != ac.get_config("mail_pw") or not ac.is_configured():
ac.set_config("mail_pw", app_pw)
configtracker = ac.configure()
try:
configtracker.wait_finish()
except ConfigureFailed as e:
print(
"configuration setup failed for %s with password:\n%s"
% (ac.get_config("addr"), ac.get_config("mail_pw"))
)
raise
ac.start_io()
avatar = pkg_resources.resource_filename(__name__, "avatar.jpg")
ac.set_avatar(avatar)
ac.set_config("displayname", "ADHS-Terminfinder")
return ac
class GreetBot:
def __init__(self, account: deltachat.Account, contacts: [str]):
self.account = account
self.domain = account.get_config("addr").split("@")[1]
self.curl = {
"url": "https://www.terminland.de/noris-psychotherapie/default.aspx?m=39059&ll=DAl3u&dpp=DAl3u&dlgid=8&step=3&dlg=1&a2291013099=2291025408&mode=frame&css=1'",
"expect": b"F\xc3\xbcr die ADHS-Abkl\xc3\xa4rung stehen derzeit keine freien Termine zur Verf\xc3\xbcgung.<br />Bitte versuchen Sie es zu einem sp\xc3\xa4teren Zeitpunkt noch einmal.",
"notification": "Hey, auf https://noris-psychotherapie.de/kontakt/ müsste es jetzt neue Termine für ADHS geben :) oder ich weiß nicht mehr wie ich richtig gucke, das kann auch sein. Dann sag nami@systemli.org bescheid.",
}
self.contacts = contacts
def crawl(self):
response = r.get(self.curl['url'])
if self.curl['expect'] not in response.content:
print(str(response.content))
for addr in self.contacts:
print("Notifying", addr)
chat = self.account.create_chat(addr)
chat.send_text(self.curl["notification"])
print("Sleeping for 24 hours now, then I'll try again.")
time.sleep(60*60*24)
def main():
args = configargparse.ArgumentParser()
args.add_argument("--email", help="the bot's email address", env_var="DELTACHAT_ADDR")
args.add_argument("--password", help="the bot's password", env_var="DELTACHAT_PASSWORD")
args.add_argument("--recipients", help="space-separated list of recipients", env_var="DELTACHAT_RECIPIENTS")
args.add_argument("--db_path", help="location of the Delta Chat database")
args.add_argument("--show-ffi", action="store_true", help="print Delta Chat log")
ops = args.parse_args()
# ensuring account data directory
if ops.db_path is None:
tempdir = tempfile.TemporaryDirectory(prefix="adhdbot")
ops.db_path = tempdir.name
elif not os.path.exists(ops.db_path):
os.mkdir(ops.db_path)
ac = setup_account(ops.email, ops.password, ops.db_path, ops.show_ffi)
greeter = GreetBot(ac, ops.recipients.split())
print("waiting for ADHD appointments... (and I hate waiting)")
while 1:
greeter.crawl()
sleep(60)
if __name__ == "__main__":
main()