epicyon/socnet.py

89 lines
3.2 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
from session import createSession
from webfinger import webfingerHandle
from posts import getPersonBox
from posts import getPostDomains
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2020-07-08 12:28:41 +00:00
2021-12-25 16:17:53 +00:00
def instancesGraph(base_dir: str, handles: str,
2021-12-25 21:09:22 +00:00
proxy_type: str,
2021-12-25 17:09:22 +00:00
port: int, http_prefix: str,
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str,
2021-12-25 23:03:28 +00:00
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-25 21:09:22 +00:00
session = createSession(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-25 17:09:22 +00:00
webfingerHandle(session, handle, http_prefix,
2021-12-25 22:28:18 +00:00
cached_webfingers,
2021-12-25 20:34:38 +00:00
domain, project_version, debug, False,
2021-12-25 23:03:28 +00:00
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-25 23:03:28 +00:00
displayName, _) = getPersonBox(signing_priv_key_pem,
originDomain,
2021-12-25 16:17:53 +00:00
base_dir, session, wfRequest,
2021-12-25 22:17:49 +00:00
person_cache,
2021-12-25 20:34:38 +00:00
project_version, http_prefix,
nickname, domain, 'outbox',
27261)
wordFrequency = {}
2020-07-08 12:28:41 +00:00
postDomains = \
2021-12-25 21:04:51 +00:00
getPostDomains(session, personUrl, 64, max_mentions, max_emoji,
2021-12-25 23:45:30 +00:00
maxAttachments, federation_list,
2021-12-25 22:17:49 +00:00
person_cache, debug,
2021-12-25 20:34:38 +00:00
project_version, http_prefix, domain,
2021-12-25 23:03:28 +00:00
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'