]> 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 logging
18
19 import argparse
20
21 from fba import commands
22 from fba import database
23
24 from fba.helpers import locking
25
26 logging.basicConfig(level=logging.INFO)
27 logger = logging.getLogger(__name__)
28
29 # Argument parser
30 _PARSER = None
31
32 def init_parser():
33     logger.debug("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
42     # Generic:
43     _PARSER.add_argument("--debug", action="store_const", dest="log_level", const=logging.DEBUG, help="Full debug output")
44
45     # Commands:
46     subparser_command = _PARSER.add_subparsers(
47         dest="command",
48         title="Commands to execute",
49         required=True,
50         help="Command to perform",
51     )
52
53     ### Check instance ###
54     parser = subparser_command.add_parser(
55         "check_instance",
56         help="Checks given instance if it exists and returns proper exit code"
57     )
58     parser.add_argument("--domain", required=True, help="Instance name (aka. domain) to check")
59     parser.set_defaults(command=commands.check_instance)
60
61     ### Fetch from bka.li ###
62     parser = subparser_command.add_parser(
63         "fetch_bkali",
64         help="Fetches domain names from bka.li API",
65     )
66     parser.set_defaults(command=commands.fetch_bkali)
67
68     ### Recheck instance's obfuscated peers ###
69     parser = subparser_command.add_parser(
70         "recheck_obfuscation",
71         help="Checks all instance's obfuscated peers if they can be de-obfuscated now.",
72     )
73     parser.add_argument("--domain", help="Instance name (aka. domain)")
74     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
75     parser.add_argument("--all", action="store_true", help="Include also already existing instances, otherwise only new are checked")
76     parser.set_defaults(command=commands.recheck_obfuscation)
77
78     ### Fetch blocks from registered instances or given ###
79     parser = subparser_command.add_parser(
80         "fetch_blocks",
81         help="Fetches blocks from registered instances (run command fetch_instances first!).",
82     )
83     parser.add_argument("--domain", help="Instance name (aka. domain)")
84     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
85     parser.set_defaults(command=commands.fetch_blocks)
86
87     ### Fetch blocks from chaos.social ###
88     parser = subparser_command.add_parser(
89         "fetch_cs",
90         help="Fetches blocks from chaos.social's meta sub domain.",
91     )
92     parser.set_defaults(command=commands.fetch_cs)
93
94     ### Fetch blocks from todon.eu wiki ###
95     parser = subparser_command.add_parser(
96         "fetch_todon_wiki",
97         help="Fetches blocks from todon.eu's wiki.",
98     )
99     parser.set_defaults(command=commands.fetch_todon_wiki)
100
101     ### Fetch blocks from a FBA-specific RSS feed  ###
102     parser = subparser_command.add_parser(
103         "fetch_fba_rss",
104         help="Fetches domains from a FBA-specific RSS feed.",
105     )
106     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
107     parser.set_defaults(command=commands.fetch_fba_rss)
108
109     ### Fetch blocks from FBA's bot account ###
110     parser = subparser_command.add_parser(
111         "fetch_fbabot_atom",
112         help="Fetches ATOM feed with domains from FBA's bot account.",
113     )
114     parser.set_defaults(command=commands.fetch_fbabot_atom)
115
116     ### Fetch blocks from oliphant's GIT repository ###
117     parser = subparser_command.add_parser(
118         "fetch_oliphant",
119         help="Fetches CSV files (block recommendations) for more possible instances to disover",
120     )
121     parser.add_argument("--domain", help="Instance name (aka. domain) to check")
122     parser.set_defaults(command=commands.fetch_oliphant)
123
124     ### Fetch instances from given initial instance ###
125     parser = subparser_command.add_parser(
126         "fetch_instances",
127         help="Fetches instances (aka. \"domains\") from an initial instance. You may want to re-run this command several times (at least 3 with big instances) to have a decent amount of valid instances.",
128     )
129     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 .")
130     parser.add_argument("--single", action="store_true", help="Only fetch given instance.")
131     parser.set_defaults(command=commands.fetch_instances)
132
133     ### Fetch blocks from static text file(s) ###
134     parser = subparser_command.add_parser(
135         "fetch_txt",
136         help="Fetches text/plain files as simple domain lists",
137     )
138     parser.set_defaults(command=commands.fetch_txt)
139
140     ### Fetch blocks from joinfediverse.wiki ###
141     parser = subparser_command.add_parser(
142         "fetch_joinfediverse",
143         help="Fetches FediBlock page from joinfediverse.wiki",
144     )
145     parser.set_defaults(command=commands.fetch_joinfediverse)
146
147     ### Fetch blocks from fediverse.observer ###
148     parser = subparser_command.add_parser(
149         "fetch_observer",
150         help="Fetches blocks from fediverse.observer.",
151     )
152     parser.set_defaults(command=commands.fetch_observer)
153     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
154
155     ### Fetch instances from fedipact.online ###
156     parser = subparser_command.add_parser(
157         "fetch_fedipact",
158         help="Fetches blocks from fedipact.online.",
159     )
160     parser.set_defaults(command=commands.fetch_fedipact)
161
162     ### Fetch from pixelfed.org's API ###
163     parser = subparser_command.add_parser(
164         "fetch_pixelfed_api",
165         help="Fetches domain names from pixelfed.org's API",
166     )
167     parser.set_defaults(command=commands.fetch_pixelfed_api)
168
169     ### Check nodeinfo ###
170     parser = subparser_command.add_parser(
171         "check_nodeinfo",
172         help="Checks if domain is part of nodeinfo.",
173     )
174     parser.set_defaults(command=commands.check_nodeinfo)
175
176     ### Fetch CSV from fedilist.com ###
177     parser = subparser_command.add_parser(
178         "fetch_fedilist",
179         help="Fetches CSV from fedilist.com",
180     )
181     parser.set_defaults(command=commands.fetch_fedilist)
182     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
183     parser.add_argument("--all", action="store_true", help="Include also already existing instances, otherwise only new are checked")
184
185     logger.debug("EXIT!")
186
187 def run_command():
188     logger.debug("CALLED!")
189     args = _PARSER.parse_args()
190
191     if args.log_level is not None:
192         loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
193         for _logger in loggers:
194             _logger.setLevel(args.log_level)
195
196     logger.debug("args[%s]='%s'", type(args), args)
197     status = args.command(args)
198
199     logger.debug("status=%d - EXIT!", status)
200     return status
201
202 def shutdown():
203     logger.debug("Closing database connection ...")
204     database.connection.close()
205     locking.release()
206     logger.debug("Shutdown completed.")