|
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_lat, default_lon = 40.7128, -74.0060 |
|
|
|
|
|
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 |
|
|