Snake case

main
Bob Mottram 2022-01-02 22:49:32 +00:00
parent c9d5ed8091
commit 551f24706b
1 changed files with 101 additions and 100 deletions

View File

@ -17,16 +17,16 @@ from metadata import meta_data_instance
def _get_mast_api_v1id(path: str) -> int: def _get_mast_api_v1id(path: str) -> int:
"""Extracts the mastodon Id number from the given path """Extracts the mastodon Id number from the given path
""" """
mastoId = None masto_id = None
idPath = '/api/v1/accounts/:' id_path = '/api/v1/accounts/:'
if not path.startswith(idPath): if not path.startswith(id_path):
return None return None
mastoIdStr = path.replace(idPath, '') masto_id_str = path.replace(id_path, '')
if '/' in mastoIdStr: if '/' in masto_id_str:
mastoIdStr = mastoIdStr.split('/')[0] masto_id_str = masto_id_str.split('/')[0]
if mastoIdStr.isdigit(): if masto_id_str.isdigit():
mastoId = int(mastoIdStr) masto_id = int(masto_id_str)
return mastoId return masto_id
return None return None
@ -37,16 +37,17 @@ def get_masto_api_v1id_from_nickname(nickname: str) -> int:
def _int_to_bytes(num: int) -> str: def _int_to_bytes(num: int) -> str:
"""Integer conversion
"""
if num == 0: if num == 0:
return b"" return b""
else: return _int_to_bytes(num // 256) + bytes([num % 256])
return _int_to_bytes(num // 256) + bytes([num % 256])
def get_nickname_from_masto_api_v1id(mastoId: int) -> str: def get_nickname_from_masto_api_v1id(masto_id: int) -> str:
"""Given the mastodon Id return the nickname """Given the mastodon Id return the nickname
""" """
nickname = _int_to_bytes(mastoId).decode() nickname = _int_to_bytes(masto_id).decode()
return nickname[::-1] return nickname[::-1]
@ -55,34 +56,34 @@ def _get_masto_api_v1account(base_dir: str, nickname: str, domain: str) -> {}:
blob/master/Using-the-API/API.md#account blob/master/Using-the-API/API.md#account
Authorization has already been performed Authorization has already been performed
""" """
accountFilename = acct_dir(base_dir, nickname, domain) + '.json' account_filename = acct_dir(base_dir, nickname, domain) + '.json'
if not os.path.isfile(accountFilename): if not os.path.isfile(account_filename):
return {} return {}
accountJson = load_json(accountFilename) account_json = load_json(account_filename)
if not accountJson: if not account_json:
return {} return {}
mastoAccountJson = { masto_account_json = {
"id": get_masto_api_v1id_from_nickname(nickname), "id": get_masto_api_v1id_from_nickname(nickname),
"username": nickname, "username": nickname,
"acct": nickname, "acct": nickname,
"display_name": accountJson['name'], "display_name": account_json['name'],
"locked": accountJson['manuallyApprovesFollowers'], "locked": account_json['manuallyApprovesFollowers'],
"created_at": "2016-10-05T10:30:00Z", "created_at": "2016-10-05T10:30:00Z",
"followers_count": 0, "followers_count": 0,
"following_count": 0, "following_count": 0,
"statuses_count": 0, "statuses_count": 0,
"note": accountJson['summary'], "note": account_json['summary'],
"url": accountJson['id'], "url": account_json['id'],
"avatar": accountJson['icon']['url'], "avatar": account_json['icon']['url'],
"avatar_static": accountJson['icon']['url'], "avatar_static": account_json['icon']['url'],
"header": accountJson['image']['url'], "header": account_json['image']['url'],
"header_static": accountJson['image']['url'] "header_static": account_json['image']['url']
} }
return mastoAccountJson return masto_account_json
def masto_api_v1_response(path: str, calling_domain: str, def masto_api_v1_response(path: str, calling_domain: str,
uaStr: str, ua_str: str,
authorized: bool, authorized: bool,
http_prefix: str, http_prefix: str,
base_dir: str, nickname: str, domain: str, base_dir: str, nickname: str, domain: str,
@ -99,27 +100,27 @@ def masto_api_v1_response(path: str, calling_domain: str,
of returning an empty result to sites like of returning an empty result to sites like
https://mastopeek.app-dist.eu https://mastopeek.app-dist.eu
""" """
sendJson = None send_json = None
sendJsonStr = '' send_json_str = ''
if not uaStr: if not ua_str:
uaStr = '' ua_str = ''
# parts of the api needing authorization # parts of the api needing authorization
if authorized and nickname: if authorized and nickname:
if path == '/api/v1/accounts/verify_credentials': if path == '/api/v1/accounts/verify_credentials':
sendJson = _get_masto_api_v1account(base_dir, nickname, domain) send_json = _get_masto_api_v1account(base_dir, nickname, domain)
sendJsonStr = \ send_json_str = \
'masto API account sent for ' + nickname + ' ' + uaStr 'masto API account sent for ' + nickname + ' ' + ua_str
# information about where the request is coming from # information about where the request is coming from
callingInfo = ' ' + uaStr + ', ' + calling_domain calling_info = ' ' + ua_str + ', ' + calling_domain
# Parts of the api which don't need authorization # Parts of the api which don't need authorization
mastoId = _get_mast_api_v1id(path) masto_id = _get_mast_api_v1id(path)
if mastoId is not None: if masto_id is not None:
pathNickname = get_nickname_from_masto_api_v1id(mastoId) path_nickname = get_nickname_from_masto_api_v1id(masto_id)
if pathNickname: if path_nickname:
originalPath = path original_path = path
if '/followers?' in path or \ if '/followers?' in path or \
'/following?' in path or \ '/following?' in path or \
'/search?' in path or \ '/search?' in path or \
@ -127,85 +128,85 @@ def masto_api_v1_response(path: str, calling_domain: str,
'/statuses?' in path: '/statuses?' in path:
path = path.split('?')[0] path = path.split('?')[0]
if path.endswith('/followers'): if path.endswith('/followers'):
sendJson = [] send_json = []
sendJsonStr = \ send_json_str = \
'masto API followers sent for ' + nickname + \ 'masto API followers sent for ' + nickname + \
callingInfo calling_info
elif path.endswith('/following'): elif path.endswith('/following'):
sendJson = [] send_json = []
sendJsonStr = \ send_json_str = \
'masto API following sent for ' + nickname + \ 'masto API following sent for ' + nickname + \
callingInfo calling_info
elif path.endswith('/statuses'): elif path.endswith('/statuses'):
sendJson = [] send_json = []
sendJsonStr = \ send_json_str = \
'masto API statuses sent for ' + nickname + \ 'masto API statuses sent for ' + nickname + \
callingInfo calling_info
elif path.endswith('/search'): elif path.endswith('/search'):
sendJson = [] send_json = []
sendJsonStr = \ send_json_str = \
'masto API search sent ' + originalPath + \ 'masto API search sent ' + original_path + \
callingInfo calling_info
elif path.endswith('/relationships'): elif path.endswith('/relationships'):
sendJson = [] send_json = []
sendJsonStr = \ send_json_str = \
'masto API relationships sent ' + originalPath + \ 'masto API relationships sent ' + original_path + \
callingInfo calling_info
else: else:
sendJson = \ send_json = \
_get_masto_api_v1account(base_dir, pathNickname, domain) _get_masto_api_v1account(base_dir, path_nickname, domain)
sendJsonStr = \ send_json_str = \
'masto API account sent for ' + nickname + \ 'masto API account sent for ' + nickname + \
callingInfo calling_info
# NOTE: adding support for '/api/v1/directory seems to create # NOTE: adding support for '/api/v1/directory seems to create
# federation problems, so avoid implementing that # federation problems, so avoid implementing that
if path.startswith('/api/v1/blocks'): if path.startswith('/api/v1/blocks'):
sendJson = [] send_json = []
sendJsonStr = \ send_json_str = \
'masto API instance blocks sent ' + path + callingInfo 'masto API instance blocks sent ' + path + calling_info
elif path.startswith('/api/v1/favorites'): elif path.startswith('/api/v1/favorites'):
sendJson = [] send_json = []
sendJsonStr = 'masto API favorites sent ' + path + callingInfo send_json_str = 'masto API favorites sent ' + path + calling_info
elif path.startswith('/api/v1/follow_requests'): elif path.startswith('/api/v1/follow_requests'):
sendJson = [] send_json = []
sendJsonStr = \ send_json_str = \
'masto API follow requests sent ' + path + callingInfo 'masto API follow requests sent ' + path + calling_info
elif path.startswith('/api/v1/mutes'): elif path.startswith('/api/v1/mutes'):
sendJson = [] send_json = []
sendJsonStr = \ send_json_str = \
'masto API mutes sent ' + path + callingInfo 'masto API mutes sent ' + path + calling_info
elif path.startswith('/api/v1/notifications'): elif path.startswith('/api/v1/notifications'):
sendJson = [] send_json = []
sendJsonStr = \ send_json_str = \
'masto API notifications sent ' + path + callingInfo 'masto API notifications sent ' + path + calling_info
elif path.startswith('/api/v1/reports'): elif path.startswith('/api/v1/reports'):
sendJson = [] send_json = []
sendJsonStr = 'masto API reports sent ' + path + callingInfo send_json_str = 'masto API reports sent ' + path + calling_info
elif path.startswith('/api/v1/statuses'): elif path.startswith('/api/v1/statuses'):
sendJson = [] send_json = []
sendJsonStr = 'masto API statuses sent ' + path + callingInfo send_json_str = 'masto API statuses sent ' + path + calling_info
elif path.startswith('/api/v1/timelines'): elif path.startswith('/api/v1/timelines'):
sendJson = { send_json = {
'error': 'This method requires an authenticated user' 'error': 'This method requires an authenticated user'
} }
sendJsonStr = 'masto API timelines sent ' + path + callingInfo send_json_str = 'masto API timelines sent ' + path + calling_info
elif path.startswith('/api/v1/custom_emojis'): elif path.startswith('/api/v1/custom_emojis'):
sendJson = custom_emoji send_json = custom_emoji
sendJsonStr = \ send_json_str = \
'masto API custom emojis sent ' + path + callingInfo 'masto API custom emojis sent ' + path + calling_info
admin_nickname = get_config_param(base_dir, 'admin') admin_nickname = get_config_param(base_dir, 'admin')
if admin_nickname and path == '/api/v1/instance': if admin_nickname and path == '/api/v1/instance':
instanceDescriptionShort = \ instance_description_short = \
get_config_param(base_dir, 'instanceDescriptionShort') get_config_param(base_dir, 'instanceDescriptionShort')
if not instanceDescriptionShort: if not instance_description_short:
instanceDescriptionShort = \ instance_description_short = \
translate['Yet another Epicyon Instance'] translate['Yet another Epicyon Instance']
instanceDescription = \ instance_description = \
get_config_param(base_dir, 'instanceDescription') get_config_param(base_dir, 'instanceDescription')
instanceTitle = get_config_param(base_dir, 'instanceTitle') instance_title = get_config_param(base_dir, 'instanceTitle')
if calling_domain.endswith('.onion') and onion_domain: if calling_domain.endswith('.onion') and onion_domain:
domain_full = onion_domain domain_full = onion_domain
@ -217,11 +218,11 @@ def masto_api_v1_response(path: str, calling_domain: str,
if broch_mode: if broch_mode:
show_node_info_accounts = False show_node_info_accounts = False
sendJson = \ send_json = \
meta_data_instance(show_node_info_accounts, meta_data_instance(show_node_info_accounts,
instanceTitle, instance_title,
instanceDescriptionShort, instance_description_short,
instanceDescription, instance_description,
http_prefix, http_prefix,
base_dir, base_dir,
admin_nickname, admin_nickname,
@ -230,16 +231,16 @@ def masto_api_v1_response(path: str, calling_domain: str,
registration, registration,
system_language, system_language,
project_version) project_version)
sendJsonStr = 'masto API instance metadata sent ' + uaStr send_json_str = 'masto API instance metadata sent ' + ua_str
elif path.startswith('/api/v1/instance/peers'): elif path.startswith('/api/v1/instance/peers'):
# This is just a dummy result. # This is just a dummy result.
# Showing the full list of peers would have privacy implications. # Showing the full list of peers would have privacy implications.
# On a large instance you are somewhat lost in the crowd, but on # On a large instance you are somewhat lost in the crowd, but on
# small instances a full list of peers would convey a lot of # small instances a full list of peers would convey a lot of
# information about the interests of a small number of accounts # information about the interests of a small number of accounts
sendJson = ['mastodon.social', domain_full] send_json = ['mastodon.social', domain_full]
sendJsonStr = 'masto API peers metadata sent ' + uaStr send_json_str = 'masto API peers metadata sent ' + ua_str
elif path.startswith('/api/v1/instance/activity'): elif path.startswith('/api/v1/instance/activity'):
sendJson = [] send_json = []
sendJsonStr = 'masto API activity metadata sent ' + uaStr send_json_str = 'masto API activity metadata sent ' + ua_str
return sendJson, sendJsonStr return send_json, send_json_str