Created
January 6, 2025 15:25
-
-
Save s-leroux/1ccf614b0e5f23a901884ef95b4f4dd0 to your computer and use it in GitHub Desktop.
Quick and dirty script to download messages from a Flarum forum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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