Spaces:
Sleeping
Sleeping
Create travel.py
Browse files
travel.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from geopy.geocoders import Nominatim
|
| 3 |
+
|
| 4 |
+
def get_coordinates(location_name):
|
| 5 |
+
geolocator = Nominatim(user_agent="travelspotfinder")
|
| 6 |
+
try:
|
| 7 |
+
location = geolocator.geocode(location_name)
|
| 8 |
+
return location.latitude, location.longitude
|
| 9 |
+
except:
|
| 10 |
+
return None, None
|
| 11 |
+
|
| 12 |
+
def get_travel_spots(location_name):
|
| 13 |
+
lat, lon = get_coordinates(location_name)
|
| 14 |
+
if lat is None or lon is None:
|
| 15 |
+
return f"📍 Could not find coordinates for {location_name}."
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
# Using Wikipedia API as a simple public fallback
|
| 19 |
+
response = requests.get(
|
| 20 |
+
f"https://en.wikipedia.org/w/api.php",
|
| 21 |
+
params={
|
| 22 |
+
"action": "query",
|
| 23 |
+
"list": "geosearch",
|
| 24 |
+
"gsradius": 10000,
|
| 25 |
+
"gscoord": f"{lat}|{lon}",
|
| 26 |
+
"gslimit": 5,
|
| 27 |
+
"format": "json"
|
| 28 |
+
}
|
| 29 |
+
)
|
| 30 |
+
data = response.json()
|
| 31 |
+
places = data["query"]["geosearch"]
|
| 32 |
+
if not places:
|
| 33 |
+
raise Exception("No places found")
|
| 34 |
+
|
| 35 |
+
spots = [f"• {p['title']}" for p in places]
|
| 36 |
+
return f"🏞️ Tourist spots near {location_name}:\n" + "\n".join(spots)
|
| 37 |
+
|
| 38 |
+
except:
|
| 39 |
+
# Static fallback
|
| 40 |
+
static_spots = {
|
| 41 |
+
"pune": ["Shaniwar Wada", "Sinhagad Fort", "Aga Khan Palace", "Pataleshwar Caves"],
|
| 42 |
+
"ranikhet": ["Jhula Devi Temple", "Chaubatia Gardens", "Majkhali", "Bhalu Dam"]
|
| 43 |
+
}
|
| 44 |
+
city_key = location_name.lower()
|
| 45 |
+
if city_key in static_spots:
|
| 46 |
+
return f"🏞️ Tourist spots in {location_name}:\n" + "\n".join([f"• {spot}" for spot in static_spots[city_key]])
|
| 47 |
+
else:
|
| 48 |
+
return f"📍 No travel suggestions found for {location_name}."
|