]> 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     if not isinstance(string, str):
25         raise ValueError(f"Parameter string[]='{type(string)}' is not of type 'str'")
26
27     # Strip string
28     string = string.strip()
29
30     logger.debug("string='%s' - EXIT!", string)
31     return string
32
33 def domain(string: str) -> str:
34     logger.debug("string='%s' - CALLED!", string)
35     if not isinstance(string, str):
36         raise ValueError(f"Parameter string[]='{type(string)}' is not of type 'str'")
37
38     # All lower-case and strip spaces out + last dot
39     string = string.lower().strip().rstrip(".")
40     logger.debug("string='%s' - #1", string)
41
42     # No port number
43     string = re.sub(r"\:\d+$", "", string)
44     logger.debug("string='%s' - #2", string)
45
46     # No protocol, sometimes without the slashes
47     string = re.sub(r"^https?\:(\/*)", "", string)
48     logger.debug("string='%s' - #3", string)
49
50     # No trailing slash
51     string = re.sub(r"\/$", "", string)
52     logger.debug("string='%s' - #4", string)
53
54     # No @ or : sign
55     string = re.sub(r"^\@", "", string)
56     string = string.split(":")[0]
57     logger.debug("string='%s' - #5", string)
58
59     # No individual users in block lists
60     string = re.sub(r"(.+)\@", "", string)
61     logger.debug("string='%s' - #6", string)
62
63     if string.find("/profile/"):
64         string = string.split("/profile/")[0]
65     elif string.find("/users/"):
66         string = string.split("/users/")[0]
67     elif string.find("/tag/"):
68         string = string.split("/tag/")[0]
69
70     logger.debug("string='%s' - EXIT!", string)
71     return string