]> git.mxchange.org Git - fba.git/blob - fba/models/apis.py
8725e9a125730136e94d5b05247043dcf0f0fd83
[fba.git] / fba / models / apis.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 time
20
21 from fba import database
22
23 from fba.helpers import config
24 from fba.helpers import domain as domain_helper
25
26 logging.basicConfig(level=logging.INFO)
27 logger = logging.getLogger(__name__)
28
29 def is_recent(api_domain: str) -> bool:
30     logger.debug("api_domain='%s' - CALLED!", api_domain)
31     domain_helper.raise_on(api_domain)
32
33     is_recent = False
34     database.cursor.execute("SELECT last_accessed FROM apis WHERE hostname = ? LIMIT 1", [api_domain])
35
36     row = database.cursor.fetchone()
37     logger.debug("row[]='%s'", type(row))
38     if row is not None:
39         logger.debug("api_domain='%s',row[last_accessed]=%d", api_domain, row["last_accessed"])
40         is_recent = (time.time() - row["last_accessed"]) <= config.get("api_last_access")
41
42     logger.debug("is_recent='%s' - EXIT!", is_recent)
43     return is_recent
44
45 def update (api_domain: str):
46     logger.debug("api_domain='%s' - CALLED!", api_domain)
47     domain_helper.raise_on(api_domain)
48
49     database.cursor.execute("SELECT * FROM apis WHERE hostname = ? LIMIT 1", [api_domain])
50
51     row = database.cursor.fetchone()
52     logger.debug("row[]='%s'", type(row))
53     if row is None:
54         # Add instance
55         database.cursor.execute("INSERT INTO apis (hostname, last_accessed) VALUES (?, ?)", [
56             api_domain,
57             time.time()
58         ])
59     else:
60         # Update last_accessed
61         database.cursor.execute("UPDATE apis SET last_accessed = ? WHERE hostname = ? LIMIT 1", [
62             time.time(),
63             api_domain
64         ])
65
66     logger.debug("EXIT!")