]> git.mxchange.org Git - fba.git/blob - fba/helpers/tidyup.py
f09c2824eaeb9032f771398a672ae66ac1533d61
[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 logging
17 import re
18
19 logging.basicConfig(level=logging.INFO)
20 logger = logging.getLogger(__name__)
21
22 def reason(string: str) -> str:
23     logger.debug(f"string='{string}' - CALLED!")
24     if not isinstance(string, str):
25         raise ValueError(f"Parameter string[]='{type(string)}' is not 'str'")
26
27     # Strip string
28     string = string.strip()
29
30     logger.debug(f"string='{string}' - EXIT!")
31     return string
32
33 def domain(string: str) -> str:
34     logger.debug(f"string='{string}' - CALLED!")
35     if not isinstance(string, str):
36         raise ValueError(f"Parameter string[]='{type(string)}' is not 'str'")
37
38     # All lower-case and strip spaces out + last dot
39     string = string.lower().strip().rstrip(".")
40     logger.debug(f"string='{string}' - #1")
41
42     # No port number
43     string = re.sub("\:\d+$", "", string)
44     logger.debug(f"string='{string}' - #2")
45
46     # No protocol, sometimes without the slashes
47     string = re.sub("^https?\:(\/*)", "", string)
48     logger.debug(f"string='{string}' - #3")
49
50     # No trailing slash
51     string = re.sub("\/$", "", string)
52     logger.debug(f"string='{string}' - #4")
53
54     # No @ or : sign
55     string = re.sub("^\@", "", string)
56     string = string.split(":")[0]
57     logger.debug(f"string='{string}' - #4")
58
59     # No individual users in block lists
60     string = re.sub("(.+)\@", "", string)
61     logger.debug(f"string='{string}' - #5")
62     if string.find("/profile/"):
63         string = string.split("/profile/")[0]
64     elif string.find("/users/"):
65         string = string.split("/users/")[0]
66
67     logger.debug(f"string='{string}' - EXIT!")
68     return string