main.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from source import *
  2. import config
  3. from event import Event, HeapEvent
  4. from datetime import datetime, timedelta
  5. from time import sleep
  6. import notification
  7. from geocoding import position_of_location, k_neighbors
  8. from gmail import get_list_event_gmail
  9. from gcal import get_list_event_gcal
  10. def make_tz_aware(dt, tz='UTC', is_dst=None):
  11. """Add timezone information to a datetime object, only if it is naive."""
  12. tz = dt.tzinfo or tz
  13. try:
  14. tz = pytz.timezone(tz)
  15. except AttributeError:
  16. pass
  17. return tz.localize(dt, is_dst=is_dst)
  18. def gen_sources(sourceProviders, location):
  19. position = position_of_location(location)
  20. if not position:
  21. print("Unable to find a position for " + location)
  22. else:
  23. for sp in sourceProviders:
  24. # keep 2 nearest sources (and distance equivalent), if distance < 2 km
  25. ids_pos = {id: pos for (dist, pos, id) in k_neighbors(sp.dic_of_positions(), position, 2) if dist < 2}
  26. print('Cherche les sources :', list(ids_pos.keys()))
  27. for source in sp.sources_of_ids(ids_pos):
  28. yield source
  29. def get_events():
  30. # événements test
  31. manual = [Event('manual_1', datetime.now()+timedelta(minutes=30, seconds=10), "cinéma mk2 bibliothèque", "film !")]
  32. return get_list_event_gmail() + get_list_event_gcal() + manual
  33. def main():
  34. sourceProviders = [SourceProvider_ratp(),
  35. SourceProvider_jcdecaux_vls(),
  36. SourceProvider_transilien()]
  37. event_seen = set()
  38. heap = HeapEvent()
  39. gap = timedelta(minutes=30) # 30 minutes : time to check trafic before an event
  40. refresh = timedelta(seconds=30) # grab events every 30 secondes
  41. while True:
  42. # feed heap
  43. for event in get_events():
  44. # check if we already know it
  45. if event.id not in event_seen:
  46. event_seen.add(event.id)
  47. print()
  48. if (event.date - datetime.now()).total_seconds() < 0:
  49. print("Ignore event in the past:")
  50. else:
  51. print("Add event:")
  52. heap.push(event)
  53. print(str(event))
  54. # sleep the min between 1 minute and the next event - gap
  55. next = refresh
  56. if not heap.empty():
  57. next = min(next, heap.top().date-datetime.now()-gap)
  58. if next.total_seconds() > 0:
  59. print()
  60. print("Sleeping " + str(next) + "...")
  61. sleep(next.total_seconds())
  62. # next event
  63. if not heap.empty() and heap.top().date-datetime.now() < gap:
  64. event = heap.pop()
  65. print()
  66. print("Check event:")
  67. print(str(event))
  68. # get useful ids of sources for this location, and grab info from internet
  69. sources=gen_sources(sourceProviders, event.location)
  70. if 'print' in config.notification['methods']:
  71. print() # show an empty line
  72. for src in sources:
  73. if src.problem():
  74. # there is a problem ! We notify the user...
  75. notification.notify(src.message)
  76. if __name__ == "__main__":
  77. main()