From 9adb0528698a6c5934b952870f59dc0d9f1cfda8 Mon Sep 17 00:00:00 2001 From: oloturia Date: Fri, 20 Mar 2020 17:48:07 +0100 Subject: [PATCH] First commit --- .gitignore | 2 ++ buonanotte.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ register_app.py | 15 ++++++++++++++ schedule.csv | 0 4 files changed, 71 insertions(+) create mode 100644 .gitignore create mode 100644 buonanotte.py create mode 100644 register_app.py create mode 100644 schedule.csv diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..50669a4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +token +secret diff --git a/buonanotte.py b/buonanotte.py new file mode 100644 index 0000000..fbe868c --- /dev/null +++ b/buonanotte.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +from mastodon import Mastodon, StreamListener +from dateutil.tz import tzutc +from dateutil import parser +import datetime +import re +import csv + +API_URL = "https://botsin.space" +regex = re.compile("([0-1]\d|[2][0-3]):[0-5]\d") + + +class goodListener(StreamListener): + + def on_notification(self,notification): + print("Notification received") + account = notification["account"]["acct"] + content = notification["status"]["content"] + goodNight = regex.search(content) + try: + result = goodNight.group() + with open("schedule.csv","a") as file: + row = [result,account] + writer = csv.writer(file) + writer.writerow(row) + mastodon.toot("Ciao @"+account+", ti ricordero' di andare a dormire alle ore "+result) + except AttributeError: + mastodon.toot("Ciao @"+account+", non ho capito a che ora vorresti andare a dormire") + + def handle_heartbeat(self): + with open("schedule.csv","r") as file: + reader = csv.reader(file) + sentToBed = [] + for line,row in enumerate(reader): + if parser.parse(row[0]) < datetime.datetime.now(): + mastodon.toot("Ciao @"+row[1]+" e' ora di andare a dormire! Buonanotte!") + sentToBed.append(line) + if (len(sentToBed) > 0): + with open("schedule.csv","r") as file: + lines = file.readlines() + with open("schedule.csv","w") as file: + for line,row in enumerate(lines): + if not (line in sentToBed): + file.write(row) + + +if __name__ == "__main__": + with open("token") as f: + createapp = f.readlines() + createapp = [x.strip() for x in createapp] + TOKEN = createapp[0] + mastodon = Mastodon(access_token = TOKEN, api_base_url = API_URL) + listener = goodListener() + mastodon.stream_user(listener) diff --git a/register_app.py b/register_app.py new file mode 100644 index 0000000..eacac1d --- /dev/null +++ b/register_app.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 +from mastodon import Mastodon + +''' +you need to register the app only once +''' + + +Mastodon.create_app( + "goodnight", + api_base_url = "https://botsin.space", + to_file = "secret" +) + +print("App registered!") diff --git a/schedule.csv b/schedule.csv new file mode 100644 index 0000000..e69de29