From a6da379c5f0395cfa88c7dc95c4ab101b0006182 Mon Sep 17 00:00:00 2001 From: stormdragon2976 Date: Tue, 31 Jan 2023 18:18:39 -0500 Subject: [PATCH] A bit of code reorganization. Added /refresh command. Actually displaying stuff and grabbing the latest only still needs some work. --- ratatoskr.sh | 84 ++++++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 39 deletions(-) diff --git a/ratatoskr.sh b/ratatoskr.sh index 5e8c477..d29c7f4 100755 --- a/ratatoskr.sh +++ b/ratatoskr.sh @@ -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 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. @@ -231,45 +272,7 @@ done # Important, set bgLoop to 0 or this loop will not close with the program bgLoop=0 while : ; do - 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 - 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 - + show_timeline sleep "${interval:-300}" done & @@ -286,6 +289,9 @@ while : ; do "/exit"|"/quit") exit 0 ;; + "/refresh") + show_timeline + ;; *) echo "Error: '${command}' is not a valid command." play_sound error