File size: 1,762 Bytes
ed11779 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import wikipedia
import requests
class WikipediaHandler:
def get_landmarks_in_area(self, bounds):
"""Fetch landmarks within the given bounds using the Wikipedia API."""
url = "https://en.wikipedia.org/w/api.php"
# Default coordinates (New York City) in case bounds are None
default_lat, default_lon = 40.7128, -74.0060
# Use default coordinates if bounds are None or contain None values
north = bounds.get('north')
south = bounds.get('south')
east = bounds.get('east')
west = bounds.get('west')
if north is None or south is None or east is None or west is None:
center_lat, center_lon = default_lat, default_lon
else:
center_lat = (north + south) / 2
center_lon = (east + west) / 2
params = {
"action": "query",
"format": "json",
"list": "geosearch",
"gscoord": f"{center_lat}|{center_lon}",
"gsradius": "10000",
"gslimit": "50"
}
response = requests.get(url, params=params)
data = response.json()
landmarks = []
for place in data["query"]["geosearch"]:
try:
page = wikipedia.page(place["title"])
landmarks.append({
"title": place["title"],
"lat": place["lat"],
"lon": place["lon"],
"summary": page.summary,
"url": page.url
})
except wikipedia.exceptions.DisambiguationError:
continue
except wikipedia.exceptions.PageError:
continue
return landmarks
|