Added Thread viewer

pages
Terence Eden 2022-11-15 15:41:07 +00:00
rodzic 79db10c94c
commit 39052559b3
2 zmienionych plików z 47 dodań i 1 usunięć

Wyświetl plik

@ -4,7 +4,6 @@ from mastodon import Mastodon
import config
# Set up access
instance = "https://mastodon.example"
mastodon = Mastodon( api_base_url=config.instance, access_token=config.access_token )
# Get user's info

47
threads.py 100644
Wyświetl plik

@ -0,0 +1,47 @@
from datetime import datetime, timedelta
from mastodon import Mastodon
from treelib import Node, Tree
import config
# Status ID
# This is the ID from *your* instance
status_id = 109343943300929632
# Set up access
mastodon = Mastodon( api_base_url=config.instance, access_token=config.access_token )
# Get the status
status = mastodon.status(status_id)
# Get the conversation tree
# Documentation https://docs.joinmastodon.org/entities/context/
conversation = mastodon.status_context(status_id)
# If there are ancestors, that means we are only on a single branch.
# The 0th ancestor is the "root" of the conversation tree
if len(conversation["ancestors"]) > 0 :
status = conversation["ancestors"][0]
status_id = status["id"]
conversation = mastodon.status_context(status_id)
# Create a new tree
tree = Tree()
# Add the "root"
try :
tree.create_node(
str(status["created_at"]) + " " + status["account"]["username"] + " " + status["uri"], status["id"])
except :
print("Problem adding node to the tree")
# Add any subsequent replies
for status in conversation["descendants"] :
try :
tree.create_node(str(status["created_at"]) + " " + status["account"]["username"] + " " + status["uri"], status["id"], parent=status["in_reply_to_id"])
except :
# If a parent node is missing
print("Problem adding node to the tree")
# Display
tree.show()