]> git.mxchange.org Git - fba.git/blob - fetch_bkali.py
Continued:
[fba.git] / fetch_bkali.py
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
5 # Copyright (C) 2023 Free Software Foundation
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published
9 # by the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16 #
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20 import json
21 import sys
22 import validators
23 import fba
24
25 fba.lock_process()
26
27 domains = list()
28 try:
29     fetched = fba.post_json_api("gql.api.bka.li", "/v1/graphql", json.dumps({
30         "query": "query domainlist {nodeinfo(order_by: {domain: asc}) {domain}}"
31     }))
32
33     # DEBUG: print(f"DEBUG: fetched({len(fetched)})[]='{type(fetched)}'")
34     if len(fetched) == 0:
35         raise Exception("WARNING: Returned no records")
36     elif not "data" in fetched:
37         raise Exception(f"WARNING: fetched()={len(fetched)} does not contain element 'data'")
38     elif not "nodeinfo" in fetched["data"]:
39         raise Exception(f"WARNING: fetched()={len(fetched['data'])} does not contain element 'nodeinfo'")
40
41     for entry in fetched["data"]["nodeinfo"]:
42         # DEBUG: print(f"DEBUG: entry['{type(entry)}']='{entry}'")
43         if not "domain" in entry:
44             print(f"WARNING: entry does not contain 'domain' - SKIPPED!")
45             continue
46         elif not validators.domain(entry["domain"]):
47             print(f"WARNING: domain='{entry['domain']}' is not a valid domain - SKIPPED!")
48             continue
49         elif fba.is_blacklisted(entry["domain"]):
50             # DEBUG: print(f"DEBUG: domain='{entry['domain']}' is blacklisted - SKIPPED!")
51             continue
52         elif fba.is_instance_registered(entry["domain"]):
53             # DEBUG: print(f"DEBUG: domain='{entry['domain']}' is already registered - SKIPPED!")
54             continue
55
56         # DEBUG: print(f"DEBUG: Adding domain='{entry['domain']}' ...")
57         domains.append(entry["domain"])
58
59 except BaseException as e:
60     print(f"ERROR: Cannot fetch graphql,exception[{type(e)}]:'{str(e)}'")
61     sys.exit(255)
62
63 # Show domains
64 # DEBUG: print(f"DEBUG: domains()={len(domains)}")
65 if len(domains) > 0:
66     print(f"INFO: Adding {len(domains)} new instances ...")
67     for domain in domains:
68         print(f"INFO: Fetching instances from domain='{domain}' ...")
69         fba.fetch_instances(domain, None, None, sys.argv[0])
70
71 fba.shutdown()