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
2020-12-16 11:38:40 +00:00
from utils import getFullDomain
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,
systemLanguage: str, signingPrivateKeyPem: 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'
personCache = {}
cachedWebfingers = {}
federationList = []
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]
2020-12-16 11:38:40 +00:00
domainFull = getFullDomain(domain, port)
2021-12-25 17:09:22 +00:00
handle = http_prefix + "://" + domainFull + "/@" + nickname
2020-07-08 12:28:41 +00:00
wfRequest = \
2021-12-25 17:09:22 +00:00
webfingerHandle(session, handle, http_prefix,
2020-07-08 12:28:41 +00:00
cachedWebfingers,
2021-12-25 20:34:38 +00:00
domain, project_version, debug, False,
signingPrivateKeyPem)
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,
displayName, _) = getPersonBox(signingPrivateKeyPem,
originDomain,
2021-12-25 16:17:53 +00:00
base_dir, session, wfRequest,
personCache,
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,
2020-07-08 12:28:41 +00:00
maxAttachments, federationList,
personCache, debug,
2021-12-25 20:34:38 +00:00
project_version, http_prefix, domain,
wordFrequency, [], systemLanguage,
signingPrivateKeyPem)
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'