From ff175849a5d98ef87ce8a677d3414b503ba0f76d Mon Sep 17 00:00:00 2001 From: falsycat Date: Sat, 11 Feb 2023 13:09:33 +0900 Subject: [PATCH] implement fetching a page to make a reserve --- main.py | 95 ++++++++++++++-------- mods/__pycache__/elicense.cpython-310.pyc | Bin 0 -> 2821 bytes mods/elicense.py | 81 ++++++++++++++++++ 3 files changed, 140 insertions(+), 36 deletions(-) create mode 100644 mods/__pycache__/elicense.cpython-310.pyc create mode 100644 mods/elicense.py diff --git a/main.py b/main.py index 12a1d40..89f0e11 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import logging import json import os import time @@ -6,11 +7,14 @@ from datetime import datetime, timezone from html.parser import HTMLParser from mastodon import Mastodon +import mods.elicense + class Handler: def __init__(self, app, st): - self.app = app - self.st = st + self.logging = logging.getLogger("app.handler") + self.app = app + self.st = st self.text = sanitize_html(st["content"]) self.tokens = self.text.split()[1:] @@ -18,7 +22,6 @@ class Handler: self.handle() def handle(self): - print(self.tokens) if self.tokens[0] == "watch": self.watch() else: @@ -32,45 +35,24 @@ class Handler: if elapsed.seconds < self.app.CONFIG["interval"]: self.app.reply(self.st, "accepted") - print("watching", date, slots) + mod = self.app.mods["elicense"] + mod.watch(self.st["id"], date, slots) class App: def __init__(self): + logging.basicConfig() + self.logging = logging.getLogger("app") + self.logging.setLevel(logging.DEBUG) + with open("config.json", "r") as f: self.CONFIG = json.load(f) + self.logging.info("configuration loaded") + self.M = self.authenticate() - self.run() + self.logging.info("authentication succeeded") - def reply(self, st, msg): - try: - print(msg) - #self.M.status_post( - # status = "@{} {}".format(st["account"]["acct"], msg), - # in_reply_to_id = st["id"]) - except Exception as e: - print("reply failure:", msg, "(", e, ")") - - def run(self): - while True: - try: - items = self.M.notifications( - account_id = self.CONFIG["master"], - types = "mention") - for item in items: - st = item["status"] - # handle the item - try: - Handler(self, st) - except Exception as e: - self.reply(st, "handling aborted: {}".format(e)) - self.M.notifications_dismiss(id = item["id"]) - - print("cycle done") - except Exception as e: - print("cycle aborted:", e) - time.sleep(self.CONFIG["interval"]) - return + self.mods = {} def authenticate(self): if os.path.exists("auth.json"): @@ -100,9 +82,50 @@ class App: dic = {"client_id": cid, "secret": secret, "token": tok} with open("auth.json", "w") as f: json.dump(dic, f, indent = 2) - return M + def install(self, name, mod): + self.logging.info("mod installed, "+name) + + logger = logging.getLogger("mod."+name) + self.mods[name] = mod.init(self.CONFIG["mods"][name], logger) + + def run(self): + self.install("elicense", mods.elicense) + while True: + try: + self.cycle() + self.logging.debug("cycle done") + except Exception as e: + self.logging.warning("cycle aborted:", e) + + for mod in self.mods: + self.mods[mod].cycle() + time.sleep(self.CONFIG["interval"]) + + def cycle(self): + items = self.M.notifications( + account_id = self.CONFIG["master"], + types = "mention") + for item in items: + st = item["status"] + try: + Handler(self, st) + except Exception as e: + self.reply(st, "handling aborted: {}".format(e)) + self.M.notifications_dismiss(id = item["id"]) + + # TODO: call mod cycles + + def reply(self, st, msg): + try: + self.logging.debug("reply:", msg) + #self.M.status_post( + # status = "@{} {}".format(st["account"]["acct"], msg), + # in_reply_to_id = st["id"]) + except Exception as e: + self.logging.error("reply failure:", msg, "(", e, ")") + def sanitize_html(html): class F(HTMLParser): @@ -115,4 +138,4 @@ def sanitize_html(html): f.feed(html) return f.text -App() +App().run() diff --git a/mods/__pycache__/elicense.cpython-310.pyc b/mods/__pycache__/elicense.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..95d3186637affa0389414b16bca622584d117082 GIT binary patch literal 2821 zcma)8TW=Fb6rP!#U2kkBq$m^+MTF8)Tw0r=UJ;@|xD+Y42#QcGRn}%_Vmqw8%gmU7 z>r@rtwUzqLgCq5Yhf0bRI zCq7vqWD^@Z9}bQ4P;?!PAc7{Ok-uq!x;AStc(cUo@&>0wc!EDB4gV?;z6c%@5v;RD z0Ih-up%qHL?llU6-XfK$XRs!zSEwz1^WF8u8>3sCgiV1{%=3|(Ox zloI0&p}o#-k*Bm?;WpILEXw;bv4tdUwK~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