4 Commits
Author SHA1 Message Date
scambier f90cc7d2a0 Removed logs and don't overwrite existing markdown files 2024-06-24 19:05:37 +02:00
scambier 1341dd2833 Update README.md 2024-06-24 09:09:39 +02:00
scambier 260b946f25 Update README.md 2024-06-23 22:33:28 +02:00
scambier 15ae3f241f Added server url in main args 2024-06-23 22:31:47 +02:00
3 changed files with 32 additions and 10 deletions
+6 -2
View File
@@ -1,5 +1,7 @@
# Mastodon Bookmarks Archive # Mastodon Bookmarks Archive
[Release download](https://git.scambier.xyz/scambier/janet-mastodon-bookmarks/releases)
_A small Janet script to archive all your Mastodon bookmarks in Markdown files._ _A small Janet script to archive all your Mastodon bookmarks in Markdown files._
Because Mastodon posts can disappear for a variety of reasons (deletion, defederation, server instance shutdown, ...), it is safer to archive your bookmarks. Because Mastodon posts can disappear for a variety of reasons (deletion, defederation, server instance shutdown, ...), it is safer to archive your bookmarks.
@@ -23,9 +25,11 @@ Most of these values will be in the form of `key:: value` for easy integration w
- Run the program - Run the program
```sh ```sh
$ ./mastodon-bookmarks YOUR_APP_TOKEN $ ./mastodon-bookmarks YOUR_INSTANCE YOUR_APP_TOKEN
``` ```
The `YOUR_INSTANCE` value must not have the trailing slash (e.g: `https://hachyderm.io`)
## Development ## Development
- Clone this repository - Clone this repository
@@ -33,7 +37,7 @@ $ ./mastodon-bookmarks YOUR_APP_TOKEN
- `jpm deps --local` - `jpm deps --local`
```sh ```sh
$ janet main.janet YOUR_APP_TOKEN $ janet main.janet YOUR_INSTANCE YOUR_APP_TOKEN
``` ```
## Build binary ## Build binary
+19 -6
View File
@@ -2,9 +2,11 @@
(import spork/json :as json) (import spork/json :as json)
(import ./utils :as utils) (import ./utils :as utils)
(var server "")
(var apptoken "") (var apptoken "")
(var dbg false) (var dbg false)
(def urls @["https://hachyderm.io/api/v1/bookmarks"])
(def urls @[])
(def save-path "./bookmarks/") (def save-path "./bookmarks/")
(def done @[]) (def done @[])
@@ -13,20 +15,27 @@
"Downloads the media from the item." "Downloads the media from the item."
(def files @[]) (def files @[])
(def media (item "media_attachments")) (def media (item "media_attachments"))
(pp (item "media_attachments")) (if dbg
(pp (item "media_attachments")))
(each m media (each m media
(def url (m "url")) (def url (m "url"))
(def folder (string save-path "attachments/")) (def folder (string save-path "attachments/"))
(array/push files {:path (string "./attachments/" (utils/get-image url folder)) (array/push files {:path (string "./attachments/" (utils/get-image url folder))
:description (m "description")})) :description (m "description")}))
(pp files) (if dbg (pp files))
files) files)
(defn write-markdown [item] (defn write-markdown [item]
(def files (download-media item)) (def files (download-media item))
(def id (item "id")) (def id (item "id"))
(def fout (file/open (string save-path id ".md") :w)) (def path (string save-path id ".md"))
(if (utils/exists? path)
(do
(print (string "Skipping " id))
(break)))
(def fout (file/open path :w))
(def inline (string (def inline (string
"mastodon-url:: " (item "url") " \n" "mastodon-url:: " (item "url") " \n"
@@ -96,8 +105,12 @@
(defn main [& args] (defn main [& args]
(set apptoken (or (os/getenv "TOKEN") (args 1))) (set server (args 1))
(set dbg (get args 2)) (set apptoken (args 2))
(set dbg (get args 3))
(array/push urls (string server "/api/v1/bookmarks"))
(os/mkdir "./bookmarks") (os/mkdir "./bookmarks")
(os/mkdir "./bookmarks/attachments") (os/mkdir "./bookmarks/attachments")
(download-bookmarks)) (download-bookmarks))
+5
View File
@@ -1,5 +1,10 @@
(import httprequest :as r) (import httprequest :as r)
(def exists? (fn [path]
"Returns whether a file exists at the specified path."
(def f (file/open path :r))
(if f (do (file/close f) true) false)))
(defn get-image [url folder &named headers opts] (defn get-image [url folder &named headers opts]
"Download an image from a URL and save it to a file. Returns the path to the saved file." "Download an image from a URL and save it to a file. Returns the path to the saved file."
(default headers {}) (default headers {})