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,18 +18,25 @@ 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
""" """
global MAX_TOOT_ID
server_data = []
for _ in range(math.ceil(RETRIEVE_NUM_TOOTS // 40)):
# Grab toots from Mastodon # Grab toots from Mastodon
limit_param = "?limit=" + str(RETRIEVE_NUM_TOOTS) \ limit_param = "?limit=" + str(RETRIEVE_NUM_TOOTS) \
if RETRIEVE_NUM_TOOTS > 0 else "" if RETRIEVE_NUM_TOOTS > 0 else "?"
url = SERVER + "/api/v1/accounts/" + str(MUID) + "/statuses" + limit_param 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 response: Optional[HTTPResponse] = None
try: try:
@ -40,20 +48,28 @@ def retrieve_toots_from_server():
sys.exit(-1) sys.exit(-1)
# Parse server response # Parse server response
server_data: Optional[list] = None server_data_part: Optional[list] = None
try: try:
server_data = json.loads(response.read()) server_data_part = json.loads(response.read())
except Exception: except Exception:
print("Malformed JSON response from server.") print("Malformed JSON response from server.")
if server_data is None: if server_data is None:
sys.exit(-1) sys.exit(-1)
if not isinstance(server_data, list): if not isinstance(server_data_part, list):
print("Unexpected JSON response, should be of form list.") 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") # 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