]> git.mxchange.org Git - fba.git/blob - fba/helpers/cookies.py
Fixed:
[fba.git] / fba / helpers / cookies.py
1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17 import logging
18
19 import validators
20
21 logging.basicConfig(level=logging.INFO)
22 logger = logging.getLogger(__name__)
23
24 # Cookies stor
25 _cookies = {}
26
27 def store (domain: str, cookies: dict):
28     logger.debug(f"domain='{domain}',cookies()={len(cookies)} - CALLED!")
29     if not isinstance(domain, str):
30         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
31     elif domain == "":
32         raise ValueError("Parameter 'domain' is empty")
33     elif domain.lower() != domain:
34         raise ValueError(f"Parameter domain='{domain}' must be all lower-case")
35     elif not validators.domain(domain.split("/")[0]):
36         raise ValueError(f"domain='{domain}' is not a valid domain")
37     elif domain.endswith(".arpa"):
38         raise ValueError(f"domain='{domain}' is a domain for reversed IP addresses, please don't crawl them!")
39     elif domain.endswith(".tld"):
40         raise ValueError(f"domain='{domain}' is a fake domain, please don't crawl them!")
41     elif not isinstance(cookies, dict):
42         raise ValueError(f"Parameter cookies[]='{type(cookies)}' is not 'dict'")
43
44     _cookies[domain] = cookies
45
46     logger.debug("EXIT!")
47
48 def get_all(domain: str) -> dict:
49     logger.debug("domain(%d)='%s' - CALLED!", len(domain), domain)
50     if not isinstance(domain, str):
51         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
52     elif domain == "":
53         raise ValueError("Parameter 'domain' is empty")
54     elif domain.lower() != domain:
55         raise ValueError(f"Parameter domain='{domain}' must be all lower-case")
56     elif not validators.domain(domain.split("/")[0]):
57         raise ValueError(f"domain='{domain}' is not a valid domain")
58     elif domain.endswith(".arpa"):
59         raise ValueError(f"domain='{domain}' is a domain for reversed IP addresses, please don't crawl them!")
60     elif domain.endswith(".tld"):
61         raise ValueError(f"domain='{domain}' is a fake domain, please don't crawl them!")
62     elif not has(domain):
63         raise Exception(f"domain='{domain}' has no cookies stored, maybe invoke store() first?")
64
65     logger.debug(f"_cookies[{domain}]()={len(_cookies[domain])} - EXIT!")
66     return _cookies[domain]
67
68 def has (domain: str) -> bool:
69     logger.debug("domain(%d)='%s' - CALLED!", len(domain), domain)
70     if not isinstance(domain, str):
71         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
72     elif domain == "":
73         raise ValueError("Parameter 'domain' is empty")
74     elif domain.lower() != domain:
75         raise ValueError(f"Parameter domain='{domain}' must be all lower-case")
76
77     has_cookies = domain in _cookies
78
79     logger.debug(f"has_cookies='{has_cookies}' - EXIT!")
80     return has_cookies
81
82 def clear (domain: str):
83     logger.debug("domain(%d)='%s' - CALLED!", len(domain), domain)
84     if not isinstance(domain, str):
85         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
86     elif domain == "":
87         raise ValueError("Parameter 'domain' is empty")
88     elif domain.lower() != domain:
89         raise ValueError(f"Parameter domain='{domain}' must be all lower-case")
90     elif not validators.domain(domain.split("/")[0]):
91         raise ValueError(f"domain='{domain}' is not a valid domain")
92     elif domain.endswith(".arpa"):
93         raise ValueError(f"domain='{domain}' is a domain for reversed IP addresses, please don't crawl them!")
94     elif domain.endswith(".tld"):
95         raise ValueError(f"domain='{domain}' is a fake domain, please don't crawl them!")
96
97     if has(domain):
98         logger.debug(f"Removing cookies for domain='{domain}' ...")
99         del _cookies[domain]
100
101     logger.debug("EXIT!")