ActivityDigest/digest.sh

110 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
url=$(curl -s -H Accept:application/activity+json "$1" | jq -r '.outbox' | xargs curl -s -H Accept:application/activity+json | jq -r '.first')
targetdate=$(date -d "now - $2 days" +%s)
declare -A activities
# Mastodon has pagination, so - let's find the page that contains post from now to n days ago.
stop=false
echo -n "Processing pages"
while [ "$url" != "" ] && [ "$stop" != true ] ; do
response=$(curl -s -H "Accept: application/activity+json" "$url")
lines=$(echo "$response" | jq -c '.orderedItems[] | {id, published}')
while IFS= read -r line; do
ts=$(echo "$line" | jq -r '.published' | xargs date +%s -d)
act=$(echo "$line" | jq -r '.id')
if [[ "$ts" -gt "$targetdate" ]]; then
activities["${ts}"]="$act"
else
stop=true
fi
done <<<"$lines"
url=$(echo "$response" | jq -r .next)
echo -n "."
done
echo ""
# We got some activities, so now let's extract relevant info form them, turning into actual posts data
declare -a posts
echo -n "Processing activities"
for key in "${!activities[@]}"; do
activity="${activities[$key]}"
response=$(curl -s -H "Accept: application/activity+json" "$activity")
type=$(echo "$response" | jq -r '.type')
datetime=$(echo "$response" | jq -r '.published')
date=$(echo "$datetime" | cut -d "T" -f 1)
time=$(echo "$datetime" | cut -d "T" -f 2 | tr -d "Z")
if [[ "$type" = "Announce" ]]; then
link=$(echo "$response" | jq -r '.object')
object=$(curl -s -H "Accept: application/activity+json" "$link")
content=$(echo "$object" | jq -r '.content')
replyto=$(echo "$response" | jq -r '.inReplyRTo')
actorlink=$(echo "$object" | jq -r '.attributedTo')
attachment=$(echo "$object" | jq -jc '.attachment[].url' 2>/dev/null || echo null)
if [ "$attachment" = "" ]; then
attachment="null"
fi
by=$(curl -s -H "Accept: application/activity+json" "$actorlink" | jq -r '.name')
else
link=$(echo "$response" | jq -r '.object.id')
content=$(echo "$response" | jq -r '.object.content')
replyto=$(echo "$response" | jq -r '.object.inReplyTo')
actorlink=$(echo "$response" | jq -r '.actor')
attachment=$(echo "$response" | jq -jc '.object.attachment[].url' 2>/dev/null || echo null)
if [ "$attachment" = "" ]; then
attachment="null"
fi
by=$(curl -s -H "Accept: application/activity+json" "$actorlink" | jq -r '.name')
fi
posts+=("$date$time$type$link$by$content$replyto$attachment")
echo -n "."
# $(echo $content | w3m -T text/html -dump)
done
# Sort posts by date
IFS=$'\n' sortedPosts=($(sort <<<"${posts[*]}"))
unset IFS
echo ""
# Render posts on the screen and via less
for i in "${sortedPosts[@]}"
do
date=$(echo "$i" | awk -F'―' '{ print $1 }')
time=$(echo "$i" | awk -F'―' '{ print $2 }')
type=$(echo "$i" | awk -F'―' '{ print $3 }')
link=$(echo "$i" | awk -F'―' '{ print $4 }')
by=$(echo "$i" | awk -F'―' '{ print $5 }')
content=$(echo "$i" | awk -F'―' '{ print $6 }')
replyto=$(echo "$i" | awk -F'―' '{ print $7 }')
attachment=$(echo "$i" | awk -F'―' '{ print $8 }')
if [ "$prevdate" != "$date" ]; then
echo -e "\e[1;41m====$date==== \e[0m"
prevdate="$date"
fi
if [[ "$type" = "Announce" ]]; then
type="🔄"
else
type="✨"
fi
echo -e "\e[1;42m$type $time $by $link\e[0m"
if [ "$replyto" != "null" ]; then
echo " ↩️ $replyto"
fi
echo "$content" | w3m -T text/html -dump
if [ "$attachment" != "null" ]; then
echo "$attachment"
fi
done | less -r
#printf "[%s]\n" "${sortedPosts[@]}"