Add new function: _normalize_query_string

pull/1062/head
Igor Chubin 1 month ago
parent 5a554e819e
commit 19d82d6180

@ -25,6 +25,7 @@ import os
import time import time
import json import json
import hashlib import hashlib
import re
import requests import requests
import cyrtranslit import cyrtranslit
@ -41,7 +42,6 @@ import proxy_log
import globals import globals
from globals import ( from globals import (
PROXY_CACHEDIR, PROXY_CACHEDIR,
PROXY_HOST,
PROXY_PORT, PROXY_PORT,
USE_METNO, USE_METNO,
USER_AGENT, USER_AGENT,
@ -330,6 +330,29 @@ def _make_query(path, query_string):
return content, headers 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("/<path:path>") @APP.route("/<path:path>")
def proxy(path): def proxy(path):
""" """
@ -338,6 +361,7 @@ def proxy(path):
lang = request.args.get("lang", "en") lang = request.args.get("lang", "en")
query_string = request.query_string.decode("utf-8") 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("sr-lat", "sr")
query_string = query_string.replace("lang=None", "lang=en") query_string = query_string.replace("lang=None", "lang=en")
content = "" content = ""

Loading…
Cancel
Save