day_planning.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import icalendar
  2. import datetime
  3. import zoneinfo
  4. from pathlib import Path
  5. def today():
  6. """Returns a string containing all events for the day, their location and their time"""
  7. cal_path = Path("data/cal.ics") # Rename your calendar in cal.ics and put in in a folder called data (or just change the path)
  8. cal = icalendar.Calendar.from_ical(cal_path.read_bytes())
  9. today = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Paris")) # The date of today, in the Paris timezone, choose whichever you want
  10. attends_tab = []
  11. for event in cal.events:
  12. if event.get("DTSTART").dt.day == today.day and event.get("DTSTART").dt.month == today.month : # Check every event in the calendar and execute the code if their are today
  13. event_desc = "\n"+"### "+event.get("SUMMARY")+" ###"
  14. event_desc += "\n"+event.get("LOCATION")
  15. event_desc += "\n"+"<t:"+str(round(event.get("DTSTART").dt.timestamp()))+":t> - <t:"+str(round(event.get("DTEND").dt.timestamp()))+":t>" # Convert the date in vDDDType to a discord timestamp
  16. attends_tab.append((event_desc,event.get("DTSTART").dt.timestamp()))
  17. attends_tab_sorted = sorted(attends_tab, key= lambda x: x[1]) # Order every eent of today by ascending timestamp
  18. msg = "Voici l'emploi du temps du jour :\n"
  19. for event in attends_tab_sorted: # Add all events to the message
  20. msg += event[0]
  21. return msg
  22. def tomorrow():
  23. """Returns a string containing all events for the day, their location and their time"""
  24. cal_path = Path("data/cal.ics") # Rename your calendar in cal.ics and put in in a folder called data (or just change the path)
  25. cal = icalendar.Calendar.from_ical(cal_path.read_bytes())
  26. today = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Paris")) # The date of today, in the Paris timezone, choose whichever you want
  27. attends_tab = []
  28. for event in cal.events:
  29. if event.get("DTSTART").dt.day - 1 == today.day and event.get("DTSTART").dt.month == today.month : # Check every event in the calendar and execute the code if their are today
  30. event_desc = "\n"+"### "+event.get("SUMMARY")+" ###"
  31. event_desc += "\n"+event.get("LOCATION")
  32. event_desc += "\n"+"<t:"+str(round(event.get("DTSTART").dt.timestamp()))+":t> - <t:"+str(round(event.get("DTEND").dt.timestamp()))+":t>" # Convert the date in vDDDType to a discord timestamp
  33. attends_tab.append((event_desc,event.get("DTSTART").dt.timestamp()))
  34. attends_tab_sorted = sorted(attends_tab, key= lambda x: x[1]) # Order every eent of today by ascending timestamp
  35. msg = "Voici l'emploi du temps du jour :\n"
  36. for event in attends_tab_sorted: # Add all events to the message
  37. msg += event[0]
  38. return msg