improved web interface, JS download option missing, no storage of files on host system necessary anymore
This commit is contained in:
102
websearch.py
102
websearch.py
@@ -11,6 +11,9 @@ print("Content-type: text/html\n") # makes sure the http connection does not die
|
||||
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…)."
|
||||
|
||||
@@ -25,37 +28,106 @@ args = {
|
||||
"limit": 50,
|
||||
"use_boundingbox": False,
|
||||
"boundingbox": [],
|
||||
"outputfilename": None,
|
||||
"ignore_ids": [],
|
||||
"maxsearchnums": 10,
|
||||
"url": "https://nominatim.openstreetmap.org/search/",
|
||||
"output": "return",
|
||||
}
|
||||
immutable_args = ["format", "outputfilename", "url",]
|
||||
immutable_args = ["format", "url", "ouput"]
|
||||
outputfolder = "downloads"
|
||||
DEBUG = False
|
||||
|
||||
#####################################
|
||||
######## end of configuration
|
||||
#####################################
|
||||
|
||||
def get_arguments():
|
||||
"""Wrapper for cgi-environment.
|
||||
Takes no options, returns dict of arguments
|
||||
"""
|
||||
args = cgi.FieldStorage()
|
||||
return {i: args.getvalue(i) for i in args}
|
||||
|
||||
# get cgi args and sanitize
|
||||
cgi_args = get_arguments()
|
||||
# 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:
|
||||
# no key called "query" found, abort
|
||||
exit()
|
||||
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]
|
||||
print(parsed_args, " parsed")
|
||||
|
||||
if DEBUG: print("Parsed argument dict: ", parsed_args)
|
||||
args.update(cgi_args)
|
||||
|
||||
#status = osmsearch.conduct_search(args)
|
||||
# generate output file name from query
|
||||
# outputfolder/date-randomnumber.gpx
|
||||
if not os.path.isdir(outputfolder):
|
||||
os.makedirs(outputfolder)
|
||||
outputfilename = f"{outputfolder}/{str(datetime.date.today())}-{str(random.random()).replace('0.', '')}.gpx"
|
||||
args["outputfilename"] = outputfilename
|
||||
|
||||
searchresult = osmsearch.OSMSearch(args)
|
||||
gpxcontent = searchresult.gpxfile.to_xml()
|
||||
gpxcontent_escaped = searchresult.gpxfile.to_xml().replace("\\", "\\\\").replace("'", "\\'").replace('"', '\\"')
|
||||
|
||||
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>
|
||||
<script type='text/javascript'>
|
||||
function download_file(filename, text) {
|
||||
var element = document.createElement('a');
|
||||
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
|
||||
element.setAttribute('download', filename);
|
||||
|
||||
element.style.display = 'none';
|
||||
document.body.appendChild(element);
|
||||
|
||||
element.click();
|
||||
|
||||
document.body.removeChild(element);
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body class=body>
|
||||
<div class=head>
|
||||
<h3>Export OSM search to GPX file</h3>
|
||||
</div>
|
||||
<div class=box>
|
||||
<h4>Success</h4>
|
||||
Copy file content from here:
|
||||
<br>
|
||||
<textarea rows=10 cols=80 style="font-family: monospace;">"""
|
||||
+ gpxcontent +
|
||||
"""</textarea>
|
||||
<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>
|
||||
""")
|
||||
# TODO download button does not work this way!
|
||||
#"""</textarea>
|
||||
#<br>
|
||||
#or <button onclick="download_file('OSMsearch.gpx', '{gpxcontent}')">dowload here</button> (requires enabled JavaScript).
|
||||
#
|
||||
#<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>
|
||||
#""".format(gpxcontent=gpxcontent_escaped))
|
||||
|
||||
Reference in New Issue
Block a user