]> 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 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("string='%s' - CALLED!", string)
24
25     if not isinstance(string, str):
26         raise ValueError(f"Parameter string[]='{type(string)}' is not of type 'str'")
27
28     # Strip string
29     string = string.strip()
30
31     logger.debug("string='%s' - EXIT!", string)
32     return string
33
34 def domain(string: str) -> str:
35     logger.debug("string='%s' - CALLED!", string)
36
37     if not isinstance(string, str):
38         raise ValueError(f"Parameter string[]='{type(string)}' is not of type 'str'")
39     elif string == "":
40         raise ValueError("Parameter string is empty")
41
42     # All lower-case and strip spaces out + last dot
43     string = string.lower().strip().rstrip(".").replace("..", ".")
44     logger.debug("string='%s' - #1", string)
45
46     # No port number
47     string = re.sub(r"\:\d+$", "", string)
48     logger.debug("string='%s' - #2", string)
49
50     # No protocol, sometimes without the slashes
51     string = re.sub(r"^https?\:(\/*)", "", string)
52     logger.debug("string='%s' - #3", string)
53
54     # No trailing slash
55     string = re.sub(r"\/$", "", string)
56     logger.debug("string='%s' - #4", string)
57
58     # No @ or : sign
59     string = re.sub(r"^\@", "", string)
60     string = string.split(":")[0]
61     logger.debug("string='%s' - #5", string)
62
63     # Try to "detect" user profiles, not wanted here. Don't block single users
64     # in an instance block list! Everything personal can be solved in a
65     # personal block.
66     string = re.sub(r"(.+)\@", "", string)
67     logger.debug("string='%s' - #6", string)
68
69     if string.find("/profile/"):
70         string = string.split("/profile/")[0]
71     elif string.find("/users/"):
72         string = string.split("/users/")[0]
73     elif string.find("/tag/"):
74         string = string.split("/tag/")[0]
75
76     # Some people have TLDs with this word on the end
77     logger.debug("string='%s' - #7", string)
78     if string.endswith("silence"):
79         string = string.split("silence")[0]
80
81     logger.debug("string='%s' - EXIT!", string)
82     return string