]> 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
19 from fba import commands
20 from fba import fba
21 from fba import locking
22
23 _PARSER = None
24
25 def init_parser():
26     # DEBUG: print("DEBUG: init_parser(): CALLED!")
27     global _PARSER
28
29     # DEBUG: print("DEBUG: Initializing parser ...")
30     _PARSER = argparse.ArgumentParser(
31         description="Fetches block reasons from the fediverse",
32         epilog="Please note that some commands have optional arguments, you may want to try fba.py <command> --help to find them out.",
33     )
34     subparser_command = _PARSER.add_subparsers(
35         dest="command",
36         title="Commands to execute",
37         required=True,
38         help="Command to perform",
39     )
40
41     ### Check instance ###
42     parser = subparser_command.add_parser(
43         "check_instance",
44         help="Checks given instance if it exists and returns proper exit code"
45     )
46     parser.add_argument("--domain", required=True, help="Instance name (aka. domain) to check")
47     parser.set_defaults(command=commands.check_instance)
48
49     ### Fetch from bka.li ###
50     parser = subparser_command.add_parser(
51         "fetch_bkali",
52         help="Fetches domain names from bka.li API",
53     )
54     parser.set_defaults(command=commands.fetch_bkali)
55
56     ### Fetch blocks from registered instances or given ###
57     parser = subparser_command.add_parser(
58         "fetch_blocks",
59         help="Fetches blocks from registered instances (run command fetch_instances first!).",
60     )
61     parser.add_argument("--domain", help="Instance name (aka. domain) to fetch blocks from")
62     parser.set_defaults(command=commands.fetch_blocks)
63
64     ### Fetch blocks from chaos.social ###
65     parser = subparser_command.add_parser(
66         "fetch_cs",
67         help="Fetches blocks from chaos.social's meta sub domain.",
68     )
69     parser.set_defaults(command=commands.fetch_cs)
70
71     ### Fetch blocks from a FBA-specific RSS feed  ###
72     parser = subparser_command.add_parser(
73         "fetch_fba_rss",
74         help="Fetches domains from a FBA-specific RSS feed.",
75     )
76     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
77     parser.set_defaults(command=commands.fetch_fba_rss)
78
79     ### Fetch blocks from FBA's bot account ###
80     parser = subparser_command.add_parser(
81         "fetch_fbabot_atom",
82         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.",
83     )
84     parser.set_defaults(command=commands.fetch_fbabot_atom)
85
86     ### Fetch blocks from oliphant's GIT repository ###
87     parser = subparser_command.add_parser(
88         "fetch_oliphant",
89         help="Fetches CSV files (block recommendations) for more possible instances to disover",
90     )
91     parser.add_argument("--domain", help="Instance name (aka. domain) to check")
92     parser.set_defaults(command=commands.fetch_oliphant)
93
94     ### Fetch instances from given initial instance ###
95     parser = subparser_command.add_parser(
96         "fetch_instances",
97         help="Fetches instances (aka. \"domains\") from an initial instance.",
98     )
99     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 .")
100     parser.add_argument("--single", action="store_true", help="Only fetch given instance.")
101     parser.set_defaults(command=commands.fetch_instances)
102
103     ### Fetch blocks from static text file(s) ###
104     parser = subparser_command.add_parser(
105         "fetch_txt",
106         help="Fetches text/plain files as simple domain lists",
107     )
108     parser.set_defaults(command=commands.fetch_txt)
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     # DEBUG: print(f"DEBUG: args[{type(args)}]={args}")
116     status = args.command(args)
117     # DEBUG: print("DEBUG: status={status} - EXIT!")
118     return status if isinstance(status, int) else 0
119
120 def shutdown():
121     # DEBUG: print("DEBUG: Closing database connection ...")
122     fba.connection.close()
123     locking.release()
124     # DEBUG: print("DEBUG: Shutdown completed.")