epicyon/socnet.py

89 lines
3.3 KiB
Python
Raw Normal View History

2020-07-08 12:28:41 +00:00
__filename__ = "socnet.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-07-08 12:28:41 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-07-08 12:28:41 +00:00
__status__ = "Production"
2021-06-26 11:16:41 +00:00
__module_group__ = "Moderation"
2020-07-08 12:28:41 +00:00
2021-12-28 16:56:57 +00:00
from session import create_session
2021-12-29 21:55:09 +00:00
from webfinger import webfinger_handle
from posts import get_person_box
from posts import get_post_domains
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2020-07-08 12:28:41 +00:00
2021-12-29 21:55:09 +00:00
def instances_graph(base_dir: str, handles: str,
proxy_type: str,
port: int, http_prefix: str,
debug: bool, project_version: str,
system_language: str, signing_priv_key_pem: str) -> str:
2020-07-08 12:28:41 +00:00
""" Returns a dot graph of federating instances
based upon a few sample handles.
The handles argument should contain a comma separated list
of handles on different instances
"""
dotGraphStr = 'digraph instances {\n'
if ',' not in handles:
return dotGraphStr + '}\n'
2021-12-28 16:56:57 +00:00
session = create_session(proxy_type)
2020-07-08 12:28:41 +00:00
if not session:
return dotGraphStr + '}\n'
2021-12-25 22:17:49 +00:00
person_cache = {}
2021-12-25 22:28:18 +00:00
cached_webfingers = {}
2021-12-25 23:45:30 +00:00
federation_list = []
2021-12-25 21:02:44 +00:00
max_mentions = 99
2021-12-25 21:04:51 +00:00
max_emoji = 99
2020-07-08 12:28:41 +00:00
maxAttachments = 5
personHandles = handles.split(',')
for handle in personHandles:
handle = handle.strip()
if handle.startswith('@'):
handle = handle[1:]
if '@' not in handle:
continue
nickname = handle.split('@')[0]
domain = handle.split('@')[1]
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2021-12-26 10:00:46 +00:00
handle = http_prefix + "://" + domain_full + "/@" + nickname
2020-07-08 12:28:41 +00:00
wfRequest = \
2021-12-29 21:55:09 +00:00
webfinger_handle(session, handle, http_prefix,
cached_webfingers,
domain, project_version, debug, False,
signing_priv_key_pem)
2020-07-08 12:28:41 +00:00
if not wfRequest:
return dotGraphStr + '}\n'
if not isinstance(wfRequest, dict):
print('Webfinger for ' + handle + ' did not return a dict. ' +
str(wfRequest))
return dotGraphStr + '}\n'
2021-09-15 14:05:08 +00:00
originDomain = None
(personUrl, pubKeyId, pubKey, personId, shaedInbox, avatarUrl,
2021-12-29 21:55:09 +00:00
displayName, _) = get_person_box(signing_priv_key_pem,
originDomain,
base_dir, session, wfRequest,
person_cache,
project_version, http_prefix,
nickname, domain, 'outbox',
27261)
wordFrequency = {}
2020-07-08 12:28:41 +00:00
postDomains = \
2021-12-29 21:55:09 +00:00
get_post_domains(session, personUrl, 64, max_mentions, max_emoji,
maxAttachments, federation_list,
person_cache, debug,
project_version, http_prefix, domain,
wordFrequency, [], system_language,
signing_priv_key_pem)
2020-07-08 12:28:41 +00:00
postDomains.sort()
for fedDomain in postDomains:
dotLineStr = ' "' + domain + '" -> "' + fedDomain + '";\n'
if dotLineStr not in dotGraphStr:
dotGraphStr += dotLineStr
return dotGraphStr + '}\n'