summary refs log tree commit diff
path: root/etc/pronouns/bot.sh
blob: 8b7c9802b970892e569a904de0961ef873747ab5 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
set -eu

Instance=https://mstdn.isometry.group

if ! test -f access_token; then
	echo 'No access_token!' >&2
	exit 1
fi

post=true
while getopts 'n' opt; do
	case $opt in
		(n) post=false;;
		(?) exit 1;;
	esac
done
shift $((OPTIND - 1))

access_token=$(cat access_token)

account_id=$(
	curl -Ss \
		-H "Authorization: Bearer ${access_token}" \
		${Instance}/api/v1/accounts/verify_credentials |
	jq -r .id
)

# XXX: no pagination because I don't expect this to ever have over 80 followers
followers=$(
	curl -Ss \
		-H "Authorization: Bearer ${access_token}" \
		"${Instance}/api/v1/accounts/${account_id}/followers?limit=80" |
	jq -r 'map(select(.acct | contains("@") | not)) | .[].id'
)

for follower_id in $followers 112284333737697665; do
	account=$(
		curl -Ss \
			-H "Authorization: Bearer ${access_token}" \
			${Instance}/api/v1/accounts/${follower_id}
	)
	username=$(printf '%s' "${account}" | jq -r .username)
	pronouns=$(
		printf '%s' "${account}" |
		jq -r '
			.fields |
			map(select(.name | test("^prono(un|m)s?[?]?$|^((professional|leisure|casual) |anti-)no(un|m)s?$"; "i"))) |
			.[].value
		'
	)
	if ! test -f $follower_id; then
		printf '%s' "${pronouns}" >$follower_id
		continue
	fi
	old_pronouns=$(cat $follower_id)
	if [ "${pronouns}" != "${old_pronouns}" ]; then
		text=$(printf '%s' "${pronouns}" | dehtml)
		if $post; then
			curl -Ss -X POST \
				-H "Authorization: Bearer ${access_token}" \
				-F visibility=unlisted \
				--form-string \
				"status=@${username} has changed pronouns to: ${text}" \
				${Instance}/api/v1/statuses >/dev/null
		fi
		printf '%s' "${pronouns}" >$follower_id
	fi
done