demo.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from geocoding import *
  2. import webbrowser
  3. import main
  4. import notification
  5. import tempfile
  6. import os
  7. def str_of_pos(pos):
  8. return '{lat: ' + str(pos[0]) + ', lng: ' + str(pos[1]) + '}'
  9. def str_of_marker(pos, description):
  10. return """
  11. marker = new google.maps.Marker({
  12. position: """ + str_of_pos(pos) + """,
  13. map: map,
  14. title: '""" + description + """'
  15. });
  16. var infowindow = new google.maps.InfoWindow({
  17. content: '""" + description + """'
  18. });
  19. infowindow.open(marker.get('map'), marker);"""
  20. def open_markers(center, markers):
  21. # markers_str = map(lambda pos_name: '&markers=color:blue|label:' + pos_name[1] + '|' + str_of_pos(pos_name[0]) , markers)
  22. # if input('Montrer la carte ? [Y/n] ') != 'n':
  23. # url = "http://maps.google.com/maps/api/staticmap?center" + str_of_pos(center) +\
  24. # '&zoom=14&size=512x512&maptype=roadmap' +\
  25. # '&markers=color:red|label:destination|' + str_of_pos(center) + ''.join(markers_str)
  26. # webbrowser.open_new(url)
  27. if input('Montrer la carte ? [Y/n] ') != 'n':
  28. html = """<!DOCTYPE html>
  29. <html>
  30. <head>
  31. <style type="text/css">
  32. html, body { height: 100%; margin: 0; padding: 0; }
  33. #map { height: 100%; }
  34. </style>
  35. </head>
  36. <body>
  37. <div id="map"></div>
  38. <script type="text/javascript">
  39. var map;
  40. function initMap() {
  41. map = new google.maps.Map(document.getElementById('map'), {
  42. center: """ + str_of_pos(center) + """,
  43. zoom: 15
  44. });
  45. """ + str_of_marker(center, 'destination')
  46. for (pos, name) in markers:
  47. html += str_of_marker(pos, name)
  48. html += """
  49. }
  50. </script>
  51. <script async defer
  52. src="https://maps.googleapis.com/maps/api/js?key=""" + config.api_key['google_maps'] + """&callback=initMap">
  53. </script>
  54. </body>
  55. </html>"""
  56. (f, path) = tempfile.mkstemp(suffix=".html")
  57. os.write(f, html.encode())
  58. os.close(f)
  59. webbrowser.open_new('file://' + path)
  60. def message():
  61. yield("Faisons un essai !\n")
  62. yield("Encore une fois ?\n")
  63. yield("Pas fatigués ?\n")
  64. while True:
  65. yield("")
  66. sourceProviders = [SourceProvider_ratp(),
  67. SourceProvider_jcdecaux_vls(),
  68. SourceProvider_transilien()]
  69. for m in message():
  70. print(m)
  71. location = input("Adresse : ")
  72. position = position_of_location(location)
  73. if not position:
  74. print("Désolé, l'adresse n'est pas reconnue :-(")
  75. else:
  76. #open_pos(position)
  77. markers = []
  78. for source in main.gen_sources(sourceProviders, location):
  79. if source.problem():
  80. print("Problème : ", end='')
  81. else:
  82. print("Pas de problème : ", end='')
  83. notification.notify(source.message)
  84. markers.append((source.pos, source.id))
  85. open_markers(position, markers)
  86. # empty line
  87. print()