diff --git a/lib/parse_query.py b/lib/parse_query.py index f086c01..6c45e56 100644 --- a/lib/parse_query.py +++ b/lib/parse_query.py @@ -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): """ diff --git a/lib/view/v2.py b/lib/view/v2.py index c2d0707..22a7518 100644 --- a/lib/view/v2.py +++ b/lib/view/v2.py @@ -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 = """ @@ -551,14 +555,14 @@ def main(query, parsed_query, data): - +
 {textual_information}
 
""".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: diff --git a/lib/wttr_srv.py b/lib/wttr_srv.py index c5c5825..2ae69c1 100644 --- a/lib/wttr_srv.py +++ b/lib/wttr_srv.py @@ -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