Skip to content

Instantly share code, notes, and snippets.

@pstef
Created March 11, 2023 10:36
Show Gist options
  • Save pstef/b77a4b7103ac7210731db366f056a803 to your computer and use it in GitHub Desktop.
Save pstef/b77a4b7103ac7210731db366f056a803 to your computer and use it in GitHub Desktop.
Generate plain text mastodon threads
#!/usr/bin/env python
# https://codeberg.org/edent/Mastodon_Tools/src/commit/b284814810db1aaa5b0dfe98afa668f8d35eb525/threads.py
from mastodon import Mastodon
from treelib import Node, Tree
from datetime import datetime, timedelta
from urllib.parse import urlparse
import argparse
from bs4 import BeautifulSoup
# Take a command line argument. e.g. python3 threads.py https://mastodon.example/@me@somewhere/1234
parser = argparse.ArgumentParser(description='Display a tree based on a Mastodon conversation.')
parser.add_argument('url', metavar='URL', type=str, help='A URL of a post on your instance of Mastodon')
args = parser.parse_args()
url = urlparse(args.url)
path_id = url.path.split("/")[-1]
if path_id.isdigit():
status_id = int(path_id)
else:
print("Hmmm... That doesn't look like a valid Mastodon Status URL.\n" +
"It should look like https://mastodon.example/@me@somewhere/1234\n" +
"The last part of the URL must be the status ID, which is a number."
)
exit()
mastodon = Mastodon(api_base_url=url.scheme+'://'+url.netloc)
root = mastodon.status(status_id)
# 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:
root = conversation["ancestors"][0]
conversation = mastodon.status_context(root["id"])
tree = Tree()
# Add any subsequent replies
for status in [root] + conversation["descendants"]:
try:
tree.create_node(status["account"]["username"] + ": " + BeautifulSoup(status["content"], features="html.parser").get_text(), status["id"], parent=status["in_reply_to_id"])
except:
print("Problem adding node to the tree")
tree.show()
@pstef
Copy link
Author

pstef commented Nov 21, 2023

$ fgrep masto .shrc
masto(){ ~/threads.py "$@" | less --redraw-on-quit; }
$ masto https://mastodon.social/@Mastodon/111166379692730026
Mastodon: The 2022 Annual Report is now available for download! 2022 was an interesting year for #Mastodon to say the least.https://blog.joinmastodon.org/2023/10/annual-report-2022/
├── AJCxZ0: @Mastodon Fascinating report. Thank you for sharing this with us.I notice that> Set up a US non-profit branch (501(c)(3)) ..."is rendered as> Set up a US non-profit branch (501©(3)) ...in three of three different PDF viewers tested.
├── ErikUden: @Mastodon wow! Especially. The ending. Must've been wild to see so many leave Twitter and join Mastodon during the mass exodus. The Fediverse :fediverse: feels entirely different since then, you've done a great job!
├── JLuisNieves: @Mastodon Excellent 👌😲👍‼️
├── M0YNG: @Mastodon can I please ask for this as a webpage suitable for reading on electronic devices rather than a fancy PDF designed for print?
│   └── zorangrbic: @M0YNG @Mastodon eh, what? A PDF reader doesn't work for you?
│       └── M0YNG: @zorangrbic @Mastodon obviously I can use a PDF viewer, Firefox even does it for me.But you must agree that zooming in and dragging it around to read columns of text then zooming out to find the next bit then dragging around then zooming in again and then out a bit then in more and dragging around then zooming out etc. Is not as easy as if the text was in a blog post
│           └── zorangrbic: @M0YNG @Mastodon Ah, ok, got it. I understand the issue. I'm sorry, I initially took Adobe's word for it that PDF should render the same way, never mind where you read it.It seems that is not the case. Bummer for a portable file format. ;-)
├── Torrone: @AugierLe42e ⬆  C'était de ça dont je te parlais je ne sais plus quand.Les rémunérations sont en page 11
├── bison: @Mastodon the true cost of a social network, thank you. for everything. :BlobhajHeart: :BlobhajTinyHeart:
├── bufalo1973: @Mastodon I think the 2022 vs 2023 will be very fun.
├── fantinel: @Mastodon I just wanted to say that I absolutely adore the Mastodon brand identity. Makes me wonder how would the web app look if it was applied there…
│   └── f4grx: @fantinel @Mastodon error. we dont want brands on the fediverse.
├── honbra: @Mastodon I love the income charts. The Twitter migration had a measurable, positive (at least financially) impact on Mastodon, and probably the Fediverse in general.
│   └── andypiper: @honbra yes, but note the drop right afterwards - the hope is that we can continue to see healthy financial contributions!
│       └── danielauer: @andypiper @honbra According to the report, most of the donations are annual Patreon memberships. Which means the donations didn’t go down the next month, people just prepaid for the next 12 month.
├── kungkungblabak: @Mastodon :pika:
├── pallenberg: @Mastodon what a mission, what a vision. You guys rock, also when it comes to transparency
├── poag: @Mastodon Slide 9 is interesting. Although mastodon.social got the most single server subscribers. Most new users were not on .social and spread throughout the network. Ace :)I remember at the time there were concerns about .social being the defacto location for all new arrivals.
├── tecolote: @Mastodon Great job Mastodon team! Glad to be part of this network. Keep going! :mastodon:
└── vanecx: @Mastodon is there any possibility to know the detail of the expenses?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment