Fix 406 error from Overpass API by setting Accept: */* header
This commit is contained in:
@@ -86,11 +86,16 @@ def query_overpass(query):
|
|||||||
"""Send query to Overpass API and return JSON response, with retries."""
|
"""Send query to Overpass API and return JSON response, with retries."""
|
||||||
url = "https://overpass-api.de/api/interpreter"
|
url = "https://overpass-api.de/api/interpreter"
|
||||||
data = urllib.parse.urlencode({"data": query}).encode("utf-8")
|
data = urllib.parse.urlencode({"data": query}).encode("utf-8")
|
||||||
|
req = urllib.request.Request(
|
||||||
|
url,
|
||||||
|
data=data,
|
||||||
|
headers={"Accept": "*/*", "User-Agent": "TheVillagesImport/1.0"},
|
||||||
|
)
|
||||||
|
|
||||||
max_attempts = 3
|
max_attempts = 3
|
||||||
for attempt in range(1, max_attempts + 1):
|
for attempt in range(1, max_attempts + 1):
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen(url, data=data, timeout=300) as response:
|
with urllib.request.urlopen(req, timeout=300) as response:
|
||||||
return json.loads(response.read().decode("utf-8"))
|
return json.loads(response.read().decode("utf-8"))
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user