]> git.mxchange.org Git - fba.git/blob - fba/helpers/tidyup.py
Continued:
[fba.git] / fba / helpers / tidyup.py
1 # Copyright (C) 2023 Free Software Foundation
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU Affero General Public License as published
5 # by the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU Affero General Public License for more details.
12 #
13 # You should have received a copy of the GNU Affero General Public License
14 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16 import re
17
18 def reason(string: str) -> str:
19     # DEBUG: print(f"DEBUG: string='{string}' - CALLED!")
20     if not isinstance(string, str):
21         raise ValueError(f"Parameter string[]='{type(string)}' is not 'str'")
22
23     # Strip string
24     string = string.strip()
25
26     # Replace â with "
27     string = re.sub("â", "\"", string)
28
29     # DEBUG: print(f"DEBUG: string='{string}' - EXIT!")
30     return string
31
32 def domain(string: str) -> str:
33     # DEBUG: print(f"DEBUG: string='{string}' - CALLED!")
34     if not isinstance(string, str):
35         raise ValueError(f"Parameter string[]='{type(string)}' is not 'str'")
36
37     # All lower-case and strip spaces out + last dot
38     string = string.lower().strip().rstrip(".")
39
40     # No port number
41     string = re.sub("\:\d+$", "", string)
42
43     # No protocol, sometimes without the slashes
44     string = re.sub("^https?\:(\/*)", "", string)
45
46     # No trailing slash
47     string = re.sub("\/$", "", string)
48
49     # No @ sign
50     string = re.sub("^\@", "", string)
51
52     # No individual users in block lists
53     string = re.sub("(.+)\@", "", string)
54     if string.find("/profile/"):
55         string = string.split("/profile/")[0]
56     elif string.find("/users/"):
57         string = string.split("/users/")[0]
58
59     # DEBUG: print(f"DEBUG: string='{string}' - EXIT!")
60     return string