commute.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import hashlib
  2. import json
  3. import requests
  4. import config
  5. import data
  6. import db
  7. import geo_util
  8. class TransitMethod:
  9. DRIVING = 'driving'
  10. WALKING = 'walking'
  11. CYCLING = 'bicycling'
  12. TRANSIT = 'transit'
  13. def _get_key(*args):
  14. k = ','.join([str(x) for x in args]).encode('utf-8')
  15. return hashlib.md5(k).hexdigest()[-12:]
  16. def _record_api_response(response):
  17. stmt = 'replace into transit (id, mode, submode, src_lat, src_lon, dest_lat, dest_lon, distance, duration, raw_json) VALUES'
  18. params = []
  19. for route in response.get('routes', []):
  20. for leg in route.get('legs', []):
  21. for step in leg.get('steps', []):
  22. distance = step['distance']['value']
  23. duration = step['duration']['value']
  24. source = round(step['start_location']['lat'], 10), round(step['start_location']['lng'], 10)
  25. destination = round(step['end_location']['lat'], 10), round(step['end_location']['lng'], 10)
  26. mode = step['travel_mode']
  27. submode = step.get('transit_details', {}).get('line', {}).get('vehicle', {}).get('type')
  28. stmt += ' (?, ?, ?, ?, ?, ?, ?, ?, ?, ?),'
  29. params.extend([
  30. _get_key(source, destination, mode, submode),
  31. mode,
  32. submode,
  33. source[0],
  34. source[1],
  35. destination[0],
  36. destination[1],
  37. distance,
  38. duration,
  39. json.dumps(step),
  40. ])
  41. with db.connect() as conn:
  42. conn.execute(stmt[:-1], *params)
  43. def directions_call(source, destination, mode):
  44. # doc: https://developers.google.com/maps/documentation/directions/intro
  45. url = 'https://maps.googleapis.com/maps/api/directions/json?origin=%s,%s&destination=%s,%s&key=%s&mode=%s&arrival_time=%i&alternatives=true'
  46. params = (
  47. source[0],
  48. source[1],
  49. destination[0],
  50. destination[1],
  51. config.GCLOUD_API_KEY,
  52. mode,
  53. config.WORKPLACE_TARGET_TIME,
  54. )
  55. #resp = requests.get(url % params)
  56. # print(resp.status_code)
  57. # print(json.dumps(resp.json()))
  58. resp_data = data.x
  59. _record_api_response(resp_data)
  60. def load_data():
  61. graph = {
  62. config.WORKPLACE: {}
  63. }
  64. with db.connect() as conn:
  65. rows = conn.select('select id, mode, dest_lat, dest_lon, src_lat, src_lon, duration from transit')
  66. for key, mode, src_lat, src_lon, dest_lat, dest_lon, duration in rows:
  67. loc = src_lat, src_lon
  68. if loc not in graph:
  69. graph[loc] = {}
  70. for lat, lon in graph:
  71. if 0 < geo_util.ggcd(lon, lat, src_lon, src_lat) < 200:
  72. k = 'w-' + _get_key(lon, lat, src_lon, src_lat)
  73. graph[loc][k] = {
  74. 'mode': TransitMethod.WALKING,
  75. 'dest': (lat, lon),
  76. 'duration': 0,
  77. }
  78. graph[lat,lon][k] = {
  79. 'mode': TransitMethod.WALKING,
  80. 'dest': loc,
  81. 'duration': 0,
  82. }
  83. graph[loc][key] = {
  84. 'mode': mode,
  85. 'dest': (dest_lat, dest_lon),
  86. 'duration': duration,
  87. }
  88. for k, v in graph.items():
  89. print((k,v))
  90. print(graph[config.WORKPLACE])
  91. # directions_call((53.388631,-6.071959), config.WORKPLACE, TransitMethod.TRANSIT)
  92. load_data()