diff --git a/plugin.py b/plugin.py index 34ce165..42b3def 100644 --- a/plugin.py +++ b/plugin.py @@ -31,7 +31,10 @@ from supybot import utils, plugins, ircutils, callbacks, world, conf, log from supybot.commands import * +# HTTP Imports +from supybot import httpserver +# Misc from num2words import num2words import pickle import datetime @@ -63,6 +66,110 @@ METHODS = { "smoked": ["Smoked"] } +class TripsitServerCallback(httpserver.SupyHTTPServerCallback): + name = 'Tripsit' + defaultResponse = """ + This plugin handles only GET request, please don't use other requests.""" + + def __init__(self, plugin): + self.plugin = plugin # to access db + + def doGet(self, handler, path): + if path == '/doses': + # Collect all dose logs from self.db + dose_logs = [] + for nick, data in self.plugin.db.items(): + for dose in data.get('doses', []): + dose_logs.append({ + 'nick': nick, + 'time': dose['time'], + 'dose': dose['dose'], + 'drug': dose['drug'], + 'method': dose['method'], + }) + + # Create HTML response + html_response = """ + + + + + Dose Logs + + + +

Dose Logs

+ + + + + + + + + + + + + """ + # Add rows for each dose log + for log in dose_logs: + html_response += f""" + + + + + + + + """ + + html_response += """ + +
NickTimeDoseDrugMethod
{log['nick']}{log['time']}{log['dose']}{log['drug']}{log['method']}
+ + + + """ + + # Send the HTML response + handler.send_response(200) + handler.send_header('Content-type', 'text/html') + handler.end_headers() + handler.wfile.write(html_response.encode('utf-8')) + else: + # 404 response for unknown paths + handler.send_response(404) + handler.send_header('Content-type', 'text/html') + handler.end_headers() + handler.wfile.write(b"

404 Not Found

") class Tripsit(callbacks.Plugin): """Harm-Reduction tools from tripsit's tripbot and the tripsitwiki""" @@ -74,6 +181,7 @@ class Tripsit(callbacks.Plugin): self.db = {} self._loadDb() world.flushers.append(self._flushDb) + httpserver.hook('tripsit', TripsitServerCallback(self)) # register the callback at `/tripsit` def _loadDb(self): """Loads the (flatfile) database mapping nicks to doses.""" @@ -95,6 +203,7 @@ class Tripsit(callbacks.Plugin): def die(self): self._flushDb() + httpserver.unhook('tripsit') world.flushers.remove(self._flushDb) self.__parent.die() @@ -211,7 +320,102 @@ class Tripsit(callbacks.Plugin): This command takes no arguments. Retrieves the number of doses logged for a given nick - """ + """ def doGet(self, handler, path): + if path == '/doses': + # Collect all dose logs from self.db + dose_logs = [] + for nick, data in self.plugin.db.items(): + for dose in data.get('doses', []): + dose_logs.append({ + 'nick': nick, + 'time': dose['time'], + 'dose': dose['dose'], + 'drug': dose['drug'], + 'method': dose['method'], + }) + + # Create HTML response + html_response = """ + + + + + Dose Logs + + + +

Dose Logs

+ + + + + + + + + + + + + """ + # Add rows for each dose log + for log in dose_logs: + html_response += f""" + + + + + + + + """ + + html_response += """ + +
NickTimeDoseDrugMethod
{log['nick']}{log['time']}{log['dose']}{log['drug']}{log['method']}
+ + + + """ + + # Send the HTML response + handler.send_response(200) + handler.send_header('Content-type', 'text/html') + handler.end_headers() + handler.wfile.write(html_response.encode('utf-8')) + else: + # 404 response for unknown paths + handler.send_response(404) + handler.send_header('Content-type', 'text/html') + handler.end_headers() + handler.wfile.write(b"

404 Not Found

") nick = msg.nick if nick in self.db: try: