A bit of code reorganization. Added /refresh command. Actually displaying stuff and grabbing the latest only still needs some work.

This commit is contained in:
stormdragon2976 2023-01-31 18:18:39 -05:00
parent f790267a44
commit a6da379c5f

View File

@ -159,6 +159,47 @@ local json="$(jq -n --arg status "$text" \
} }
# Display timelines
show_timeline() {
if [[ -n "${since_id}" ]]; then
result="$(curl -sS --oauth2-bearer "${oauth_token}" "${instanceURL}/api/v1/timelines/${timeline:-home}" -d "since_id=${since_id}")"
else
result="$(curl -sS --oauth2-bearer "${oauth_token}" "${instanceURL}/api/v1/timelines/${timeline:-home}")"
fi
# Error checking
# Check if the result is a valid JSON
if ! echo "$result" | jq '.' >/dev/null 2>&1; then
echo "Error: The response from the server was invalid."
play_sound error
return
fi
error=$(echo "$result" | jq -r '.error // "null"' 2> /dev/null)
if [[ "${error:-null}" != "null" ]]; then
echo "Error fetching ${timeline:-home} timeline: $error"
play_sound error
return
fi
# process the response to get the latest event id
latest_id="$(jq -r '.[].id' <<< "$result")"
if [[ "${since_id}" != "${latest_id}" ]]; then
# handle new events
events="$(jq -r '.[].content' <<< "$result")"
stripped_events="$(echo "$events" | sed -E 's/<[^>]+>//g')"
usernames="$(jq -r '.[].account.username' <<< "$result")"
echo -e "$usernames" | while read -r username; do
echo "$username: $(echo "$stripped_events" | head -n 1)"
stripped_events="$(echo "$stripped_events" | tail -n +2)"
echo
done
play_sound new_${timeline}
since_id="$latest_id"
fi
}
# Variable initialization # Variable initialization
configPath="${XDG_CONFIG_HOME:-$HOME/.config}/ratatoskr" # Path for settings, usually ~/.config/ratatoskr configPath="${XDG_CONFIG_HOME:-$HOME/.config}/ratatoskr" # Path for settings, usually ~/.config/ratatoskr
configFile="default.conf" # The default config file, eventually will support multiple accounts. configFile="default.conf" # The default config file, eventually will support multiple accounts.
@ -231,45 +272,7 @@ done
# Important, set bgLoop to 0 or this loop will not close with the program # Important, set bgLoop to 0 or this loop will not close with the program
bgLoop=0 bgLoop=0
while : ; do while : ; do
if [[ -n "${since_id}" ]]; then show_timeline
result="$(curl -sS --oauth2-bearer "${oauth_token}" "${instanceURL}/api/v1/timelines/${timeline:-home}" -d "since_id=${since_id}")"
else
result="$(curl -sS --oauth2-bearer "${oauth_token}" "${instanceURL}/api/v1/timelines/${timeline:-home}")"
fi
# Error checking
# Check if the result is a valid JSON
if ! echo "$result" | jq '.' >/dev/null 2>&1; then
echo "Error: The response from the server was invalid."
play_sound error
sleep "${interval:-300}"
continue
fi
error=$(echo "$result" | jq -r '.error // "null"' 2> /dev/null)
if [[ "${error:-null}" != "null" ]]; then
echo "Error fetching ${timeline:-home} timeline: $error"
play_sound error
sleep "${interval:-300}"
continue
fi
# process the response to get the latest event id
latest_id="$(jq -r '.[].id' <<< "$result")"
if [[ "${since_id}" != "${latest_id}" ]]; then
# handle new events
events="$(jq -r '.[].content' <<< "$result")"
stripped_events="$(echo "$events" | sed -E 's/<[^>]+>//g')"
usernames="$(jq -r '.[].account.username' <<< "$result")"
echo -e "$usernames" | while read -r username; do
echo "$username: $(echo "$stripped_events" | head -n 1)"
stripped_events="$(echo "$stripped_events" | tail -n +2)"
echo
done
play_sound new_${timeline}
since_id="$latest_id"
fi
sleep "${interval:-300}" sleep "${interval:-300}"
done & done &
@ -286,6 +289,9 @@ while : ; do
"/exit"|"/quit") "/exit"|"/quit")
exit 0 exit 0
;; ;;
"/refresh")
show_timeline
;;
*) *)
echo "Error: '${command}' is not a valid command." echo "Error: '${command}' is not a valid command."
play_sound error play_sound error