main.py 3.0 KB

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