summary refs log tree commit diff
path: root/www/photo.causal.agency/mastodon.sh
blob: 1eaa11147a1e48c32a2bddc7c9722866d36aadea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/sh
set -eu

Instance=https://tilde.zone
Root=${1:-static}

if ! test -f app.json; then
	echo 'No app.json!' >&2
	exit 1
fi
chmod 600 app.json

if ! test -f token.json; then
	client_id=$(jq -r .client_id app.json)
	client_secret=$(jq -r .client_secret app.json)
	echo "Please open ${Instance}/oauth/authorize?client_id=${client_id}&scope=write&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code"
	printf 'Enter code: '
	read -r code
	curl -Ss -X POST \
		-F 'grant_type=authorization_code' \
		-F "client_id=${client_id}" \
		-F "client_secret=${client_secret}" \
		-F 'redirect_uri=urn:ietf:wg:oauth:2.0:oob' \
		-F "code=${code}" \
		${Instance}/oauth/token >token.json
fi
chmod 600 token.json

access_token=$(jq -r .access_token token.json)

if ! test -f posted.txt; then
	touch posted.txt
fi

photo=$(
	find ${Root} -type f -path '*/0*/*.jpg' |
	sort | comm -13 posted.txt - | head -n 1
)
preview=${Root}/preview/${photo##*/}

media_id=$(
	curl -Ss -X POST \
		-H "Authorization: Bearer ${access_token}" \
		-F "file=@${preview}" \
		${Instance}/api/v2/media |
	jq -r .id
)

curl -Ss -X POST \
	-H "Authorization: Bearer ${access_token}" \
	-F "media_ids[]=${media_id}" \
	${Instance}/api/v1/statuses >/dev/null

echo ${photo} >>posted.txt