Files
osmsearch/websearch.py
2020-04-20 09:13:59 +02:00

99 lines
2.6 KiB
Python
Executable File

#!/usr/bin/python
"""
CGI interface to osmsearch.py
"""
__author__ = "fordprefect"
__date__ = "2020-03-26"
__version__ = "0.1"
print("Content-type: text/html\n") # makes sure the http connection does not die - do nothing before this!
import cgi
import pathvalidate
import osmsearch
import datetime
import random
import os
import sys
assert sys.version_info >= (3, 6), "At least Python 3.8 required due to fStrings. Replace all f\"\" and comment this line to enable running in lower versions (but really, you should just update your Python version…)."
import base64
#####################################
######## configuration
#####################################
# default set of arguments
args = {
"q": "Camping",
"format": "json",
"limit": 50,
"use_boundingbox": False,
"boundingbox": [],
"ignore_ids": [],
"maxsearchnums": 10,
"url": "https://nominatim.openstreetmap.org/search/",
"output": "return",
}
immutable_args = ["format", "url", "ouput"]
outputfolder = "downloads"
DEBUG = False
#####################################
######## end of configuration
#####################################
# get cgi args sanitize, and update default args
rawargs = cgi.FieldStorage()
cgi_args = {i: rawargs.getvalue(i) for i in rawargs}
del(rawargs)
parsed_args = {}
try:
cgi_args["q"] = pathvalidate.sanitize_filename(cgi_args["query"])
except KeyError:
if DEBUG:
# local testing
cgi_args["q"] = "Camping"
else:
# we don't need to exit gracefully, whoever is arriving here has fiddled with the request manually
exit()
for arg in cgi_args:
if arg not in immutable_args and arg in args:
parsed_args[arg] = cgi_args[arg]
if DEBUG: print("Parsed argument dict: ", parsed_args)
args.update(cgi_args)
searchresult = osmsearch.OSMSearch(args)
gpxcontent = searchresult.gpxfile.to_xml()
gpxcontent_base64 = base64.b64encode(gpxcontent.encode("utf-8")).decode("utf-8")
print("""
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="robots" content="noindex,nofollow" />
<title>OSM search to GPX</title>
</head>
<body class=body>
<div class=head>
<h3>Export OSM search to GPX file</h3>
</div>
<div class=box>
<h4>Success</h4>
""" + f"""
<a href="data:text/xml;base64,{gpxcontent_base64}"
""" + """
download="search.gpx">Download GPX file here</a>
<br><br>
<a href=index.html>Return to search page</a>
</div>
<div class=foot>
OSMsearchToGPX - ein Service von <a href=https://dukun.de>dukun.de</a>
</div>
</body>
</html>
""")