mirror of
https://gitlab.com/oloturia/buonanotte.git
synced 2025-01-06 21:47:20 +01:00
now accepted relative time
This commit is contained in:
parent
91097cf5f5
commit
c4a9b292b6
5 changed files with 80 additions and 67 deletions
0
LICENSE
Normal file → Executable file
0
LICENSE
Normal file → Executable file
0
README.md
Normal file → Executable file
0
README.md
Normal file → Executable file
147
buonanotte.py
Normal file → Executable file
147
buonanotte.py
Normal file → Executable file
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/python3
|
||||||
from mastodon import Mastodon, StreamListener
|
from mastodon import Mastodon, StreamListener
|
||||||
from dateutil.tz import tzutc
|
from dateutil.tz import tzutc
|
||||||
from dateutil import parser
|
from dateutil import parser
|
||||||
|
@ -7,78 +7,91 @@ import re
|
||||||
import csv
|
import csv
|
||||||
|
|
||||||
API_URL = "https://botsin.space"
|
API_URL = "https://botsin.space"
|
||||||
regex = re.compile("([0-1]\d|[2][0-3]):[0-5]\d")
|
regex_h = re.compile("\d+(?=\s(ore|heu|Stu|stu|hor|hou))")
|
||||||
|
regex_m = re.compile("\d+(?=\s(min|Min))")
|
||||||
languages = {
|
languages = {
|
||||||
"it":["Ciao @"," ti ricorderò di andare a dormire alle ore "," è ora di andare a dormire! Buonanotte!"],
|
"it":["Ciao @"," ti ricorderò di andare a dormie fra "," è ora di andare a dormire! Buonanotte!"],
|
||||||
"es":["Hola @"," te recordaré que te vayas a dormir a las "," es hora de ir a dormir, buenas noches!"],
|
"es":["Hola @"," te recordaré que te vayas a dormi en "," es hora de ir a dormir, buenas noches!"],
|
||||||
"fr":["Salut @"," je vais te rappeler d'aller dormir à "," il est temps de dormir! Bonne nuit!"],
|
"fr":["Salut @"," je vais te rappeler d'aller dormir en "," il est temps de dormir! Bonne nuit!"],
|
||||||
"pt":["Ola' @"," vou lembrá-lo a dormir as "," é hora de ir dormir! Boa noite!"],
|
"pt":["Ola' @"," vou lembrá-lo a dormir em "," é hora de ir dormir! Boa noite!"],
|
||||||
"de":["Hallo @"," ich werde Sie daran erinnern um Uhr "," es ist Zeit zu schlafen! Gute Nacht!"],
|
"de":["Hallo @"," ich werde Sie daran erinnern in "," es ist Zeit zu schlafen! Gute Nacht!"],
|
||||||
"en":["Hello @"," I'll remind you to go to sleep at "," time to go to bed! Good night!"]
|
"en":["Hello @"," I'll remind you to go to sleep in "," time to go to bed! Good night!"]
|
||||||
}
|
}
|
||||||
|
|
||||||
class goodListener(StreamListener):
|
class goodListener(StreamListener):
|
||||||
|
|
||||||
def on_notification(self,notification):
|
def on_notification(self,notification):
|
||||||
try:
|
try:
|
||||||
account = notification["account"]["acct"]
|
account = notification["account"]["acct"]
|
||||||
content = notification["status"]["content"]
|
content = notification["status"]["content"]
|
||||||
goodNight = regex.search(content)
|
|
||||||
if content.find("dormire") != -1:
|
|
||||||
lang = 'it'
|
|
||||||
elif content.find("dormiria") != -1:
|
|
||||||
lang = 'es'
|
|
||||||
elif content.find("dormir") != -1:
|
|
||||||
lang = 'fr'
|
|
||||||
elif content.find("dormia") != -1:
|
|
||||||
lang = 'pt'
|
|
||||||
elif content.find("schlafen") != -1:
|
|
||||||
lang = 'de'
|
|
||||||
else:
|
|
||||||
lang = 'en'
|
|
||||||
greet = languages[lang][0]
|
|
||||||
reminder = languages[lang][1]
|
|
||||||
except KeyError:
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
result = goodNight.group()
|
|
||||||
if (parser.parse(result) < datetime.datetime.now()):
|
|
||||||
delta = 1
|
|
||||||
else:
|
|
||||||
delta = 0
|
|
||||||
datesleep = (datetime.datetime.now()+datetime.timedelta(days=delta)).strftime("%Y/%m/%d")+" "+result
|
|
||||||
with open("schedule.csv","a") as file:
|
|
||||||
row = [datesleep,account,lang]
|
|
||||||
writer = csv.writer(file)
|
|
||||||
writer.writerow(row)
|
|
||||||
mastodon.status_post(greet+account+reminder+result,visibility="direct")
|
|
||||||
except AttributeError:
|
|
||||||
return
|
|
||||||
|
|
||||||
def handle_heartbeat(self):
|
goodNight_hours = regex_h.search(content)
|
||||||
with open("schedule.csv","r") as file:
|
goodNight_minutes = regex_m.search(content)
|
||||||
reader = csv.reader(file)
|
if content.find("dormire") != -1:
|
||||||
sentToBed = []
|
lang = 'it'
|
||||||
for line,row in enumerate(reader):
|
elif content.find("dormiria") != -1:
|
||||||
if (parser.parse(row[0]) < datetime.datetime.now()):
|
lang = 'es'
|
||||||
greet = languages[row[2]][0]
|
elif content.find("dormir") != -1:
|
||||||
goodnight = languages[row[2]][2]
|
lang = 'fr'
|
||||||
mastodon.status_post(greet+row[1]+goodnight,visibility="direct")
|
elif content.find("dormia") != -1:
|
||||||
sentToBed.append(line)
|
lang = 'pt'
|
||||||
if (len(sentToBed) > 0):
|
elif content.find("schlafen") != -1:
|
||||||
with open("schedule.csv","r") as file:
|
lang = 'de'
|
||||||
lines = file.readlines()
|
else:
|
||||||
with open("schedule.csv","w") as file:
|
lang = 'en'
|
||||||
for line,row in enumerate(lines):
|
greet = languages[lang][0]
|
||||||
if not (line in sentToBed):
|
reminder = languages[lang][1]
|
||||||
file.write(row)
|
except KeyError:
|
||||||
|
return
|
||||||
|
|
||||||
|
result = ""
|
||||||
|
try:
|
||||||
|
hours_delay = int(goodNight_hours.group())
|
||||||
|
result += goodNight_hours.group()+"h "
|
||||||
|
except AttributeError:
|
||||||
|
hours_delay = 0
|
||||||
|
|
||||||
|
try:
|
||||||
|
minutes_delay = int(goodNight_minutes.group())
|
||||||
|
result += goodNight_minutes.group()+"m"
|
||||||
|
except AttributeError:
|
||||||
|
minutes_delay = 0
|
||||||
|
|
||||||
|
if hours_delay == minutes_delay == 0:
|
||||||
|
mastodon.status_post("Can't find a valid time")
|
||||||
|
|
||||||
|
datesleep = (datetime.datetime.now()+datetime.timedelta(hours=hours_delay,minutes=minutes_delay)).strftime("%Y/%m/%d %H:%M")
|
||||||
|
with open("/mnt/nas/tmp/schedule.csv","a") as file:
|
||||||
|
row = [datesleep,account,lang]
|
||||||
|
writer = csv.writer(file)
|
||||||
|
writer.writerow(row)
|
||||||
|
mastodon.status_post(greet+account+reminder+result,visibility="direct")
|
||||||
|
return
|
||||||
|
|
||||||
|
def handle_heartbeat(self):
|
||||||
|
with open("/mnt/nas/tmp/schedule.csv","r") as file:
|
||||||
|
reader = csv.reader(file)
|
||||||
|
sentToBed = []
|
||||||
|
for line,row in enumerate(reader):
|
||||||
|
if (parser.parse(row[0]) < datetime.datetime.now()):
|
||||||
|
greet = languages[row[2]][0]
|
||||||
|
goodnight = languages[row[2]][2]
|
||||||
|
mastodon.status_post(greet+row[1]+goodnight,visibility="direct")
|
||||||
|
sentToBed.append(line)
|
||||||
|
if (len(sentToBed) > 0):
|
||||||
|
with open("/mnt/nas/tmp/schedule.csv","r") as file:
|
||||||
|
lines = file.readlines()
|
||||||
|
with open("/mnt/nas/tmp/schedule.csv","w") as file:
|
||||||
|
for line,row in enumerate(lines):
|
||||||
|
if not (line in sentToBed):
|
||||||
|
file.write(row)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
with open("token") as f:
|
with open("/home/goodnight/Documents/buonanotte/token") as f:
|
||||||
createapp = f.readlines()
|
createapp = f.readlines()
|
||||||
createapp = [x.strip() for x in createapp]
|
createapp = [x.strip() for x in createapp]
|
||||||
TOKEN = createapp[0]
|
TOKEN = createapp[0]
|
||||||
mastodon = Mastodon(access_token = TOKEN, api_base_url = API_URL)
|
mastodon = Mastodon(access_token = TOKEN, api_base_url = API_URL)
|
||||||
listener = goodListener()
|
listener = goodListener()
|
||||||
mastodon.stream_user(listener)
|
mastodon.stream_user(listener)
|
||||||
|
|
0
register_app.py
Normal file → Executable file
0
register_app.py
Normal file → Executable file
Loading…
Reference in a new issue