From ce861d453756130dc4e12a90314bc0879be941f3 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Sun, 27 Nov 2022 22:24:29 +0100 Subject: [PATCH] Commit a draft from after the first working session --- .gitignore | 2 ++ bot.py | 42 ++++++++++++++++++++++++++++++++++++++++++ config.py.default | 4 ++++ memory.py | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 .gitignore create mode 100644 bot.py create mode 100644 config.py.default create mode 100644 memory.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3afd512 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +config.py diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..903bcc5 --- /dev/null +++ b/bot.py @@ -0,0 +1,42 @@ +from mastodon import Mastodon +import config +import memory +import time + +secret_file = config.user + '.secret' + +def register(): + Mastodon.create_app( + config.user, + api_base_url = config.instance, + to_file = secret_file + ) + +def connect(password): + api = Mastodon( + client_id = secret_file, + api_base_url = config.instance + ) + api.log_in( + ) + +def scalar(a, b): + return len(a.intersection(b)) + +def answer(db, notifications): + for notification in notifications: + if notification.type == 'mention': + indexed_input = db.index(notification.status.content) + reply = memory.find_best_quote(db, indexed_input) + +def main(): + api = connect(???) + db = memory.init('pratchett') + last = None + while True: + notifications = api.notifications(since_id=last) + if len(notifications) > 0: + last = notifications[0] + answer(db, notifications) + else: + time.sleep(config.delay) diff --git a/config.py.default b/config.py.default new file mode 100644 index 0000000..0cb5284 --- /dev/null +++ b/config.py.default @@ -0,0 +1,4 @@ +instance = HOSTNAME_OF_THE_INSTANCE_HOSTING_THE_BOT +user = USER_ACCOUNT_OF_THE_BOT +email = EMAIL_ACCOUNT_OF_THE_BOT +delay = HOW_OFTEN_DOES_THE_BOT_LOOK_FOR_NEW_MESSAGES diff --git a/memory.py b/memory.py new file mode 100644 index 0000000..796411d --- /dev/null +++ b/memory.py @@ -0,0 +1,41 @@ +import csv +import json + +WORD_THRESHOLD = 4 + +def build_db(inputCSV, outputJSON): + db = [] + with open(inputCSV, 'r') as file: + csv_reader = csv.reader(file, delimiter=',') + data = False + for row in csv_reader: + if data: + db.append(row + (index(row[1]),)) + else: + data = True + with open(outputJSON, 'w') as file: + json.dump(serialize(db), file) + return db + +def serialize(db): + return list(map(lambda row: (row[0], row[1], list(row[2])), db)) + +def unserialize(db): + return list(map(lambda row: (row[0], row[1], set(row[2])), db)) + +def open_db(filePath): + with open(filePath, 'r') as file: + return unserialize(json.load(file)) + +def scalar(a, b): + return len(a.intersection(b)) + +def find_best_quote(db, indexed_input) + max_score = None + for entry in db: + + +def index(text): + words = map(lambda w: ''.join([c for c in w if c.isalpha()]), text.split(' ')) + important_words = set([w for w in words if len(w) > WORD_THRESHOLD]) + return important_words