Browse Source

First commit

oloturia 4 years ago
commit
9adb052869
4 changed files with 71 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 54 0
      buonanotte.py
  3. 15 0
      register_app.py
  4. 0 0
      schedule.csv

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+token
+secret

+ 54 - 0
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)

+ 15 - 0
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!")

+ 0 - 0
schedule.csv