]> 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 domains = list()
26 try:
27     fetched = fba.post_json_api("gql.api.bka.li", "/v1/graphql", json.dumps({
28         "query": "query domainlist {nodeinfo(order_by: {domain: asc}) {domain}}"
29     }))
30
31     # DEBUG: print(f"DEBUG: fetched({len(fetched)})[]='{type(fetched)}'")
32     if len(fetched) == 0:
33         raise Exception("WARNING: Returned no records")
34     elif not "data" in fetched:
35         raise Exception(f"WARNING: fetched()={len(fetched)} does not contain element 'data'")
36     elif not "nodeinfo" in fetched["data"]:
37         raise Exception(f"WARNING: fetched()={len(fetched['data'])} does not contain element 'nodeinfo'")
38
39     for entry in fetched["data"]["nodeinfo"]:
40         # DEBUG: print(f"DEBUG: entry['{type(entry)}']='{entry}'")
41         if not "domain" in entry:
42             print(f"WARNING: entry does not contain 'domain' - SKIPPED!")
43             continue
44         elif not validators.domain(entry["domain"]):
45             print(f"WARNING: domain='{entry['domain']}' is not a valid domain - SKIPPED!")
46             continue
47         elif fba.is_blacklisted(entry["domain"]):
48             # DEBUG: print(f"DEBUG: domain='{entry['domain']}' is blacklisted - SKIPPED!")
49             continue
50         elif fba.is_instance_registered(entry["domain"]):
51             # DEBUG: print(f"DEBUG: domain='{entry['domain']}' is already registered - SKIPPED!")
52             continue
53
54         # DEBUG: print(f"DEBUG: Adding domain='{entry['domain']}' ...")
55         domains.append(entry["domain"])
56
57 except BaseException as e:
58     print(f"ERROR: Cannot fetch graphql,exception[{type(e)}]:'{str(e)}'")
59     sys.exit(255)
60
61 # Show domains
62 # DEBUG: print(f"DEBUG: domains()={len(domains)}")
63 if len(domains) > 0:
64     print(f"INFO: Adding {len(domains)} new instances ...")
65     for domain in domains:
66         print(f"INFO: Fetching instances from domain='{domain}' ...")
67         fba.fetch_instances(domain, None, None, sys.argv[0])
68
69 fba.connection.close()