Last active
January 11, 2023 14:10
-
-
Save jahzielv/6fe6b229e707a83e29944ec2f4e461b3 to your computer and use it in GitHub Desktop.
A small script for quickly posting to micro.blog + mastodon from your shell.
This file contains 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
#!/bin/sh | |
# micro.sh: a small program for posting to micro.blog and/or mastodon. Probably poorly written shell, use at your own risk! | |
# Works well enough for me to use it as a daily driver for quickly posting to my micro.blog and mastodon account. | |
# Expected env vars | |
# - $MICRO_BLOG_API_KEY | |
# - $MASTODON_URL (should be of form https://{YOUR_MASTODON_DOMAIN_HERE}/api/v1) | |
# - $MASTODON_ACCESS_TOKEN | |
# | |
# Dependencies: | |
# - jq | |
# - https://stedolan.github.io/jq/ | |
# - `brew install jq` | |
# - gum | |
# - https://github.com/charmbracelet/gum | |
# - `brew install gum` | `go install github.com/charmbracelet/gum@latest` | |
CANCEL="cancel" | |
EDIT="edit" | |
AUTH_HEADER="Authorization: Bearer $MICRO_BLOG_API_KEY" | |
# mb_get data from micro.blog with cURL | |
mb_get () { | |
curl -s --header "$AUTH_HEADER" $1; | |
} | |
for i in "$@" ; do | |
if [[ $i == "e" ]] ; then | |
POST_URL=$(mb_get "https://micro.blog/micropub?q=source" | jq '.items[].properties.url[0]' | gum choose) | |
POST_URL=$(echo $POST_URL | tr -d '"') | |
echo $POST_URL | |
UPDATE=$(mb_get "https://micro.blog/micropub?q=source&url=$POST_URL" | jq '.properties.content[0]' | tr -d '"' | sed 's/\\n/\n/g' | gum write) | |
if ( $(gum confirm "Update post?") ) ; then | |
echo "Post updated" | |
fi | |
elif [[ $i == "l" ]] ; then | |
POST_URL=$(mb_get "https://micro.blog/micropub?q=source&limit=10" | jq '.items[].properties.url[0]' | tr -d '""' | gum choose) | |
mb_get "https://micro.blog/micropub?q=source&url=$POST_URL" | jq '.properties.content[0]' | xargs printf | gum pager -- | |
elif [[ $i == "r" ]] ; then | |
# TODO: figure out why this doesn't work... post is created and has @jahziel, but isn't a reply to the in-reply-to on micro.blog | |
POST_URL=$(mb_get "https://micro.blog/micropub?q=source&limit=10" | jq '.items[].properties.url[0]' | tr -d '""' | gum choose) | |
TEXT=$(gum write --width 50 --base.border double) | |
RESP_JSON=$(curl -X POST --header "$AUTH_HEADER" "https://micro.blog/micropub?in-reply-to=$POST_URL" --data-urlencode "h=entry" --data-urlencode "content=$TEXT") | |
elif [[ $i == "-h" ]] ; then | |
echo "micro.sh: a small CLI for micro.blog" | |
echo | |
echo "usage:" | |
echo " mb\t#opens a text field for you to write a new post. submit your post with ^D" | |
echo " mb [command]" | |
echo | |
echo "available commands:" | |
echo " l: list out your micro.blog posts and view the one you select" | |
echo " e: select a post and edit it" | |
echo " r: select a post and reply to it" | |
echo " m: post only to your mastodon account. Make sure $MASTODON_ACCESS_TOKEN is exported with your access token!" | |
elif [[ $i == "m" ]] ; then | |
TEXT=$(gum write --width 50 --base.border double) | |
RESP_JSON=$(curl -s -X POST "$MASTODON_URL/statuses?access_token=$MASTODON_ACCESS_TOKEN" --data-urlencode "status=$TEXT") | |
echo $RESP_JSON | jq .url | |
exit 0 | |
fi | |
exit 0 | |
done | |
TEXT=$(gum write --width 50 --base.border double) | |
RESP_JSON=$(curl -s -X POST --header "$AUTH_HEADER" https://micro.blog/micropub --data-urlencode "h=entry" --data-urlencode "content=$TEXT") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment