]> git.mxchange.org Git - fba.git/blob - fba/boot.py
56d2643a55836f8a986e7df0cf3d87ee97e83dc6
[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 todon.eu wiki ###
80     parser = subparser_command.add_parser(
81         "fetch_todon_wiki",
82         help="Fetches blocks from todon.eu's wiki.",
83     )
84     parser.set_defaults(command=commands.fetch_todon_wiki)
85
86     ### Fetch blocks from a FBA-specific RSS feed  ###
87     parser = subparser_command.add_parser(
88         "fetch_fba_rss",
89         help="Fetches domains from a FBA-specific RSS feed.",
90     )
91     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
92     parser.set_defaults(command=commands.fetch_fba_rss)
93
94     ### Fetch blocks from FBA's bot account ###
95     parser = subparser_command.add_parser(
96         "fetch_fbabot_atom",
97         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.",
98     )
99     parser.set_defaults(command=commands.fetch_fbabot_atom)
100
101     ### Fetch blocks from oliphant's GIT repository ###
102     parser = subparser_command.add_parser(
103         "fetch_oliphant",
104         help="Fetches CSV files (block recommendations) for more possible instances to disover",
105     )
106     parser.add_argument("--domain", help="Instance name (aka. domain) to check")
107     parser.set_defaults(command=commands.fetch_oliphant)
108
109     ### Fetch instances from given initial instance ###
110     parser = subparser_command.add_parser(
111         "fetch_instances",
112         help="Fetches instances (aka. \"domains\") from an initial instance.",
113     )
114     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 .")
115     parser.add_argument("--single", action="store_true", help="Only fetch given instance.")
116     parser.set_defaults(command=commands.fetch_instances)
117
118     ### Fetch blocks from static text file(s) ###
119     parser = subparser_command.add_parser(
120         "fetch_txt",
121         help="Fetches text/plain files as simple domain lists",
122     )
123     parser.set_defaults(command=commands.fetch_txt)
124
125     ### Fetch blocks from fediverse.observer ###
126     parser = subparser_command.add_parser(
127         "fetch_observer",
128         help="Fetches blocks from fediverse.observer.",
129     )
130     parser.set_defaults(command=commands.fetch_observer)
131
132     ### Fetch instances from fedipact.online ###
133     parser = subparser_command.add_parser(
134         "fetch_fedipact",
135         help="Fetches blocks from fedipact.online.",
136     )
137     parser.set_defaults(command=commands.fetch_fedipact)
138
139     logger.debug("init_parser(): EXIT!")
140
141 def run_command():
142     logger.debug("run_command(): CALLED!")
143     args = _PARSER.parse_args()
144     logger.debug(f"args[{type(args)}]={args}")
145     status = args.command(args)
146     logger.debug("status={status} - EXIT!")
147     return status if isinstance(status, int) else 0
148
149 def shutdown():
150     logger.debug("Closing database connection ...")
151     fba.connection.close()
152     locking.release()
153     logger.debug("Shutdown completed.")