]> git.mxchange.org Git - fba.git/blob - fba/helpers/software.py
Continued:
[fba.git] / fba / helpers / software.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 from fba.helpers import tidyup
20
21 logging.basicConfig(level=logging.INFO)
22 logger = logging.getLogger(__name__)
23
24 # A list of relay software
25 relays = [
26     "activityrelay",
27     "aoderelay",
28     "selective-relay",
29     "pub-relay"
30 ]
31
32 # In-function cache
33 _cache = {
34     # Cache for function alias()
35     "alias" : {},
36 }
37
38 def alias(software: str) -> str:
39     logger.debug("software='%s'- CALLED!", software)
40
41     if not isinstance(software, str) and software is not None:
42         raise ValueError(f"software[]='{type(software)}' is not type 'str'")
43     elif software == "":
44         raise ValueError("Parameter 'software' is empty")
45     elif software in _cache["alias"]:
46         logger.debug("Returning cached alias='%s' - EXIT!", _cache["alias"][software])
47         return _cache["alias"][software]
48
49     key = software
50
51     logger.debug("software='%s'- BEFORE!", software)
52     software = tidyup.domain(software)
53     logger.debug("software='%s'- AFTER!", software)
54
55     if software in ["akkoma", "rebased", "akkounfucked", "ched", "incestoma", "revolver"]:
56         logger.debug("Setting pleroma: software='%s'", software)
57         software = "pleroma"
58     elif "radiant" in software:
59         logger.debug("Setting radiant: software='%s'", software)
60         software = "radiant"
61     elif software in ["hometown", "ecko", "fedibird", "glitchcafe"] or "되는 마스토돈" in software or "mastodon" in software:
62         logger.debug("Setting mastodon: software='%s'", software)
63         software = "mastodon"
64     elif software in ["slipfox calckey", "calckey", "groundpolis", "foundkey", "cherrypick", "meisskey", "magnetar", "keybump", "dolphin", "calckey social", "azk.sns", "firefish", "qtmmsky", "iceshrimp", "owohub", "re+", "russkey", "loverskey", "hajkey", "sharkey", "renekey", "renekey-lite", "yoiyami", "catnip", "cyberskey", "catodon", "lycheebridge"] or "shumihub" in software or "мисскей" in software or "milkey" in software or "misskey" in software:
65         logger.debug("Setting misskey: software='%s'", software)
66         software = "misskey"
67     elif software in ["runtube.re", "islameye"]:
68         logger.debug("Setting peertube: software='%s'", software)
69         software = "peertube"
70     elif software in ["nextcloud social", "nextcloudpi", "storage share", "nube"] or "nextcloud" in software:
71         logger.debug("Setting nextcloud: software='%s'", software)
72         software = "nextcloud"
73     elif "discourse" in software:
74         logger.debug("Setting discourse: software='%s'", software)
75         software = "discourse"
76     elif software == "activity-relay":
77         logger.debug("Setting activityrelay: software='%s'", software)
78         software = "activityrelay"
79     elif "owncast" in software:
80         logger.debug("Setting owncast: software='%s'", software)
81         software = "owncast"
82     elif software in ["streams-hubzilla-social", "streams13"]:
83         logger.debug("Setting streams: software='%s'", software)
84         software = "streams"
85     elif software == "roadhouse":
86         logger.debug("Setting hubzilla: software='%s'", software)
87         software = "hubzilla"
88     elif software == "takahē":
89         logger.debug("Setting takahe: software='%s'", software)
90         software = "takahe"
91     elif software == "diaspora* social network":
92         logger.debug("Setting diaspora: software='%s'", software)
93         software = "diaspora"
94     elif software == "tkz relay":
95         logger.debug("Setting aoderelay: software='%s'", software)
96         software = "aoderelay"
97     elif software == "gitdab":
98         logger.debug("Setting forgejo: software='%s'", software)
99         software = "forgejo"
100     elif software == "mbin":
101         logger.debug("Setting kbin: software='%s'", software)
102         software = "kbin"
103     elif software == "write.as":
104         logger.debug("Setting writefreely: software='%s'", software)
105         software = "writefreely"
106     elif "gnu social" in software:
107         logger.debug("Setting gnusocial: software='%s'", software)
108         software = "gnusocial"
109     elif software.find("/") > 0:
110         logger.warning("Spliting of slash: software='%s'", software)
111         software = software.split("/")[-1]
112     elif software.find("|") > 0:
113         logger.warning("Spliting of pipe: software='%s'", software)
114         software = software.split("|")[0]
115     elif "powered by" in software:
116         logger.debug("software='%s' has 'powered by' in it", software)
117         software = strip_powered_by(software)
118     elif software.endswith(" experimental"):
119         logger.debug("software='%s' ends with 'experimental", software)
120         software = strip_until(software, "experimental")
121
122     if isinstance(software, str) and " by " in software:
123         logger.debug("software='%s' has ' by ' in it", software)
124         software = strip_until(software, " by ")
125     elif isinstance(software, str) and " - " in software:
126         logger.debug("software='%s' has ' - ' in it", software)
127         software = strip_until(software, " - ")
128     elif isinstance(software, str) and " see " in software:
129         logger.debug("software='%s' has ' see ' in it", software)
130         software = strip_until(software, " see ")
131
132     logger.debug("software['%s']='%s'", type(software), software)
133     if software == "":
134         logger.warning("tidyup.domain() left no software name behind: software='%s'", software)
135         software = None
136     else:
137         logger.debug("software='%s' is being cleaned up further ...")
138         software = software.rstrip("!").strip()
139
140     # Set cache
141     _cache["alias"][key] = software
142
143     logger.debug("software[%s]='%s' - EXIT!", type(software), software)
144     return software
145
146 def strip_hosted_on(software: str) -> str:
147     logger.debug("software='%s' - CALLED!", software)
148
149     if not isinstance(software, str):
150         raise ValueError(f"Parameter software[]='{type(software)}' is not of type 'str'")
151     elif software == "":
152         raise ValueError("Parameter 'software' is empty")
153     elif "hosted on" not in software:
154         logger.warning("Cannot find 'hosted on' in software='%s'!", software)
155         return software
156
157     end = software.find("hosted on ")
158     logger.debug("end[%s]=%d", type(end), end)
159
160     software = software[0:end].strip()
161     logger.debug("software[%s]='%s'", type(software), software)
162
163     if " - " in software:
164         logger.debug("Stripping ' - ' of from software='%s' ...", software)
165         software = strip_until(software, " - ").strip()
166
167     logger.debug("software='%s' - EXIT!", software)
168     return software
169
170 def strip_powered_by(software: str) -> str:
171     logger.debug("software='%s' - CALLED!", software)
172
173     if not isinstance(software, str):
174         raise ValueError(f"Parameter software[]='{type(software)}' is not of type 'str'")
175     elif software == "":
176         raise ValueError("Parameter 'software' is empty")
177     elif "powered by" not in software:
178         logger.warning("Cannot find 'powered by' in software='%s'!", software)
179         return software
180
181     start = software.find("powered by ")
182     logger.debug("start[%s]=%d", type(start), start)
183
184     software = software[start + 11:].strip()
185     logger.debug("software='%s'", software)
186
187     if " - " in software:
188         logger.debug("Stripping ' - ' of from software='%s' ...", software)
189         software = strip_until(software, " - ").strip()
190
191     logger.debug("software='%s' - EXIT!", software)
192     return software
193
194 def strip_until(software: str, until: str) -> str:
195     logger.debug("software='%s',until='%s' - CALLED!", software, until)
196
197     if not isinstance(software, str):
198         raise ValueError(f"Parameter software[]='{type(software)}' is not of type 'str'")
199     elif software == "":
200         raise ValueError("Parameter 'software' is empty")
201     elif not isinstance(until, str):
202         raise ValueError(f"Parameter until[]='{type(until)}' is not of type 'str'")
203     elif until == "":
204         raise ValueError("Parameter 'until' is empty")
205     elif not until in software:
206         logger.warning("Cannot find until='%s' in software='%s'!", until, software)
207         return software
208
209     # Next, strip until part
210     end = software.strip().find(until)
211
212     logger.debug("end[%s]=%d", type(end), end)
213     if end > 0:
214         software = software[0:end].strip()
215
216     logger.debug("software='%s' - EXIT!", software)
217     return software
218
219 def is_relay(software: str) -> bool:
220     logger.debug("software='%s'- CALLED!", software)
221
222     if not isinstance(software, str):
223         raise ValueError(f"software[]='{type(software)}' is not type 'str'")
224     elif software == "":
225         raise ValueError("Parameter 'software' is empty")
226
227     found = software in relays
228
229     logger.debug("found='%s' - EXIT!", found)
230     return found