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