Improved zipcode parsing.
This commit is contained in:
@@ -255,30 +255,45 @@ geocode_location() {
|
||||
local userAgent="stormbot-weather/1.0"
|
||||
local response
|
||||
|
||||
# URL encode the query (replace spaces with +)
|
||||
local encodedQuery="${query// /+}"
|
||||
|
||||
# Check if query contains common US indicators (case-insensitive)
|
||||
local countryCode=""
|
||||
local queryLower="${query,,}"
|
||||
local isUsZipCode=false
|
||||
|
||||
# Common US state names and abbreviations
|
||||
local usStates="alabama|alaska|arizona|arkansas|california|colorado|connecticut|delaware|florida|georgia|hawaii|idaho|illinois|indiana|iowa|kansas|kentucky|louisiana|maine|maryland|massachusetts|michigan|minnesota|mississippi|missouri|montana|nebraska|nevada|new hampshire|new jersey|new mexico|new york|north carolina|north dakota|ohio|oklahoma|oregon|pennsylvania|rhode island|south carolina|south dakota|tennessee|texas|utah|vermont|virginia|washington|west virginia|wisconsin|wyoming"
|
||||
|
||||
# Check if this is a US ZIP code query
|
||||
if [[ "$query" =~ ^[0-9]{5}([[:space:]]|$) ]]; then
|
||||
isUsZipCode=true
|
||||
fi
|
||||
|
||||
if [[ "$queryLower" =~ (usa|united states) ]] || \
|
||||
[[ "$query" =~ [[:space:]][A-Z]{2}$ ]] || \
|
||||
[[ "$queryLower" =~ [[:space:]](${usStates})$ ]] || \
|
||||
[[ "$query" =~ ^[0-9]{5}([[:space:]]|$) ]]; then
|
||||
# Query mentions USA, ends with state abbreviation, ends with state name, or starts with 5-digit zip
|
||||
[[ "$isUsZipCode" == true ]]; then
|
||||
# Query mentions USA, ends with state abbreviation, ends with state name, or is a zip code
|
||||
countryCode="&countrycodes=us"
|
||||
fi
|
||||
|
||||
# Build the query URL
|
||||
local apiUrl
|
||||
if [[ "$isUsZipCode" == true ]]; then
|
||||
# For ZIP codes, use postalcode parameter for better accuracy
|
||||
local zipCode="${query%% *}" # Extract just the ZIP code (remove any trailing text)
|
||||
apiUrl="${url}?postalcode=${zipCode}&country=us&format=json&limit=1&addressdetails=1"
|
||||
else
|
||||
# URL encode the query (replace spaces with +)
|
||||
local encodedQuery="${query// /+}"
|
||||
apiUrl="${url}?q=${encodedQuery}&format=json&limit=1&addressdetails=1${countryCode}"
|
||||
fi
|
||||
|
||||
# Enforce rate limit before making API request
|
||||
rate_limit_nominatim
|
||||
|
||||
response=$(curl -s --connect-timeout 5 --max-time 10 \
|
||||
-H "User-Agent: ${userAgent}" \
|
||||
"${url}?q=${encodedQuery}&format=json&limit=1&addressdetails=1${countryCode}")
|
||||
"${apiUrl}")
|
||||
|
||||
if [[ -z "$response" || "$response" == "[]" ]]; then
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user