v2+png: new experimental query encoding

v2
Igor Chubin 5 years ago
parent 67d4f9bfd1
commit 0d5f6c298c

@ -1,4 +1,35 @@
import re
import json
import zlib
import base64
def serialize(parsed_query):
return base64.b64encode(
zlib.compress(
json.dumps(parsed_query).encode("utf-8")),
altchars=b"-_").decode("utf-8")
def deserialize(url):
string = url[2:]
extension = None
if "." in string:
string, extension = string.split(".", 1)
try:
result = json.loads(
zlib.decompress(
base64.b64decode(string, altchars=b"-_")).decode("utf-8"))
except zlib.error:
return None
if extension == "png":
result["png_filename"] = url
result["html_output"] = False
return result
def metric_or_imperial(query, lang, us_ip=False):
"""

@ -42,6 +42,7 @@ from babel.dates import format_datetime
from globals import WWO_KEY
import constants
import translations
import parse_query
from . import line as wttr_line
if not sys.version_info >= (3, 0):
@ -543,6 +544,9 @@ def main(query, parsed_query, data):
else:
data_parsed = data
parsed_query["text"] = "no"
filename = "b_" + parse_query.serialize(parsed_query) + ".png"
if html_output:
output = """
<html>
@ -551,14 +555,14 @@ def main(query, parsed_query, data):
<link rel="stylesheet" type="text/css" href="/files/style.css" />
</head>
<body>
<img src="/{orig_location}_view=v2_text=no.png" width="592" height="532"/>
<img src="/{filename}" width="592" height="532"/>
<pre>
{textual_information}
</pre>
</body>
</html>
""".format(
orig_location=parsed_query["orig_location"],
filename=filename, orig_location=parsed_query["orig_location"],
textual_information=textual_information(
data_parsed, geo_data, parsed_query, html_output=True))
else:

@ -247,6 +247,12 @@ def parse_request(location, request, query, fast_mode=False):
Return: dictionary with parsed parameters
"""
if location.startswith("b_"):
result = parse_query.deserialize(location)
result["request_url"] = request.url
if result:
return result
png_filename = None
if location is not None and location.lower().endswith(".png"):
png_filename = location

Loading…
Cancel
Save