#!/usr/bin/env python3

import json
from html import unescape


def main():
    with open("/home/jas/downloads/mastodon-archive/outbox.json", "r") as jf:
        json_data = json.load(jf)

    print("#+TITLE: Mastodon posts archive from 2024-02-16 to 2025-01-31")
    print("#+DATE: 2025-02-02")
    print("#+TAGS[]: mastodon archives")
    print("#+AUTHOR: hyperreal")
    print("#+SLUG: mastodon_archive-20240216-20250131")
    print("#+LAYOUT: post")
    print()

    for item in sorted(
        json_data["orderedItems"], key=json_data["orderedItems"].index, reverse=True
    ):
        if type(item.get("object")) is dict:
            published = item.get("object").get("published")
            content = item.get("object").get("content")
            attachment = (
                item.get("object").get("attachment")
                if len(item.get("object").get("attachment")) >= 1
                else None
            )

            print(f"** {published}")
            print("#+BEGIN_EXPORT html")
            if type(content) is str:
                print(unescape(content))
            print("#+END_EXPORT")
            if attachment:
                for item in attachment:
                    if item.get("name"):
                        print(f"#+CAPTION: {item.get('name')}")
                    print(
                        f"[[https://files.hyperreal.coffee/mastodon_20240216-20250131/{item.get('url')}]]"
                    )
            print("-----")
            print()


if __name__ == "__main__":
    main()