cc-py-commons 0.5.12__tar.gz → 0.5.14__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/PKG-INFO +1 -1
  2. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/geolocate.py +160 -132
  3. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons.egg-info/PKG-INFO +1 -1
  4. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/setup.py +1 -1
  5. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/README.md +0 -0
  6. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/__init__.py +0 -0
  7. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/bids/__init__.py +0 -0
  8. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/bids/bid.py +0 -0
  9. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/bids/bid_history.py +0 -0
  10. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/bids/bid_history_schema.py +0 -0
  11. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/bids/bid_schema.py +0 -0
  12. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/carriers/__init__.py +0 -0
  13. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/carriers/account_carrier_map.py +0 -0
  14. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/carriers/account_carrier_map_schema.py +0 -0
  15. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/carriers/carrier.py +0 -0
  16. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/carriers/carrier_schema.py +0 -0
  17. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/carriers/contact.py +0 -0
  18. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/carriers/contact_schema.py +0 -0
  19. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/config/__init__.py +0 -0
  20. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/config/c4_account_settings.py +0 -0
  21. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/config/env.py +0 -0
  22. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/config/mcleod_load_fields.py +0 -0
  23. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/config/pc_miler_config.py +0 -0
  24. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/db/__init__.py +0 -0
  25. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/db/connection.py +0 -0
  26. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/lanes/__init__.py +0 -0
  27. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/lanes/carrier_lane.py +0 -0
  28. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/lanes/carrier_lane_schema.py +0 -0
  29. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/lanes/lane.py +0 -0
  30. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/lanes/lane_schema.py +0 -0
  31. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/__init__.py +0 -0
  32. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/equipment.py +0 -0
  33. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/equipment_schema.py +0 -0
  34. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/equipment_types.py +0 -0
  35. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/freighthub_contact.py +0 -0
  36. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/freighthub_contact_schema.py +0 -0
  37. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/load.py +0 -0
  38. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/load_schema.py +0 -0
  39. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/load_status.py +0 -0
  40. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/location.py +0 -0
  41. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/location_schema.py +0 -0
  42. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/loads/map_load_response.py +0 -0
  43. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/quotes/__init__.py +0 -0
  44. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/quotes/quote.py +0 -0
  45. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/quotes/quote_schema.py +0 -0
  46. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/quotes/quote_status.py +0 -0
  47. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/rfp/__init__.py +0 -0
  48. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/rfp/create_table.py +0 -0
  49. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/rfp/rfp_status.py +0 -0
  50. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/rfp/truck_lane_search_mapper.py +0 -0
  51. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/schemas/__init__.py +0 -0
  52. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/schemas/camel_case_schema.py +0 -0
  53. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/__init__.py +0 -0
  54. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/analytics/__init__.py +0 -0
  55. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/analytics/analytics_event.py +0 -0
  56. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/booking_agent/__init__.py +0 -0
  57. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/booking_agent/accept_bid.py +0 -0
  58. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/booking_agent/cancel_bid.py +0 -0
  59. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/booking_agent/constants.py +0 -0
  60. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/booking_agent/counter_bid.py +0 -0
  61. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/booking_agent/decline_bid.py +0 -0
  62. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/booking_agent/get_bid_by_amazon_order_id.py +0 -0
  63. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/booking_agent/get_quote.py +0 -0
  64. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/booking_agent/update_bid.py +0 -0
  65. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/c4/__init__.py +0 -0
  66. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/c4/amazon_session_token.py +0 -0
  67. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/c4/constants.py +0 -0
  68. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/c4/get_account.py +0 -0
  69. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/c4/get_account_settings.py +0 -0
  70. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/c4/get_integration.py +0 -0
  71. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/c4/get_users_for_account.py +0 -0
  72. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/c4/refresh_amazon_token.py +0 -0
  73. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/carrier_hub/__init__.py +0 -0
  74. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/carrier_hub/get_carrier.py +0 -0
  75. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/carrier_hub/list_carriers.py +0 -0
  76. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/carrier_hub/list_contacts.py +0 -0
  77. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/carrier_hub/update_contact.py +0 -0
  78. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/freight_hub/__init__.py +0 -0
  79. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/freight_hub/post_freight_hub_load.py +0 -0
  80. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/__init__.py +0 -0
  81. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/create_account_folder.py +0 -0
  82. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/db/__init__.py +0 -0
  83. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/db/delete_table.py +0 -0
  84. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/get_client.py +0 -0
  85. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/get_green_screens_lane_price.py +0 -0
  86. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/get_import_stat.py +0 -0
  87. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/get_lane_price.py +0 -0
  88. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/get_lane_price_for_bulk.py +0 -0
  89. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/list_clients.py +0 -0
  90. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/post_import_stat.py +0 -0
  91. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/mercury/update_import_stat.py +0 -0
  92. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/slack/__init__.py +0 -0
  93. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/services/slack/send_slack_message.py +0 -0
  94. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sns/__init__.py +0 -0
  95. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sns/book_load_notification.py +0 -0
  96. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sns/booking_agent/__init__.py +0 -0
  97. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sns/booking_agent/bid_counter_failure_notification.py +0 -0
  98. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sns/booking_agent/constants.py +0 -0
  99. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sns/booking_agent/send_sns_notification.py +0 -0
  100. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sns/booking_assistant_flow_notification.py +0 -0
  101. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sns/import_movements_notification.py +0 -0
  102. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sns/parsed_carrier_notification.py +0 -0
  103. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sns/sns_service.py +0 -0
  104. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sqs/__init__.py +0 -0
  105. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sqs/booking_assistant_flow_event.py +0 -0
  106. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/sqs/sqs_service.py +0 -0
  107. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/tests/__init__.py +0 -0
  108. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/tests/test_case_conversion.py +0 -0
  109. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/tests/test_lambda_utils.py +0 -0
  110. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/tests/test_map_transaction.py +0 -0
  111. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/tests/test_temperature_utils.py +0 -0
  112. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/transactions/__init__.py +0 -0
  113. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/transactions/equipment_clas_schema.py +0 -0
  114. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/transactions/equipment_class.py +0 -0
  115. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/transactions/equipment_mapping.py +0 -0
  116. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/transactions/equipment_mapping_schema.py +0 -0
  117. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/transactions/map_transaction.py +0 -0
  118. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/transactions/transaction.py +0 -0
  119. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/transactions/transaction_schema.py +0 -0
  120. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/__init__.py +0 -0
  121. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/bulk_lane_price_result_utils.py +0 -0
  122. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/case_conversion.py +0 -0
  123. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/datadog_logger.py +0 -0
  124. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/datetimes.py +0 -0
  125. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/dimension_utils.py +0 -0
  126. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/geocode_location.py +0 -0
  127. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/get_delivery_date.py +0 -0
  128. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/json_logger.py +0 -0
  129. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/lambda_utils.py +0 -0
  130. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/local_file.py +0 -0
  131. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/logger.py +0 -0
  132. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/logger_v2.py +0 -0
  133. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/pc_miler_utils.py +0 -0
  134. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/price_utils.py +0 -0
  135. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/redis.py +0 -0
  136. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/s3_file.py +0 -0
  137. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons/utils/temperature_utils.py +0 -0
  138. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons.egg-info/SOURCES.txt +0 -0
  139. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons.egg-info/dependency_links.txt +0 -0
  140. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons.egg-info/requires.txt +0 -0
  141. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/cc_py_commons.egg-info/top_level.txt +0 -0
  142. {cc_py_commons-0.5.12 → cc_py_commons-0.5.14}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cc_py_commons
3
- Version: 0.5.12
3
+ Version: 0.5.14
4
4
  Summary: Common code for microservices
5
5
  Home-page: https://github.com/Cargo-Chief/py-commons
6
6
  Author: CargoChief
@@ -5,10 +5,10 @@ from cc_py_commons.config.env import app_config
5
5
  from cc_py_commons.utils.redis import location_db_conn, distance_db_conn, timezone_db_conn
6
6
  from ast import literal_eval
7
7
 
8
- def get_location(city, state, zipcode, logger):
9
- return get_location_with_country(city, state, zipcode, None, logger)
8
+ def get_location(city, state, zipcode, logger, reset_cache=False):
9
+ return get_location_with_country(city, state, zipcode, None, logger, reset_cache)
10
10
 
11
- def get_location_with_country(city, state, zipcode, country, logger):
11
+ def get_location_with_country(city, state, zipcode, country, logger, reset_cache=False):
12
12
  from_cache = False
13
13
  postcode = zipcode
14
14
 
@@ -16,14 +16,16 @@ def get_location_with_country(city, state, zipcode, country, logger):
16
16
  parts = zipcode.split('-')
17
17
  postcode = parts[0] if len(parts) > 0 else zipcode
18
18
 
19
- data = __get_location_from_cache(city, state, postcode, logger)
19
+ data = None
20
+ if not reset_cache:
21
+ data = __get_location_from_cache(city, state, postcode, logger)
20
22
 
21
23
  if data:
22
24
  if data.get('country') and (not postcode or postcode == data.get('postcode')):
23
25
  from_cache = True
24
26
  else:
25
27
  logger.debug(json.dumps({
26
- 'message': 'Found locatin in cache that was not a match',
28
+ 'message': 'Found location in cache that was not a match',
27
29
  'city': city,
28
30
  'state': state,
29
31
  'zipcode': zipcode,
@@ -48,6 +50,23 @@ def get_location_with_country(city, state, zipcode, country, logger):
48
50
  from_cache = False
49
51
  data = __get_google_location(city=None, state=None, zipcode=postcode, country=None, logger=logger)
50
52
 
53
+ if data and not data.get('postcode'):
54
+ matching_location = __get_matching_location_from_cache(city, state, data.get('lat'), data.get('lng'), logger)
55
+ if matching_location:
56
+ data['postcode'] = matching_location.get('postcode')
57
+ else:
58
+ gmaps = googlemaps.Client(key=app_config.GOOGLE_API_KEY)
59
+ response = None
60
+ if zipcode:
61
+ response = gmaps.geocode(zipcode)
62
+ elif data.get('lat') and data.get('lng'):
63
+ response = gmaps.reverse_geocode((data.get('lat'), data.get('lng')))
64
+ post_code_data = __parse_response(response, city)
65
+ data['postcode'] = post_code_data['postcode'] if post_code_data else None
66
+
67
+ if data.get('postcode'):
68
+ __cache_location(city, state, zipcode, data, logger)
69
+
51
70
  if data:
52
71
  if not from_cache:
53
72
  __cache_location(city, state, zipcode, data, logger)
@@ -126,6 +145,7 @@ def __get_google_distance(origin_latitude, origin_longitude, destination_latitud
126
145
  def __get_google_location(city, state, zipcode, country, logger):
127
146
  try:
128
147
  loc_str = ''
148
+ data = None
129
149
 
130
150
  if city:
131
151
  loc_str = city
@@ -148,123 +168,6 @@ def __get_google_location(city, state, zipcode, country, logger):
148
168
 
149
169
  loc_str = loc_str + country
150
170
 
151
- def __parse_response(response):
152
- if not response or len(response) == 0:
153
- return None
154
- '''
155
- Sample response of Google Maps API for address: Cincinati, OH, 45204
156
- {
157
- "address_components": [
158
- {
159
- "long_name": "45204",
160
- "short_name": "45204",
161
- "types": [
162
- "postal_code"
163
- ]
164
- },
165
- {
166
- "long_name": "Cincinnati",
167
- "short_name": "Cincinnati",
168
- "types": [
169
- "locality",
170
- "political"
171
- ]
172
- },
173
- {
174
- "long_name": "Hamilton County",
175
- "short_name": "Hamilton County",
176
- "types": [
177
- "administrative_area_level_2",
178
- "political"
179
- ]
180
- },
181
- {
182
- "long_name": "Ohio",
183
- "short_name": "OH",
184
- "types": [
185
- "administrative_area_level_1",
186
- "political"
187
- ]
188
- },
189
- {
190
- "long_name": "United States",
191
- "short_name": "US",
192
- "types": [
193
- "country",
194
- "political"
195
- ]
196
- }
197
- ],
198
- "formatted_address": "Cincinnati, OH 45204, USA",
199
- "geometry": {
200
- "bounds": {
201
- "northeast": {
202
- "lat": 39.1251898,
203
- "lng": -84.539683
204
- },
205
- "southwest": {
206
- "lat": 39.073286,
207
- "lng": -84.62870989999999
208
- }
209
- },
210
- "location": {
211
- "lat": 39.0930395,
212
- "lng": -84.56676650000001
213
- },
214
- "location_type": "APPROXIMATE",
215
- "viewport": {
216
- "northeast": {
217
- "lat": 39.1251898,
218
- "lng": -84.539683
219
- },
220
- "southwest": {
221
- "lat": 39.073286,
222
- "lng": -84.62870989999999
223
- }
224
- }
225
- },
226
- "place_id": "ChIJjYEP0hK2QYgRTJoYykoQeqw",
227
- "postcode_localities": [
228
- "Cincinnati",
229
- "Queen City Square"
230
- ],
231
- "types": [
232
- "postal_code"
233
- ]
234
- }
235
- '''
236
- components = response[0]['address_components']
237
- types = response[0]['types']
238
- location = response[0]['geometry']['location']
239
- postcode_localities = response[0].get('postcode_localities', None)
240
-
241
- data = { 'city': None, 'state': None, 'postcode': None }
242
- data['lat'] = location['lat']
243
- data['lng'] = location['lng']
244
-
245
- # when checking the response for a zipcode verify the city name is in the list of localities
246
- if 'postal_code' in types and postcode_localities and city:
247
- matches = [l for l in components if (
248
- 'postal_localities' in l.get('types') and
249
- (l.short_name.lower() == city.lower() or l.long_name.lower() == city.lower())
250
- )]
251
- city_present_in_postcode_localities = (city in postcode_localities)
252
-
253
- if not matches and not city_present_in_postcode_localities:
254
- return None
255
-
256
- for component in components:
257
- if 'locality' in component['types']:
258
- data['city'] = component['short_name']
259
- elif 'administrative_area_level_1' in component['types']:
260
- data['state'] = component['short_name']
261
- elif 'postcode' in component['types'] or 'postal_code' in component['types']:
262
- data['postcode'] = component['short_name']
263
- elif 'country' in component['types']:
264
- data['country'] = component['short_name']
265
-
266
- return data
267
-
268
171
  gmaps = googlemaps.Client(key=app_config.GOOGLE_API_KEY)
269
172
  response = gmaps.geocode(loc_str)
270
173
 
@@ -280,16 +183,7 @@ def __get_google_location(city, state, zipcode, country, logger):
280
183
  logger.warning(f"Cannot select a single address from multiple for {loc_str}.")
281
184
  return None
282
185
  response = [selected_address]
283
-
284
- data = __parse_response(response)
285
-
286
- # For some locals like neighborhood google does not return postal_code
287
- ## if zipcode is present run it only and verify against city
288
- if data and not data['postcode'] and zipcode:
289
- response = gmaps.geocode(zipcode)
290
- post_code_data = __parse_response(response)
291
- data['postcode'] = post_code_data['postcode'] if post_code_data else None
292
-
186
+ data = __parse_response(response, city)
293
187
  return data
294
188
  except googlemaps.exceptions.Timeout as e:
295
189
  msg = "geolocate.__get_google_location - Location lookup timed out {0}, {1}, {2}: {3}".format(city, state, zipcode, e)
@@ -308,6 +202,123 @@ def __get_google_location(city, state, zipcode, country, logger):
308
202
 
309
203
  return None
310
204
 
205
+ def __parse_response(response, city):
206
+ if not response or len(response) == 0:
207
+ return None
208
+ '''
209
+ Sample response of Google Maps API for address: Cincinati, OH, 45204
210
+ {
211
+ "address_components": [
212
+ {
213
+ "long_name": "45204",
214
+ "short_name": "45204",
215
+ "types": [
216
+ "postal_code"
217
+ ]
218
+ },
219
+ {
220
+ "long_name": "Cincinnati",
221
+ "short_name": "Cincinnati",
222
+ "types": [
223
+ "locality",
224
+ "political"
225
+ ]
226
+ },
227
+ {
228
+ "long_name": "Hamilton County",
229
+ "short_name": "Hamilton County",
230
+ "types": [
231
+ "administrative_area_level_2",
232
+ "political"
233
+ ]
234
+ },
235
+ {
236
+ "long_name": "Ohio",
237
+ "short_name": "OH",
238
+ "types": [
239
+ "administrative_area_level_1",
240
+ "political"
241
+ ]
242
+ },
243
+ {
244
+ "long_name": "United States",
245
+ "short_name": "US",
246
+ "types": [
247
+ "country",
248
+ "political"
249
+ ]
250
+ }
251
+ ],
252
+ "formatted_address": "Cincinnati, OH 45204, USA",
253
+ "geometry": {
254
+ "bounds": {
255
+ "northeast": {
256
+ "lat": 39.1251898,
257
+ "lng": -84.539683
258
+ },
259
+ "southwest": {
260
+ "lat": 39.073286,
261
+ "lng": -84.62870989999999
262
+ }
263
+ },
264
+ "location": {
265
+ "lat": 39.0930395,
266
+ "lng": -84.56676650000001
267
+ },
268
+ "location_type": "APPROXIMATE",
269
+ "viewport": {
270
+ "northeast": {
271
+ "lat": 39.1251898,
272
+ "lng": -84.539683
273
+ },
274
+ "southwest": {
275
+ "lat": 39.073286,
276
+ "lng": -84.62870989999999
277
+ }
278
+ }
279
+ },
280
+ "place_id": "ChIJjYEP0hK2QYgRTJoYykoQeqw",
281
+ "postcode_localities": [
282
+ "Cincinnati",
283
+ "Queen City Square"
284
+ ],
285
+ "types": [
286
+ "postal_code"
287
+ ]
288
+ }
289
+ '''
290
+ components = response[0]['address_components']
291
+ types = response[0]['types']
292
+ location = response[0]['geometry']['location']
293
+ postcode_localities = response[0].get('postcode_localities', None)
294
+
295
+ data = {'city': None, 'state': None, 'postcode': None}
296
+ data['lat'] = location['lat']
297
+ data['lng'] = location['lng']
298
+
299
+ # when checking the response for a zipcode verify the city name is in the list of localities
300
+ if 'postal_code' in types and postcode_localities and city:
301
+ matches = [l for l in components if (
302
+ 'postal_localities' in l.get('types') and
303
+ (l.short_name.lower() == city.lower() or l.long_name.lower() == city.lower())
304
+ )]
305
+ city_present_in_postcode_localities = (city in postcode_localities)
306
+
307
+ if not matches and not city_present_in_postcode_localities:
308
+ return None
309
+
310
+ for component in components:
311
+ if 'locality' in component['types']:
312
+ data['city'] = component['short_name']
313
+ elif 'administrative_area_level_1' in component['types']:
314
+ data['state'] = component['short_name']
315
+ elif 'postcode' in component['types'] or 'postal_code' in component['types']:
316
+ data['postcode'] = component['short_name']
317
+ elif 'country' in component['types']:
318
+ data['country'] = component['short_name']
319
+
320
+ return data
321
+
311
322
  def __cache_location(city, state, zipcode, location_data, logger):
312
323
  location_string = __get_location_string(city, state, zipcode)
313
324
  location_db_conn.set(location_string, str(location_data))
@@ -398,3 +409,20 @@ def __select_address(city, state, address_list):
398
409
  and parsed_city_and_state['state'].strip().lower() == state.strip().lower():
399
410
  return parsed_city_and_state['address']
400
411
  return None
412
+
413
+ def __get_matching_location_from_cache(city, state, lat, lng, logger):
414
+ matching_location = None
415
+ try:
416
+ if city and state and lat and lng:
417
+ key = __get_location_string(city, state, None)
418
+ pattern = f'{key}*'
419
+ matching_keys = location_db_conn.keys(pattern)
420
+ if matching_keys:
421
+ for matching_key in matching_keys:
422
+ location = literal_eval(location_db_conn.get(matching_key).decode('utf-8'))
423
+ if location.get('lat') == lat and location.get('lng') == lng and location.get('postcode'):
424
+ matching_location = location
425
+ break
426
+ except Exception as e:
427
+ logger.warn("geolocate.__get_matching_location_from_cache: Failed: ", e)
428
+ return matching_location
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cc-py-commons
3
- Version: 0.5.12
3
+ Version: 0.5.14
4
4
  Summary: Common code for microservices
5
5
  Home-page: https://github.com/Cargo-Chief/py-commons
6
6
  Author: CargoChief
@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
3
3
  setup(
4
4
  name='cc_py_commons',
5
5
  description='Common code for microservices',
6
- version='0.5.12',
6
+ version='0.5.14',
7
7
  url='https://github.com/Cargo-Chief/py-commons',
8
8
  author='CargoChief',
9
9
  author_email='engineering@cargochief.com',
File without changes
File without changes