Refresh toots script now graphs all toots via subsequent API calls

This commit is contained in:
Brandon Rozek 2022-05-15 11:14:34 -04:00
parent 5f570b07d2
commit 4a2fa9c5a0

View file

@ -10,6 +10,7 @@ from http.client import HTTPResponse
from pathlib import Path from pathlib import Path
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
import json import json
import math
import sys import sys
TOOT_CONTENT_LOCATION = "content/toots" TOOT_CONTENT_LOCATION = "content/toots"
@ -17,43 +18,58 @@ SERVER="https://fosstodon.org"
# Quick way to find user id: https://prouser123.me/mastodon-userid-lookup/ # Quick way to find user id: https://prouser123.me/mastodon-userid-lookup/
MUID=108219415927856966 MUID=108219415927856966
# Server default (when < 0) is 20 # Server default (when < 0) is 20
# max allowed is 40 RETRIEVE_NUM_TOOTS=1000
RETRIEVE_NUM_TOOTS=40 MAX_TOOTS_PER_QUERY=40 # Cannot change (server default)
MAX_TOOT_ID=-1
def retrieve_toots_from_server(): def retrieve_toots_from_server():
""" """
Grabs toots from Mastodon server Grabs toots from Mastodon server
""" """
# Grab toots from Mastodon global MAX_TOOT_ID
limit_param = "?limit=" + str(RETRIEVE_NUM_TOOTS) \ server_data = []
if RETRIEVE_NUM_TOOTS > 0 else ""
url = SERVER + "/api/v1/accounts/" + str(MUID) + "/statuses" + limit_param
response: Optional[HTTPResponse] = None
try: for _ in range(math.ceil(RETRIEVE_NUM_TOOTS // 40)):
response = request.urlopen(url) # Grab toots from Mastodon
except Exception: limit_param = "?limit=" + str(RETRIEVE_NUM_TOOTS) \
print("Unable to grab toots from Mastodon.") if RETRIEVE_NUM_TOOTS > 0 else "?"
max_id = "&max_id=" + str(MAX_TOOT_ID) \
if MAX_TOOT_ID > 0 else ""
url = SERVER + "/api/v1/accounts/" + str(MUID) + "/statuses" + limit_param + max_id
response: Optional[HTTPResponse] = None
if response is None: try:
sys.exit(-1) response = request.urlopen(url)
except Exception:
print("Unable to grab toots from Mastodon.")
# Parse server response if response is None:
server_data: Optional[list] = None sys.exit(-1)
try:
server_data = json.loads(response.read())
except Exception:
print("Malformed JSON response from server.")
if server_data is None: # Parse server response
sys.exit(-1) server_data_part: Optional[list] = None
try:
server_data_part = json.loads(response.read())
except Exception:
print("Malformed JSON response from server.")
if not isinstance(server_data, list): if server_data is None:
print("Unexpected JSON response, should be of form list.") sys.exit(-1)
sys.exit(-1)
print(f"Successfully grabbed {len(server_data)} toots from server") if not isinstance(server_data_part, list):
print("Unexpected JSON response, should be of form list.")
sys.exit(-1)
# No more to retrieve
if len(server_data_part) == 0:
break
print(f"Retrieved {len(server_data_part)} toots from server")
server_data.extend(server_data_part)
MAX_TOOT_ID = int(min(server_data_part, key=lambda p: int(p['id']))['id'])
print(f"Successfully grabbed a total of {len(server_data)} toots from server")
return server_data return server_data