Skip to content

Instantly share code, notes, and snippets.

@frankenstein91
Last active May 18, 2021 16:16
Show Gist options
  • Save frankenstein91/db501746c79c4dc979bff8d16c40c1b0 to your computer and use it in GitHub Desktop.
Save frankenstein91/db501746c79c4dc979bff8d16c40c1b0 to your computer and use it in GitHub Desktop.
lists all mentions in a toot thread
#! /bin/env python
import argparse, multiprocessing, urllib.request,validators
import json
import sys
from urllib.parse import urlparse
import os.path
if __name__ == '__main__':
usernames =[]
multiprocessing.freeze_support()
argsParser = argparse.ArgumentParser()
argsParser.add_argument("-u","--url",help="the url of the toot",default="https://social.tchncs.de/@lauteshirn/106221169014088972",type=str)
args = argsParser.parse_args()
if validators.url(args.url):
tootUrl = urlparse(args.url)
if tootUrl.port:
port = tootUrl.port
else:
if tootUrl.scheme == "https":
port = "443"
elif tootUrl.scheme == "http":
port = "80"
else:
sys.exit(1)
APIUrlString = tootUrl.scheme + "://" + tootUrl.hostname + ":" + port + "/api/v1/statuses/" + os.path.split(tootUrl.path)[-1] + "/context"
response = urllib.request.urlopen(urllib.request.Request(APIUrlString))
data = json.load(response)
if not data["ancestors"]:
pass
else:
APIUrlString = tootUrl.scheme + "://" + tootUrl.hostname + ":" + port + "/api/v1/statuses/" + data["ancestors"][0]["id"] + "/context"
response = urllib.request.urlopen(urllib.request.Request(APIUrlString))
data = json.load(response)
for toot in data["descendants"]:
if toot["mentions"]:
for mention in toot["mentions"]:
menData = urlparse(mention["url"])
username = menData.path.replace("/@", "@") + "@" + menData.hostname
usernames.append(username)
usernamesSorted = sorted(set(usernames))
print(" ".join(usernamesSorted))
else:
print("not an URL")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment