Last active
October 13, 2023 05:32
-
-
Save delan/03cb9009f466e2560fed56ad80943fda to your computer and use it in GitHub Desktop.
cohost image uploader
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
#!/usr/bin/env zsh | |
# cohost image uploader v3 | |
# requirements: linux + zsh + imagemagick + curl + jq | |
# (you don’t need to be a zsh user) | |
# limitations: does not actually attach the image to the post | |
set -euo pipefail | |
if [ $# -lt 3 ]; then | |
>&2 echo "usage: $0 <projectHandle> <postId> <file> [file ...]" | |
exit 1 | |
fi | |
if [ "${COHOST_COOKIE+x}" = "" ]; then | |
>&2 echo 'COHOST_COOKIE not set!' | |
>&2 echo 'to avoid this prompt next time: read -r COHOST_COOKIE; export COHOST_COOKIE' | |
>&2 printf \%s 'paste your cohost cookie (connect.sid=... with % encoding): ' | |
read -r COHOST_COOKIE | |
fi | |
case "$COHOST_COOKIE" in | |
(connect.sid=*) COHOST_COOKIE="$COHOST_COOKIE" ;; | |
(*) COHOST_COOKIE="connect.sid=$COHOST_COOKIE" ;; | |
esac | |
projectHandle=$1; shift | |
postId=$1; shift | |
json=$(mktemp) | |
for file; do | |
basename -- "$file" | read -r basename | |
file --mime-type "$file" | sed 's/.*: //' | read -r contentType | |
stat -c \%s -- "$file" | read -r contentLength # FIXME doesn’t work on bsd | |
identify "$file" | sed -E 's/.* ([^ ]+)( [^ ]+){6}/\1/;s/x/ /' | read -r width height | |
>&2 echo "step 1/3 (posts.attachment.start)" | |
curl -fsSo "$json" 'https://cohost.org/api/v1/trpc/posts.attachment.start?batch=1' \ | |
-X POST -H 'Content-Type: application/json' -H "Cookie: $COHOST_COOKIE" --data-raw \ | |
'{"0":{"projectHandle":"'"$projectHandle"'","postId":'"$postId"',"filename":"'"$basename"'","contentType":"'"$contentType"'","contentLength":'"$contentLength"',"width":'"$width"',"height":'"$height"'}}' | |
# build list of -F key=value arguments from what we were given in step 1 | |
set -- | |
< "$json" jq -r '.[0].result.data.requiredFields | to_entries[] | "\(.key)=\(.value)"' | while read -r kv; do | |
set -- "$@" -F "$kv" | |
done | |
>&2 echo "step 2/3 (redcent-dev)" | |
curl -fsS "$@" -F "file=@$file" "$(< "$json" jq -r '.[0].result.data.url')" | |
>&2 echo "step 3/3 (posts.attachment.finish)" | |
curl -fsSo "$json" 'https://cohost.org/api/v1/trpc/posts.attachment.finish?batch=1' \ | |
-X POST -H 'Content-Type: application/json' -H "Cookie: $COHOST_COOKIE" --data-raw \ | |
'{"0":{"projectHandle":"'"$projectHandle"'","postId":'"$postId"',"attachmentId":"'"$(< "$json" jq -r '.[0].result.data.attachmentId')"'"}}' | |
# print both url formats, so the user can use the correct one, | |
# but also has every right to be incorrect | |
< "$json" jq -r '.[0].result.data.url' >&2 | |
< "$json" jq -r '"https://cohost.org/rc/attachment-redirect/" + .[0].result.data.attachmentId' | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment