-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo_client_service
executable file
·98 lines (81 loc) · 3.71 KB
/
geo_client_service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
#
# Author: Siddharth Srivatsa <[email protected]>, 1/2018
import logging
import argparse
import os
# Use Sentry to catch errors
import sentry_sdk
sentry_sdk.init("https://[email protected]/1520343")
# Add a logger to log to file
if not os.path.exists("log/"):
os.makedirs("log/")
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M', filename='log/carographer.log')
# Set up console logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
log_handler = logging.StreamHandler()
log_handler.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
logger.addHandler(log_handler)
from cartographer.client import CartographerClient
from cartographer.client_iface import CartographerIface
from cartographer.geo_point import GeoPoint
def parse_command_line():
""" Define the command line arguments """
parser = argparse.ArgumentParser(description='Geocoding Proxy Service.')
parser.add_argument('--google_query', '-g', action='store_true', help='Make a request to the Google Geo Coding API, no args')
parser.add_argument('--here_query', '-r', action='store_true', help='Make a request to the HERE Geo Coding API, no args')
parser.add_argument('--custom_query', '-c', action='store_true', help='Make a request to a custom Geo Coding API, no args')
args = parser.parse_args()
av = vars(args)
# print help if multiple command types are given
cmd_list = ['google_query', 'here_query', 'custom_query']
if reduce(lambda x,y: x + (1 if av[y] else 0), cmd_list, 0) != 1:
parser.print_help()
raise SystemExit
return av
def run():
av = parse_command_line() # Get command line options
client = CartographerClient()
iface = CartographerIface(client)
if av['google_query']:
api_cred = raw_input('Please enter Google API key: ')
iface.set_creds(google_api_cred=api_cred)
geo_data = iface.use_google_geoCoding()
if geo_data is None: # Try secondary geo coding service, if first fails
logger.warn("Google API returned None, trying here API")
app_id = raw_input('Please enter Here app id: ')
app_code = raw_input('Please enter Here app code: ')
iface.set_creds(here_creds=[app_id, app_code])
geo_data = iface.use_here_geoCoding()
if geo_data is None:
logger.warn('Both API returned None')
return
geo_data.output_values()
if av['here_query']:
app_id = raw_input('Please enter Here app id: ')
app_code = raw_input('Please enter Here app code: ')
iface.set_creds(here_creds=[app_id, app_code])
geo_data = iface.use_here_geoCoding()
if geo_data is None: # Try secondary geo coding service, if first fails
logger.warn("Here API returned None, trying Google API")
api_cred = raw_input('Please enter Google API key: ')
iface.set_creds(google_api_cred=api_cred)
geo_data = iface.use_google_geoCoding()
if geo_data is None:
logger.warn('Both API returned None')
return
geo_data.output_values()
if av['custom_query']:
geo_data = iface.use_custom_geoCoding()
if geo_data is None: # Try secondary geo coding service, if first fails
logger.warn("Custom API returned None, trying Google API")
geo_data = iface.use_google_geoCoding()
if geo_data is None:
logger.warn('Both API returned None')
return
geo_data.output_values()
print "Exiting!"
if __name__ == '__main__':
run()