1
0
Fork 0
mirror of https://gitlab.com/oloturia/buonanotte.git synced 2025-01-04 20:47:20 +01:00

First commit

This commit is contained in:
oloturia 2020-03-20 17:48:07 +01:00
commit 9adb052869
4 changed files with 71 additions and 0 deletions

2
.gitignore vendored Normal file
View file

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

54
buonanotte.py Normal file
View file

@ -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
register_app.py Normal file
View file

@ -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
schedule.csv Normal file
View file