From 4831aa56282dd387d08184e0d9285a4dc6d046e4 Mon Sep 17 00:00:00 2001 From: Hay1tsme Date: Sun, 26 Nov 2023 01:41:25 -0500 Subject: [PATCH] diva: add encoder helper methods --- titles/diva/handlers/base.py | 101 ++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/titles/diva/handlers/base.py b/titles/diva/handlers/base.py index c672b73..f57825b 100644 --- a/titles/diva/handlers/base.py +++ b/titles/diva/handlers/base.py @@ -1,6 +1,9 @@ from urllib import parse +from urllib.parse import quote from datetime import datetime -from typing import Union, Dict +from typing import Union, Dict, List, Any + +from ..const import DivaConstants def lazy_http_form_parse(src: Union[str, bytes]) -> Dict[bytes, bytes]: out = {} @@ -184,4 +187,98 @@ class SpendCreditResponse(BaseResponse): self.vcld_pts = 0 self.lv_str = "" self.lv_efct_id = 0 - self.lv_plt_id = 0 \ No newline at end of file + self.lv_plt_id = 0 + +def encode_int(key: str, val: Union[int, None] = None) -> str: + if type(val) != int: + val = -1 + + return f"{key}={val}" + +def encode_str(key: str, val: Union[str, None] = None, urlencode_val: bool = False) -> str: + if type(val) != str: + val = "xxx" + + if urlencode_val: + val = quote(val) + + return f"{key}={val}" + +def encode_date(key: str, val: Union[datetime, None], urlencode_val: bool = False, fmt: str = DivaConstants.LUT_TIME_FMT) -> str: + if type(val) != datetime: + val = datetime.now().astimezone() + + val = val.replace(microsecond=0) + + dt_fmt = val.strftime(fmt) + if urlencode_val: + dt_fmt = quote(dt_fmt) + + return f"{key}={dt_fmt}" + +def encode_list_int(key: str, val: Union[List[int], None], urlencode_final_val: bool = False) -> str: + if not val: + return f"{key}=" + + for x in range(len(val)): + if type(val[x]) != int: + val[x] = -1 + + all_vals = ",".join([str(x) for x in val]) + if urlencode_final_val: + all_vals = quote(all_vals) + + return f"{key}={all_vals}" + +def encode_list_str(key: str, val: Union[List[str], None], urlencode_final_val: bool = False, urlencode_vals: bool = False) -> str: + if not val: + return f"{key}=" + + for x in range(len(val)): + if type(val[x]) != str: + val[x] = "xxx" + + if urlencode_vals: + val[x] = quote(val[x]) + + all_vals = ",".join(val) + if urlencode_final_val: + all_vals = quote(all_vals) + + return f"{key}={all_vals}" + +def encode_list_date(key: str, val: Union[List[datetime], None], urlencode_final_val: bool = False, urlencode_vals: bool = False, fmt: str = DivaConstants.LUT_TIME_FMT) -> str: + if not val: + return f"{key}=" + + for x in range(len(val)): + if type(val[x]) != datetime: + val[x] = datetime.now().astimezone() + + val[x] = val[x].replace(microsecond=0).strftime(fmt) + + if urlencode_vals: + val[x] = quote(val[x]) + + all_vals = ",".join(val) + if urlencode_final_val: + all_vals = quote(all_vals) + + return f"{key}={all_vals}" + +def encode_list_any(key: str, val: Union[List[Any], None], urlencode_final_val: bool = False, urlencode_vals: bool = False) -> str: + if not val: + return f"{key}=" + + for x in range(len(val)): + if val[x] is None: + val[x] = "x" + + if urlencode_vals: + val[x] = quote(val[x]) + + all_vals = ",".join([str(x) for x in val]) + if urlencode_final_val: + all_vals = quote(all_vals) + + return f"{key}={all_vals}"