]> git.mxchange.org Git - fba.git/blob - fetch_fba_rss.py
Continued:
[fba.git] / fetch_fba_rss.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 reqto
21 import rss_parser
22 import sys
23 from fba import *
24
25 boot.acquire_lock()
26
27 feed = sys.argv[1]
28
29 domains = list()
30 try:
31     print(f"INFO: Fetch FBA-specific RSS feed='{feed}' ...")
32     res = reqto.get(feed, headers=fba.headers, timeout=(fba.config["connection_timeout"], fba.config["read_timeout"]))
33
34     # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code='{res.status_code}',res.text()={len(res.text)}")
35     if res.ok and res.status_code < 300 and len(res.text) > 0:
36         # DEBUG: print(f"DEBUG: Parsing RSS feed ...")
37         rss = rss_parser.Parser.parse(res.text)
38         for item in rss.channel.items:
39             # DEBUG: print(f"DEBUG: item.link={item.link}")
40             domain = item.link.split("=")[1]
41
42             if fba.is_blacklisted(domain):
43                 # DEBUG: print(f"DEBUG: domain='{domain}' is blacklisted - SKIPPED!")
44                 continue
45             elif domain in domains:
46                 # DEBUG: print(f"DEBUG: domain='{domain}' is already added - SKIPPED!")
47                 continue
48             elif fba.is_instance_registered(domain):
49                 # DEBUG: print(f"DEBUG: domain='{domain}' is already registered - SKIPPED!")
50                 continue
51
52             # DEBUG: print(f"DEBUG: domain='{domain}'")
53             domains.append(domain)
54
55 except BaseException as e:
56     print(f"ERROR: Cannot fetch feed='{feed}',exception[{type(e)}]:'{str(e)}'")
57
58 # Show domains
59 # DEBUG: print(f"DEBUG: domains()={len(domains)}")
60 if len(domains) > 0:
61     print(f"INFO: Adding {len(domains)} new instances ...")
62     for domain in domains:
63         print(f"INFO: Fetching instances from domain='{domain}' ...")
64         fba.fetch_instances(domain, None, None, sys.argv[0])
65
66 boot.shutdown()