mirror of
https://github.com/chubin/wttr.in
synced 2025-01-26 02:34:59 +00:00
v2+png: new experimental query encoding
This commit is contained in:
parent
67d4f9bfd1
commit
0d5f6c298c
3 changed files with 43 additions and 2 deletions
|
@ -1,4 +1,35 @@
|
||||||
import re
|
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):
|
def metric_or_imperial(query, lang, us_ip=False):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -42,6 +42,7 @@ from babel.dates import format_datetime
|
||||||
from globals import WWO_KEY
|
from globals import WWO_KEY
|
||||||
import constants
|
import constants
|
||||||
import translations
|
import translations
|
||||||
|
import parse_query
|
||||||
from . import line as wttr_line
|
from . import line as wttr_line
|
||||||
|
|
||||||
if not sys.version_info >= (3, 0):
|
if not sys.version_info >= (3, 0):
|
||||||
|
@ -543,6 +544,9 @@ def main(query, parsed_query, data):
|
||||||
else:
|
else:
|
||||||
data_parsed = data
|
data_parsed = data
|
||||||
|
|
||||||
|
parsed_query["text"] = "no"
|
||||||
|
filename = "b_" + parse_query.serialize(parsed_query) + ".png"
|
||||||
|
|
||||||
if html_output:
|
if html_output:
|
||||||
output = """
|
output = """
|
||||||
<html>
|
<html>
|
||||||
|
@ -551,14 +555,14 @@ def main(query, parsed_query, data):
|
||||||
<link rel="stylesheet" type="text/css" href="/files/style.css" />
|
<link rel="stylesheet" type="text/css" href="/files/style.css" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<img src="/{orig_location}_view=v2_text=no.png" width="592" height="532"/>
|
<img src="/{filename}" width="592" height="532"/>
|
||||||
<pre>
|
<pre>
|
||||||
{textual_information}
|
{textual_information}
|
||||||
</pre>
|
</pre>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
""".format(
|
""".format(
|
||||||
orig_location=parsed_query["orig_location"],
|
filename=filename, orig_location=parsed_query["orig_location"],
|
||||||
textual_information=textual_information(
|
textual_information=textual_information(
|
||||||
data_parsed, geo_data, parsed_query, html_output=True))
|
data_parsed, geo_data, parsed_query, html_output=True))
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -247,6 +247,12 @@ def parse_request(location, request, query, fast_mode=False):
|
||||||
Return: dictionary with parsed parameters
|
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
|
png_filename = None
|
||||||
if location is not None and location.lower().endswith(".png"):
|
if location is not None and location.lower().endswith(".png"):
|
||||||
png_filename = location
|
png_filename = location
|
||||||
|
|
Loading…
Reference in a new issue