From b24159d964d0940536fcf7d8da6c866af1af4c08 Mon Sep 17 00:00:00 2001 From: foosinn Date: Mon, 7 Sep 2020 23:38:48 +0200 Subject: [PATCH] matrix chatbot to find your key --- .gitignore | 1 + Dockerfile | 9 +++++++ README.md | 6 +++++ key.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 key.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..47def24 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/env/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..49ae423 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.8 + +WORKDIR /app +RUN true \ + && pip install beautifulsoup4 matrix-nio + +ADD . /app + +CMD python key.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..04c03fd --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# keyscan + +A small, single file matrix chatbot that parses Bamberg's `Fundbüro` for keys. +You will receive a matrix message once a new key was found. + +Made me very happy once. diff --git a/key.py b/key.py new file mode 100644 index 0000000..bcb0089 --- /dev/null +++ b/key.py @@ -0,0 +1,75 @@ +import hashlib +import time +import asyncio +import os + +import aiohttp +import bs4 +from nio import AsyncClient, MatrixRoom, RoomMessageText + + +class App: + def __init__(self, user="", password="", room_id=""): + self.user = user + self.password = password + self.room_id = room_id + self.known = set() + + async def setup(self): + self.client = AsyncClient("https://doesnt.social", self.user) + await self.client.login(self.password) + await self.send_message("hi there, i'm marvin") + + async def send_message(self, message: str): + print(message) + resp = await self.client.room_send( + room_id=self.room_id, + message_type="m.room.message", + content={ + "msgtype": "m.text", + "body": message, + }, + ) + print(resp) + + async def run(self): + await self.setup() + while True: + await self.update() + time.sleep(500) + + async def update(self): + text = "" + async with aiohttp.ClientSession() as session: + url = ( + "https://fundsuche02.kivbf.de//MyApp.asp" + "?wci=FundHeader&Mdt=Bamberg&format=&PLZ=96047" + "&KM=50&KATEGORIE=%7B705DA1A1-2E03-4B2A-A31D-4E78E713CD79%7D" + "&DATUM=01.09.2020&BESCHREIBUNG=&FILTER=A&MerkmaleData=" + ) + async with session.get(url) as resp: + text = await resp.text() + soup = bs4.BeautifulSoup(text, "html.parser") + elements = soup.find_all( + title="Link zum Suchergebnis, Link öffnet neues Fenster" + ) + for element in elements: + description = element.text.strip() + digest = hashlib.sha256(description.encode()).hexdigest() + if self.is_new(digest): + await self.send_message(description) + + def is_new(self, digest): + if digest in self.known: + return False + self.known.add(digest) + return True + + +if __name__ == "__main__": + app = App( + user=os.environ["USER"], + password=os.environ["PASSWORD"], + room_id=os.environ["ROOM_ID"], + ) + asyncio.get_event_loop().run_until_complete(app.run())