refactor: extrace get_descendants to function

This commit is contained in:
Ayo Ayco 2024-04-20 07:53:35 +02:00
parent 7e6c9d5e77
commit b20cb6b48e

20
app.py
View file

@ -11,19 +11,23 @@ def home():
threads = []
for id in thread_ids:
status = requests.get(server + '/api/v1/statuses/' + id ).json()
author = status['account']['id']
context = requests.get(server + '/api/v1/statuses/' + id + '/context').json()
descendants = []
for reply in context['descendants']:
if reply['account']['id'] == author and reply['in_reply_to_account_id'] == author:
descendants.append(clean_status(reply))
status = clean_status(status)
status['descendants'] = descendants
status['descendants'] = get_descendants(server, status)
threads.append(status)
return threads
def clean_status(status):
return {k: status[k] for k in {'content', 'created_at', 'url', 'media_attachments', 'card'}}
return {k: status[k] for k in ['id', 'account', 'content', 'created_at', 'url', 'media_attachments', 'card']}
def get_descendants(server, status):
author_id = status['account']['id']
context = requests.get(server + '/api/v1/statuses/' + status['id'] + '/context').json()
descendants = []
for reply in context['descendants']:
if reply['account']['id'] == author_id and reply['in_reply_to_account_id'] == author_id:
descendants.append(clean_status(reply))
return descendants
if __name__ == '__main__':
app.run(host='0.0.0.0')