summaryrefslogtreecommitdiff
path: root/syndicate.sh
blob: 80d1be0b3adad0a319505cdc5400bcdf4497a0b3 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
# SPDX-FileCopyrightText: 2025 Matthew Fennell <matthew@fennell.dev>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

set -u
set -o pipefail

config_file=$1

community=$(grep "^community=" "${config_file}" | cut --delimiter = --fields 2)
instance=$(grep "^instance=" "${config_file}" | cut --delimiter = --fields 2)
token=$(grep "^token=" "${config_file}" | cut --delimiter = --fields 2)
username=$(grep "^username=" "${config_file}" | cut --delimiter = --fields 2)
first_posted_magazine=$(grep "^first_posted_magazine=" "${config_file}" | cut --delimiter = --fields 2)
first_posted_podcast=$(grep "^first_posted_podcast=" "${config_file}" | cut --delimiter = --fields 2)

remote_post_history="${instance}/feeds/u/${username}.xml?sort=New"
local_post_history="/tmp/${username}.rss"

function post {
	local name="$1"
	local url="$2"
	local image="$3"
	local body="$4"

	echo "Creating post \"${name}\" with url \"${url}\", thumbnail \"${image}\" and body \"${body}\" on instance ${instance}, community ${community}"

	community_id=$(curl --silent --fail "${instance}/api/v3/community?name=${community}" | jq --raw-output .community_view.community.id | grep --only-match "[[:digit:]]*")
	curl --silent --fail-with-body --oauth2-bearer "${token}" --json "{\"name\":\"${name}\",\"community_id\":${community_id},\"url\":\"${url}\",\"body\":\"${body}\",\"language_id\":37,\"custom_thumbnail\":\"${image}\"}" "${instance}/api/v3/post"
}

function syndicate_magazine {
	echo "Syndicating magazine"

	template="Private Eye Issue"
	last_posted_issue=$(xq --extract "/rss/channel/item/title[starts-with(., '${template} ')]" "${local_post_history}" | grep --only-match "[[:digit:]]*")

	if [ -z "${last_posted_issue}" ];
	then
		echo "No previous post found: defaulting to issue ${first_posted_magazine}"
		last_posted_issue="${first_posted_magazine}"
	fi

	issue_to_try_posting=$((last_posted_issue + 1))

	echo "Last posted issue was ${last_posted_issue}. Checking issue ${issue_to_try_posting}"

	url="https://www.private-eye.co.uk/covers/cover-${issue_to_try_posting}"
	image="https://www.private-eye.co.uk/pictures/covers/full/${issue_to_try_posting}_big.jpg"
	name="${template} ${issue_to_try_posting}"

	private_eye_response=$(curl --silent --output /dev/null --write-out "%{http_code}" "${image}")

	if [ "${private_eye_response}" != "200" ];
	then
		echo "Received ${private_eye_response} for ${image}. Stop"
		return
	fi

	echo "Received ${private_eye_response} for ${image}. Intend to post it"
	post "${name}" "${url}" "${image}" "![](${image})"
}

function syndicate_podcast {
	echo "Syndicating podcast"

	remote_rss="https://audioboom.com/channels/5112392.rss"
	local_rss="/tmp/page94.rss"
	template="https://www.private-eye.co.uk/podcast/"

	last_posted_issue=$(xq --extract "/rss/channel/item/enclosure/@url[starts-with(., '${template}')]" "${local_post_history}" | cut -d / -f 5 | grep --only-match "[[:digit:]]*")

	if [ -z "${last_posted_issue}" ];
	then
		echo "No previous post found: defaulting to issue ${first_posted_podcast}"
		last_posted_issue="${first_posted_podcast}"
	fi

	curl --silent "${remote_rss}" --output "${local_rss}"

	most_recently_published_issue=$(xq --extract "/rss/channel/item/itunes:episode" "${local_rss}" | grep --only-match "[[:digit:]]*")

	name=$(xq --extract "/rss/channel/item/itunes:title" "${local_rss}")
	url="https://www.private-eye.co.uk/podcast/${most_recently_published_issue}"
	image=$(xq --extract "/rss/channel/item/itunes:image/@href" "${local_rss}")
	audio=$(xq --extract "/rss/channel/item/enclosure/@url" "${local_rss}" | cut -d '?' -f 1)

	rm -f "${local_rss}"

	echo "Last posted issue was ${last_posted_issue}. Last released issue was ${most_recently_published_issue}"

	if [ "${most_recently_published_issue}" -le "${last_posted_issue}" ];
	then
		echo "No issue more recently published than ${most_recently_published_issue}. Stop"
		return
	fi

	echo "${most_recently_published_issue} is newer than ${last_posted_issue}. Intend to post it"

	post "${name}" "${url}" "${image}" "[Direct link to audio](${audio})"
}

echo "Acting as ${username} on ${instance}"

curl --silent "${remote_post_history}" --output "${local_post_history}"

syndicate_magazine
syndicate_podcast

rm -f "${local_post_history}"