]> git.mxchange.org Git - fba.git/blob - fba/boot.py
Continued:
[fba.git] / fba / boot.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 argparse
18 import os
19 import sys
20 import tempfile
21 import zc.lockfile
22 from fba import command
23 from fba import fba
24
25 # Lock file
26 lockfile = tempfile.gettempdir() + '/fba.lock'
27 LOCK = None
28 _PARSER = None
29
30 def acquire_lock():
31     global LOCK
32     try:
33         print(f"DEBUG: Acquiring lock: '{lockfile}'")
34         LOCK = zc.lockfile.LockFile(lockfile)
35         print("DEBUG: Lock obtained.")
36
37     except zc.lockfile.LockError:
38         print(f"ERROR: Cannot aquire lock: '{lockfile}'")
39         sys.exit(100)
40
41 def init_parser():
42     # DEBUG: print("DEBUG: init_parser(): CALLED!")
43     global _PARSER
44
45     print("DEBUG: Initializing parser ...")
46     _PARSER = argparse.ArgumentParser(
47         prog="Fedi API Block",
48         description="Fetches block reasons from the fediverse"
49     )
50     subparser_command = _PARSER.add_subparsers(
51         dest="command",
52         title="Commands to execute",
53         required=True,
54         help="Command to perform",
55     )
56
57     ### Check instance ###
58     parser = subparser_command.add_parser(
59         "check_instance",
60         help="Checks given instance if it exists and returns proper exit code"
61     )
62     parser.add_argument("--domain", required=True, help="Instance name (aka. domain) to check")
63     parser.set_defaults(command=command.check_instance)
64
65     ### Fetch from bka.li ###
66     parser = subparser_command.add_parser(
67         "fetch_bkali",
68         help="Fetches domain names from bka.li API",
69     )
70     parser.set_defaults(command=command.fetch_bkali)
71
72     ### Fetch blocks from registered instances or given ###
73     parser = subparser_command.add_parser(
74         "fetch_blocks",
75         help="Fetches blocks from registered instances (run command fetch_instances first!).",
76     )
77     parser.add_argument("--domain", help="Instance name (aka. domain) to fetch blocks from")
78     parser.set_defaults(command=command.fetch_blocks)
79
80     ### Fetch blocks from chaos.social ###
81     parser = subparser_command.add_parser(
82         "fetch_cs",
83         help="Fetches blocks from chaos.social's meta sub domain.",
84     )
85     parser.set_defaults(command=command.fetch_cs)
86
87     ### Fetch blocks from a FBA-specific RSS feed  ###
88     parser = subparser_command.add_parser(
89         "fetch_fba_rss",
90         help="Fetches domains from a FBA-specific RSS feed.",
91     )
92     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
93     parser.set_defaults(command=command.fetch_fba_rss)
94
95     ### Fetch blocks from FBA's bot account ###
96     parser = subparser_command.add_parser(
97         "fetch_fbabot_atom",
98         help="Fetches ATOM feed with domains from FBA's bot account. You may wish to re-run this command several times (at least 3 with big instances) to have a decent amount of valid instances.",
99     )
100     parser.set_defaults(command=command.fetch_fbabot_atom)
101
102     ### Fetch instances from given initial instance ###
103     parser = subparser_command.add_parser(
104         "fetch_instances",
105         help="Fetches instances (aka. \"domains\") from an initial instance.",
106     )
107     parser.add_argument("--domain", help="Instance name (aka. domain) to fetch further instances from. Start with a large instance, e.g. mastodon.social .")
108     parser.set_defaults(command=command.fetch_instances)
109
110     # DEBUG: print("DEBUG: init_parser(): EXIT!")
111
112 def run_command():
113     # DEBUG: print("DEBUG: run_command(): CALLED!")
114     args = _PARSER.parse_args()
115     print(f"DEBUG: args[{type(args)}]={args}")
116     args.command(args)
117     # DEBUG: print("DEBUG: run_command(): EXIT!")
118
119 def shutdown():
120     print("DEBUG: Closing database connection ...")
121     fba.connection.close()
122
123     if LOCK != None:
124         print("DEBUG: Releasing lock ...")
125         LOCK.close()
126         print(f"DEBUG: Deleting lockfile='{lockfile}' ...")
127         os.remove(lockfile)
128
129     print("DEBUG: Shutdown completed.")