]> git.mxchange.org Git - fba.git/blob - fba/boot.py
aed135a814a310663faca5aaae803e6106ad1e30
[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 logging
18
19 import argparse
20
21 from fba import commands
22 from fba import fba
23
24 from fba.helpers import locking
25
26 logging.basicConfig(level=logging.WARNING)
27 logger = logging.getLogger(__name__)
28
29 # Argument parser
30 _PARSER = None
31
32 def init_parser():
33     logger.debug("init_parser(): CALLED!")
34     global _PARSER
35
36     logger.debug("Initializing parser ...")
37     _PARSER = argparse.ArgumentParser(
38         description="Fetches block reasons from the fediverse",
39         epilog="Please note that some commands have optional arguments, you may want to try fba.py <command> --help to find them out.",
40     )
41     subparser_command = _PARSER.add_subparsers(
42         dest="command",
43         title="Commands to execute",
44         required=True,
45         help="Command to perform",
46     )
47
48     ### Check instance ###
49     parser = subparser_command.add_parser(
50         "check_instance",
51         help="Checks given instance if it exists and returns proper exit code"
52     )
53     parser.add_argument("--domain", required=True, help="Instance name (aka. domain) to check")
54     parser.set_defaults(command=commands.check_instance)
55
56     ### Fetch from bka.li ###
57     parser = subparser_command.add_parser(
58         "fetch_bkali",
59         help="Fetches domain names from bka.li API",
60     )
61     parser.set_defaults(command=commands.fetch_bkali)
62
63     ### Fetch blocks from registered instances or given ###
64     parser = subparser_command.add_parser(
65         "fetch_blocks",
66         help="Fetches blocks from registered instances (run command fetch_instances first!).",
67     )
68     parser.add_argument("--domain", help="Instance name (aka. domain)")
69     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
70     parser.set_defaults(command=commands.fetch_blocks)
71
72     ### Fetch blocks from chaos.social ###
73     parser = subparser_command.add_parser(
74         "fetch_cs",
75         help="Fetches blocks from chaos.social's meta sub domain.",
76     )
77     parser.set_defaults(command=commands.fetch_cs)
78
79     ### Fetch blocks from a FBA-specific RSS feed  ###
80     parser = subparser_command.add_parser(
81         "fetch_fba_rss",
82         help="Fetches domains from a FBA-specific RSS feed.",
83     )
84     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
85     parser.set_defaults(command=commands.fetch_fba_rss)
86
87     ### Fetch blocks from FBA's bot account ###
88     parser = subparser_command.add_parser(
89         "fetch_fbabot_atom",
90         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.",
91     )
92     parser.set_defaults(command=commands.fetch_fbabot_atom)
93
94     ### Fetch blocks from oliphant's GIT repository ###
95     parser = subparser_command.add_parser(
96         "fetch_oliphant",
97         help="Fetches CSV files (block recommendations) for more possible instances to disover",
98     )
99     parser.add_argument("--domain", help="Instance name (aka. domain) to check")
100     parser.set_defaults(command=commands.fetch_oliphant)
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", required=True, help="Instance name (aka. domain) to fetch further instances from. Start with a large instance, e.g. mastodon.social .")
108     parser.add_argument("--single", action="store_true", help="Only fetch given instance.")
109     parser.set_defaults(command=commands.fetch_instances)
110
111     ### Fetch blocks from static text file(s) ###
112     parser = subparser_command.add_parser(
113         "fetch_txt",
114         help="Fetches text/plain files as simple domain lists",
115     )
116     parser.set_defaults(command=commands.fetch_txt)
117
118     ### Fetch blocks from fediverse.observer ###
119     parser = subparser_command.add_parser(
120         "fetch_observer",
121         help="Fetches blocks from fediverse.observer.",
122     )
123     parser.set_defaults(command=commands.fetch_observer)
124
125     ### Fetch instances from fedipact.online ###
126     parser = subparser_command.add_parser(
127         "fetch_fedipact",
128         help="Fetches blocks from fedipact.online.",
129     )
130     parser.set_defaults(command=commands.fetch_fedipact)
131
132     logger.debug("init_parser(): EXIT!")
133
134 def run_command():
135     logger.debug("run_command(): CALLED!")
136     args = _PARSER.parse_args()
137     logger.debug(f"args[{type(args)}]={args}")
138     status = args.command(args)
139     logger.debug("status={status} - EXIT!")
140     return status if isinstance(status, int) else 0
141
142 def shutdown():
143     logger.debug("Closing database connection ...")
144     fba.connection.close()
145     locking.release()
146     logger.debug("Shutdown completed.")