From 19d82d6180bb0af887815605921449305aa5751f Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Mon, 3 Feb 2025 14:16:29 +0100 Subject: [PATCH] Add new function: _normalize_query_string --- bin/proxy.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/bin/proxy.py b/bin/proxy.py index 552a88c..c37e1ce 100755 --- a/bin/proxy.py +++ b/bin/proxy.py @@ -25,6 +25,7 @@ import os import time import json import hashlib +import re import requests import cyrtranslit @@ -41,7 +42,6 @@ import proxy_log import globals from globals import ( PROXY_CACHEDIR, - PROXY_HOST, PROXY_PORT, USE_METNO, USER_AGENT, @@ -330,6 +330,29 @@ def _make_query(path, query_string): return content, headers +def _normalize_query_string(query_string): + # Normalized query string has the following fixes: + # 1. Uses , for the coordinates separation + # 2. Limits number of digits after . + + coord_match = re.search(r"q=[^&]*", query_string) + coords_str = coord_match.group(0) + coords = re.findall(r"[-0-9.]+", coords_str) + if len(coords) != 2: + return query_string + + lat = str(round(float(coords[0]), 2)) + lng = str(round(float(coords[1]), 2)) + query_string = re.sub(r"q=[^&]*", "q=" + lat + "," + lng, query_string) + print(query_string) + + # nqs = query_string.replace("%2C", ",") + # lat, lng = nqs.split(",", 1) + # nqs = f"{lat:.2f},{lng:.2f}" + + return query_string + + @APP.route("/") def proxy(path): """ @@ -338,6 +361,7 @@ def proxy(path): lang = request.args.get("lang", "en") query_string = request.query_string.decode("utf-8") + query_string = _normalize_query_string(query_string) query_string = query_string.replace("sr-lat", "sr") query_string = query_string.replace("lang=None", "lang=en") content = ""