1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
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.
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.
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/>.
21 logging.basicConfig(level=logging.INFO)
22 logger = logging.getLogger(__name__)
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'")
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'")
44 _cookies[domain] = cookies
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'")
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!")
63 raise Exception(f"domain='{domain}' has no cookies stored, maybe invoke store() first?")
65 logger.debug(f"_cookies[{domain}]()={len(_cookies[domain])} - EXIT!")
66 return _cookies[domain]
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'")
73 raise ValueError("Parameter 'domain' is empty")
74 elif domain.lower() != domain:
75 raise ValueError(f"Parameter domain='{domain}' must be all lower-case")
77 has_cookies = domain in _cookies
79 logger.debug(f"has_cookies='{has_cookies}' - EXIT!")
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'")
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!")
98 logger.debug(f"Removing cookies for domain='{domain}' ...")
101 logger.debug("EXIT!")