Skip to content

Instantly share code, notes, and snippets.

@s-leroux
Created January 6, 2025 15:25
Show Gist options
  • Save s-leroux/1ccf614b0e5f23a901884ef95b4f4dd0 to your computer and use it in GitHub Desktop.
Save s-leroux/1ccf614b0e5f23a901884ef95b4f4dd0 to your computer and use it in GitHub Desktop.
Quick and dirty script to download messages from a Flarum forum
import sys
from pprint import pprint
import requests
from html import escape
DISCUSSION=24
FIRST_COMMENT=700
def download(discution, first):
users = {}
result = {}
next = first
fails = 0
while True:
curr = next
print(f"Post {curr}", file=sys.stderr)
r = requests.get(f"https://forum.realtoken.community/api/posts?filter[id]={curr}")
r.raise_for_status() # Lazy me
json = r.json()
records = json["data"]
if not records:
fails += 1
if fails > 10: # FIXME Heuristic to detect last message :(
break
next += 1
continue
fails = 0
included = json["included"]
for user in included:
if user["type"] != "users":
continue
attributes = user["attributes"]
users[user["id"]] = attributes["displayName"]
for record in records:
post = {}
next += 1
attributes = record["attributes"]
if attributes["contentType"] != "comment":
continue
relationships = record["relationships"]
if relationships["discussion"]["data"]["id"] != str(DISCUSSION):
continue
post["id"] = record["id"]
post["author"] = users[relationships['user']['data']['id']]
post["html"] = attributes["contentHtml"]
result[curr] = post
return result
def main():
messages = download(DISCUSSION, FIRST_COMMENT)
html = "<html><body>\n"
for message in messages.values():
html += "<blockquote><h3>"
html += escape(f"Message {message['id']} par {message['author']}")
html += "</h3>"
html += message["html"]
html += "</blockquote>"
html += "</body></html>"
print(html)
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment