mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-17 14:10:21 -04:00
💾
This commit is contained in:
parent
7cb7a479f6
commit
69bb4175f1
124 changed files with 20 additions and 15 deletions
35
web2_projects/fastapi-location-app/api/methods.py
Normal file
35
web2_projects/fastapi-location-app/api/methods.py
Normal 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
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue