Files
videoreg.ru/scripts/rebuild-v2/legacy-routes.py
T

62 lines
2.9 KiB
Python

from pathlib import Path
import json
P=Path(__file__).resolve().parents[2]
routes={
'/index.shtml':'/system',
'/moves.htm':'/demo.shtml#section-3',
'/shop/index.php?categoryID=78':'/catalog#section-1',
'/shop/index.php?categoryID=79':'/catalog#section-0',
'/shop/index.php?categoryID=80':'/catalog#section-3',
'/shop/index.php?productID=132':'/equipment/svk002-lqcd',
'/shop/index.php?productID=133':'/equipment/svk002-gps',
'/shop/index.php?productID=137':'/equipment/svk004',
'/forum/index.php':'/community',
'/forum/index.php?c=2':'/community',
'/forum/viewtopic.php?t=184':'/community/flash-start',
'/forum/viewtopic.php?t=188':'/community/usb-extension',
'/forum/viewtopic.php?t=183&view=next':'/community/flash-start',
'/forum/viewtopic.php?t=8&view=next':'/community/usb-extension',
'/forum/viewtopic.php?p=220':'/community/flash-start#section-0',
'/forum/viewtopic.php?p=221':'/community/flash-start#section-1',
'/forum/viewtopic.php?p=223':'/community/flash-start#section-2',
'/forum/viewtopic.php?p=226':'/community/usb-extension#section-0',
'/forum/viewtopic.php?p=227':'/community/usb-extension#section-1',
}
(P/'deploy/legacy-routes.json').write_text(json.dumps(routes,ensure_ascii=False,indent=2)+'\n')
from urllib.parse import urlsplit, parse_qsl, urlencode
import re
def exact_legacy_pattern(original):
"""One raw-query pattern shared by nginx and the Python preview.
Only proved presentation defaults and a session token may be ignored.
Pagination, search, ordering and unknown query values must not collapse.
"""
parsed = urlsplit(original)
required = [re.escape(urlencode([(k, v)]))
for k, v in parse_qsl(parsed.query, keep_blank_values=True)]
allowed = list(required)
if parsed.path.startswith('/forum/'):
allowed += ['sid=[^&]*', 'start=0', 'postdays=0', 'postorder=asc', 'highlight=']
checks = ''.join(r'(?=(?:[^&]*&)*' + pair + r'(?:&|$))' for pair in required)
if allowed:
pair = '(?:' + '|'.join(allowed) + ')'
query = pair + '(?:&' + pair + ')*'
if not required:
query = '(?:' + query + ')?'
else:
query = ''
return '^' + re.escape(parsed.path) + r'\|' + checks + query + '$'
matchers = [dict(original=src, destination=dst, pattern=exact_legacy_pattern(src))
for src, dst in routes.items()]
(P/'deploy/legacy-matchers.json').write_text(json.dumps(matchers, ensure_ascii=False, indent=2)+'\n')
lines = ['# Generated by scripts/rebuild-v2/legacy-routes.py; exact query meaning only.',
'map "$uri|$args" $videoreg_legacy {', ' default "";']
for item in matchers:
# map's first argument remains a regex token after nginx parses quotes.
pattern = item['pattern'].replace('\\', '\\\\').replace('"', '\\"')
lines.append(' "~' + pattern + '" "' + item['destination'] + '";')
lines.append('}')
(P/'deploy/legacy-map.conf').write_text('\n'.join(lines)+'\n')