commit 1858d883f62ce1697021a7466ca559f400f4f16b Author: arne Date: Fri Jan 5 11:49:16 2024 +0100 It works :) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ee16a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,71 @@ +# Created by https://www.toptal.com/developers/gitignore/api/clojure,emacs +# Edit at https://www.toptal.com/developers/gitignore?templates=clojure,emacs + +### Clojure ### +pom.xml +pom.xml.asc +*.jar +*.class +/lib/ +/classes/ +/target/ +/checkouts/ +.lein-deps-sum +.lein-repl-history +.lein-plugins/ +.lein-failures +.nrepl-port +.cpcache/ + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# network security +/network-security.data + + +# End of https://www.toptal.com/developers/gitignore/api/clojure,emacs diff --git a/bb.edn b/bb.edn new file mode 100644 index 0000000..5837a2a --- /dev/null +++ b/bb.edn @@ -0,0 +1 @@ +{:paths ["."]} diff --git a/memento.clj b/memento.clj new file mode 100644 index 0000000..bdfe0ff --- /dev/null +++ b/memento.clj @@ -0,0 +1,76 @@ +(ns memento + (:require [cheshire.core :as json] + [org.httpkit.client :as http] + [clojure.string :as str]) + (:import [java.time LocalDateTime Instant ZoneId Duration] + [java.net URLEncoder])) + +(def acct (System/getenv "MASTODON_ACCOUNT")) +(def auth-token (System/getenv "MASTODON_AUTH_TOKEN")) + +(def assert-msg "Please set the MASTODON_ACCOUNT, MASTODON_AUTH_TOKEN and MEMENTO_ACCOUNT env vars.") +(assert (not (empty? acct)) assert-msg) +(assert (not (empty? auth-token)) assert-msg) + +(def instance-url (format "https://%s" (last (str/split acct #"@")))) + +(defn mastodon-api [path params] + (-> + @(http/request + (merge-with + #(if (map? %1) (merge %1 %2) %2) + {:url (format "%s/%s" instance-url path) + :method :get + :headers {"Authorization" (format "Bearer %s" auth-token)}} + params)) + (update :body #(json/parse-string % true)))) + +(defn prn-res [res] + (prn (assoc-in res [:opts :headers "Authorization"] "Bearer XXX"))) + +(println "Requesting account details for" acct) + +(def account-details + (mastodon-api "api/v1/accounts/lookup" {:query-params {:acct acct}})) + +(prn-res account-details) + +(def cutoff (.. (Instant/now) (minus (Duration/ofDays 1)))) + +(println "cutoff" cutoff) + +(def delete-xf + (comp (filter #(. (Instant/parse (:created_at %)) isBefore cutoff)) + (remove #(= (:visibility %) "direct")))) + +(def to-delete + (-> + (mastodon-api (format "api/v1/accounts/%s/statuses" (-> account-details :body :id)) + {:query-params {:tagged "today" + :exclude_reblogs true}}) + (update :body #(into [] delete-xf %)))) + +(defn sleep [ms] + (future (Thread/sleep ms))) + +(doseq [status (:body to-delete)] + (try + (print "Deleting post ") + (prn (select-keys status [:created_at :url])) + (let [deleted (mastodon-api (format "api/v1/statuses/%s" (:id status)) {:method :delete})] + (print "Deleted ") + (prn-res deleted) + @(sleep 2500) ; we need to wait, otherwise media_ids are still "attached" and can't be reused + (print "Recreating ") + (prn-res (mastodon-api + (format "api/v1/statuses" #_(:id refiled)) + {:method :post + :headers {"Content-Type" "application/json"} + :body (json/encode + {:visibility "direct" + :media_ids (map :id (-> deleted :body :media_attachments)) + :status (-> deleted :body :text)})}))) + (catch Exception e + (println "Error deleting " (:url status)) + (throw e)))) +