Spaces:
Sleeping
Sleeping
Update services/location_service.py
Browse files- services/location_service.py +23 -0
services/location_service.py
CHANGED
@@ -66,6 +66,28 @@ class LocationService:
|
|
66 |
Ensure the strictly that output is a valid JSON object containing strictly the above keys, without any explanations.
|
67 |
Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.
|
68 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
@staticmethod
|
71 |
def get_coordinates(data:dict) -> Coordinates | ErrorResponse:
|
@@ -73,6 +95,7 @@ class LocationService:
|
|
73 |
print(data)
|
74 |
try:
|
75 |
location = data.get('location')
|
|
|
76 |
except:
|
77 |
return Coordinates(latitude=None, longitude=None)
|
78 |
|
|
|
66 |
Ensure the strictly that output is a valid JSON object containing strictly the above keys, without any explanations.
|
67 |
Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.
|
68 |
"""
|
69 |
+
|
70 |
+
@staticmethod
|
71 |
+
def get_lat_lng(data:dict):
|
72 |
+
try:
|
73 |
+
location = data.get('location')
|
74 |
+
except:
|
75 |
+
return Coordinates(latitude=None, longitude=None)
|
76 |
+
url = f"https://maps.googleapis.com/maps/api/geocode/json?address={location}&key={os.getenv('GOOGLE_GEO_API_KEY')}"
|
77 |
+
try:
|
78 |
+
response = requests.get(url)
|
79 |
+
json_data = response.json()
|
80 |
+
lat = json_data.get('results',None)[0].get('geometry',None).get('location',None).get('lat')
|
81 |
+
long = json_data.get('results',None)[0].get('geometry',None).get('location',None).get('lng')
|
82 |
+
return Coordinates(
|
83 |
+
latitude=lat,
|
84 |
+
longitude=long
|
85 |
+
)
|
86 |
+
except Exception as e:
|
87 |
+
return Coordinates(latitude=None , longitude=None)
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
|
92 |
@staticmethod
|
93 |
def get_coordinates(data:dict) -> Coordinates | ErrorResponse:
|
|
|
95 |
print(data)
|
96 |
try:
|
97 |
location = data.get('location')
|
98 |
+
|
99 |
except:
|
100 |
return Coordinates(latitude=None, longitude=None)
|
101 |
|