main.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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, 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('Cherches les sources : ', list(ids_pos.keys()))
  27. for source in sp.sources_of_ids(ids_pos):
  28. yield source
  29. def get_events():
  30. return get_list_event_gmail() + get_list_event_gcal()
  31. def main():
  32. sourceProviders = [SourceProvider_ratp(),
  33. SourceProvider_jcdecaux_vls(),
  34. SourceProvider_transilien()]
  35. event_seen = set()
  36. heap = HeapEvent()
  37. manual = [Event('manual_1', datetime.now()+timedelta(minutes=30, seconds=10), "22 rue Henri Barbusse Villejuif", "descr Villejuif"),
  38. Event('manual_2', datetime.now()+timedelta(minutes=30, seconds=3), "68 rue Camille Desmoulins Cachan", "descr Cachan"),
  39. Event('manual_3', datetime.now()+timedelta(minutes=30, seconds=12), "université Paris Diderot", "descr p7")]
  40. gap = timedelta(minutes=30) # 30 minutes : time to check trafic before an event
  41. refresh = timedelta(seconds=30) # grab events every 30 secondes
  42. while True:
  43. # feed heap
  44. for event in get_events() + manual:
  45. # check if we already know it
  46. if event.id not in event_seen:
  47. event_seen.add(event.id)
  48. print()
  49. if (event.date - datetime.now()).total_seconds() < 0:
  50. print("Ignore event in the past:")
  51. else:
  52. print("Add event:")
  53. heap.push(event)
  54. print(str(event))
  55. # sleep the min between 1 minute and the next event - gap
  56. next = refresh
  57. if not heap.empty():
  58. next = min(next, heap.top().date-datetime.now()-gap)
  59. if next.total_seconds() > 0:
  60. print()
  61. print("Sleeping " + str(next) + "...")
  62. sleep(next.total_seconds())
  63. # next event
  64. if not heap.empty() and heap.top().date-datetime.now() < gap:
  65. event = heap.pop()
  66. print()
  67. print("Check event:")
  68. print(str(event))
  69. # get useful ids of sources for this location, and grab info from internet
  70. sources=gen_sources(sourceProviders, event.location)
  71. if 'print' in config.notification['methods']:
  72. print() # show an empty line
  73. for src in sources:
  74. if src.problem():
  75. # there is a problem ! We notify the user...
  76. notification.notify(src.message)
  77. if __name__ == "__main__":
  78. main()