diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/http.py | 3 | ||||
| -rw-r--r-- | src/utils/parse.py | 35 |
2 files changed, 36 insertions, 2 deletions
diff --git a/src/utils/http.py b/src/utils/http.py index 8b43a753..7cdae077 100644 --- a/src/utils/http.py +++ b/src/utils/http.py @@ -33,7 +33,6 @@ USERAGENT = "Mozilla/5.0 (compatible; BitBot/%s; +%s)" % ( RESPONSE_MAX = (1024*1024)*100 SOUP_CONTENT_TYPES = ["text/html", "text/xml", "application/xml"] -DECODE_CONTENT_TYPES = ["text/plain"]+SOUP_CONTENT_TYPES UTF8_CONTENT_TYPES = ["application/json"] class HTTPException(Exception): @@ -99,7 +98,7 @@ class Request(object): def get_headers(self) -> typing.Dict[str, str]: headers = self.headers.copy() if not "Accept-Language" in headers: - headers["Accept-Language"] = "en-GB" + headers["Accept-Language"] = "en-GB,en;q=0.5" if not "User-Agent" in headers: headers["User-Agent"] = self.useragent or USERAGENT if not "Content-Type" in headers and self.content_type: diff --git a/src/utils/parse.py b/src/utils/parse.py index ce2ee793..c0740785 100644 --- a/src/utils/parse.py +++ b/src/utils/parse.py @@ -120,3 +120,38 @@ def timed_args(args, min_args): return time, args[1:] return None, args +def format_tokens(s: str, names: typing.List[str], sigil: str="$" + ) -> typing.List[typing.Tuple[int, str]]: + names = names.copy() + names.sort() + names.reverse() + + i = 0 + max = len(s)-1 + sigil_found = False + tokens: typing.List[typing.Tuple[int, str]] = [] + + while i < max: + if s[i] == sigil: + i += 1 + if not s[i] == sigil: + for name in names: + if len(name) <= (len(s)-i) and s[i:i+len(name)] == name: + tokens.append((i-1, "%s%s" % (sigil, name))) + i += len(name) + break + else: + tokens.append((i, "$")) + i += 1 + return tokens + +def format_token_replace(s: str, vars: typing.Dict[str, str], + sigil: str="$") -> str: + vars = vars.copy() + vars.update({"": ""}) + tokens = format_tokens(s, list(vars.keys()), sigil) + tokens.sort(key=lambda x: x[0]) + tokens.reverse() + for i, token in tokens: + s = s[:i] + vars[token.replace(sigil, "", 1)] + s[i+len(token):] + return s |
