demo.py 2.4 KB

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