Add Location API project on FastAPI deplpyed in Vercel

This commit is contained in:
bt3gl 2022-03-26 11:13:24 +04:00
parent 188195b7a3
commit 896978f1ab
12 changed files with 134 additions and 0 deletions

View file

@ -0,0 +1,35 @@
import pytz
import datetime
from geopy.geocoders import Nominatim
from timezonefinder import TimezoneFinder
def _find_timezone(lat, lon) -> int:
obj = TimezoneFinder()
tzone = obj.timezone_at(lng=lon, lat=lat)
timezone = datetime.datetime.now(pytz.timezone(tzone)).strftime('%z')
try:
timezone = int(timezone[:-2])
except:
print('Error formatting timezone {}'.format(timezone))
return timezone
def get_location(data) -> dict:
try:
city = data['city']
except:
print('Data is ill-formatted: {}'.format(data))
geolocator = Nominatim(user_agent="$choices")
location = geolocator.geocode(city)
data = {
'lat': location.latitude,
'lon': location.longitude,
'tzone': _find_timezone(location.latitude, location.longitude)
}
return data