buonanotte.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/python3
  2. from mastodon import Mastodon, StreamListener
  3. from dateutil.tz import tzutc
  4. from dateutil import parser
  5. import datetime
  6. import re
  7. import csv
  8. API_URL = "https://botsin.space"
  9. regex_h = re.compile("\d+(?=\s(ore|ora|heu|Stu|stu|hor|hou))")
  10. regex_m = re.compile("\d+(?=\s(min|Min))")
  11. languages = {
  12. "it":["Ciao @"," ti ricorderò di andare a domire fra "," è ora di andare a dormire! Buonanotte!"],
  13. "es":["Hola @"," te recordaré que te vayas a dormi en "," es hora de ir a dormir, buenas noches!"],
  14. "fr":["Salut @"," je vais te rappeler d'aller dormir en "," il est temps de dormir! Bonne nuit!"],
  15. "pt":["Ola' @"," vou lembrá-lo a dormir em "," é hora de ir dormir! Boa noite!"],
  16. "de":["Hallo @"," ich werde Sie daran erinnern in "," es ist Zeit zu schlafen! Gute Nacht!"],
  17. "en":["Hello @"," I'll remind you to go to sleep in "," time to go to bed! Good night!"]
  18. }
  19. class goodListener(StreamListener):
  20. def on_notification(self,notification):
  21. try:
  22. account = notification["account"]["acct"]
  23. content = notification["status"]["content"]
  24. goodNight_hours = regex_h.search(content)
  25. goodNight_minutes = regex_m.search(content)
  26. if content.find("dormire") != -1:
  27. lang = 'it'
  28. elif content.find("dormiria") != -1:
  29. lang = 'es'
  30. elif content.find("dormir") != -1:
  31. lang = 'fr'
  32. elif content.find("dormia") != -1:
  33. lang = 'pt'
  34. elif content.find("schlafen") != -1:
  35. lang = 'de'
  36. else:
  37. lang = 'en'
  38. greet = languages[lang][0]
  39. reminder = languages[lang][1]
  40. except KeyError:
  41. return
  42. result = ""
  43. try:
  44. hours_delay = int(goodNight_hours.group())
  45. result += goodNight_hours.group()+"h "
  46. except AttributeError:
  47. hours_delay = 0
  48. try:
  49. minutes_delay = int(goodNight_minutes.group())
  50. result += goodNight_minutes.group()+"m"
  51. except AttributeError:
  52. minutes_delay = 0
  53. if hours_delay == minutes_delay == 0:
  54. mastodon.status_post("Can't find a valid time")
  55. return
  56. datesleep = (datetime.datetime.now()+datetime.timedelta(hours=hours_delay,minutes=minutes_delay)).strftime("%Y/%m/%d %H:%M")
  57. with open("/mnt/nas/tmp/schedule.csv","a") as file:
  58. row = [datesleep,account,lang]
  59. writer = csv.writer(file)
  60. writer.writerow(row)
  61. mastodon.status_post(greet+account+reminder+result,visibility="direct")
  62. return
  63. def handle_heartbeat(self):
  64. with open("/mnt/nas/tmp/schedule.csv","r") as file:
  65. reader = csv.reader(file)
  66. sentToBed = []
  67. for line,row in enumerate(reader):
  68. if (parser.parse(row[0]) < datetime.datetime.now()):
  69. greet = languages[row[2]][0]
  70. goodnight = languages[row[2]][2]
  71. mastodon.status_post(greet+row[1]+goodnight,visibility="direct")
  72. sentToBed.append(line)
  73. if (len(sentToBed) > 0):
  74. with open("/mnt/nas/tmp/schedule.csv","r") as file:
  75. lines = file.readlines()
  76. with open("/mnt/nas/tmp/schedule.csv","w") as file:
  77. for line,row in enumerate(lines):
  78. if not (line in sentToBed):
  79. file.write(row)
  80. if __name__ == "__main__":
  81. with open("/home/goodnight/Documents/buonanotte/token") as f:
  82. createapp = f.readlines()
  83. createapp = [x.strip() for x in createapp]
  84. TOKEN = createapp[0]
  85. mastodon = Mastodon(access_token = TOKEN, api_base_url = API_URL)
  86. listener = goodListener()
  87. mastodon.stream_user(listener)