From 77e6890a6a1cb34ff4549308d08cab07d77e6176 Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Wed, 10 Feb 2021 10:12:38 +0300 Subject: [PATCH 1/5] Avoid losing updated content If a card is selected in the Anki browser at the time of update the browser may overwrite the content of the note's fields after the update has completed. This change forces the browser to switch focus before updating notes. --- ames.sh | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/ames.sh b/ames.sh index d3a34ed..a4965b5 100755 --- a/ames.sh +++ b/ames.sh @@ -54,13 +54,14 @@ get_last_id() { "query": "added:1" } }' - local new_card_response=$(curl localhost:8765 -X POST -d "$new_card_request" --silent) - local list=$(echo $new_card_response | cut -d "[" -f2 | cut -d "]" -f1) + local new_card_response=$(ankiconnect_request "$new_card_request") + local list=$(echo "$new_card_response" | cut -d "[" -f2 | cut -d "]" -f1) IFS=',' read -ra ids <<< $list newest_card_id=${ids[0]} for n in "${ids[@]}" ; do [[ "$n" > "$newest_card_id" ]] && newest_card_id=$n done + newest_card_id=${newest_card_id// /} return 0 } @@ -77,7 +78,30 @@ store_file() { }' request=${request///$name} request=${request//$dir} - curl localhost:8765 -X POST -d "$request" --silent >> /dev/null + ankiconnect_request "$request" >> /dev/null +} + +gui_browse() { + local -r query=${1:-nid:1} + local request='{ + "action": "guiBrowse", + "version": 6, + "params": { + "query": "" + } + }' + request=${request//$query} + ankiconnect_request "$request" +} + +ankiconnect_request() { + curl --silent localhost:8765 -X POST -d "${1:?}" +} + +safe_request() { + gui_browse "nid:1" + ankiconnect_request "${1:?}" + gui_browse "nid:${newest_card_id:?Newest card is not known.}" } update_img() { @@ -95,7 +119,8 @@ update_img() { update_request=${update_request//$newest_card_id} update_request=${update_request//$SCREENSHOT_FIELD} update_request=${update_request//$1} - local update_response=$(curl localhost:8765 -X POST -d "$update_request" --silent) + + safe_request "$update_request" } update_sound() { @@ -115,7 +140,8 @@ update_sound() { update_request=${update_request//$newest_card_id} update_request=${update_request//$AUDIO_FIELD} update_request=${update_request//$1} - local update_response=$(curl localhost:8765 -X POST -d "$update_request" --silent) + + safe_request "$update_request" } screenshot() { From f14b271354f2ace2b25d63730014a89a5c2017f7 Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Wed, 10 Feb 2021 11:12:57 +0300 Subject: [PATCH 2/5] dry get_last_id() --- ames.sh | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/ames.sh b/ames.sh index a4965b5..292bfa4 100755 --- a/ames.sh +++ b/ames.sh @@ -46,23 +46,35 @@ notify_screenshot_add() { fi } +maxn() { + tr -d ' ' | tr ',' '\n' | awk ' + BEGIN { + max = 0 + } + { + if ($0 > max) { + max = $0 + } + } + END { + print max + } + ' +} + get_last_id() { - local new_card_request='{ + local new_card_request='{ "action": "findNotes", "version": 6, "params": { "query": "added:1" } }' - local new_card_response=$(ankiconnect_request "$new_card_request") - local list=$(echo "$new_card_response" | cut -d "[" -f2 | cut -d "]" -f1) - IFS=',' read -ra ids <<< $list - newest_card_id=${ids[0]} - for n in "${ids[@]}" ; do - [[ "$n" > "$newest_card_id" ]] && newest_card_id=$n - done - newest_card_id=${newest_card_id// /} - return 0 + local new_card_response list + + new_card_response=$(ankiconnect_request "$new_card_request") + list=$(echo "$new_card_response" | cut -d "[" -f2 | cut -d "]" -f1) + newest_card_id=$(echo "$list" | maxn) } store_file() { From e05f65ae709d5ae42c211f7b4e73b6932364c4e2 Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Wed, 10 Feb 2021 11:14:59 +0300 Subject: [PATCH 3/5] handle empty arguments --- ames.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ames.sh b/ames.sh index 292bfa4..2e145d5 100755 --- a/ames.sh +++ b/ames.sh @@ -238,6 +238,11 @@ record() { fi } +if [[ -z ${1-} ]]; then + usage + exit 1 +fi + while getopts 'hrsaw' flag; do case "${flag}" in h) usage ;; @@ -245,5 +250,6 @@ while getopts 'hrsaw' flag; do s) screenshot ;; a) again ;; w) screenshot_window ;; + *) ;; esac done From 624f0cfff0cacd3d67c8d9fc6c5ca71748b99fdf Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Wed, 10 Feb 2021 11:16:44 +0300 Subject: [PATCH 4/5] fix shellcheck warnings --- ames.sh | 90 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 33 deletions(-) diff --git a/ames.sh b/ames.sh index 2e145d5..ff5bddf 100755 --- a/ames.sh +++ b/ames.sh @@ -26,6 +26,7 @@ IMAGE_FORMAT="webp" CONFIG_FILE_PATH="$HOME/.config/ames/config" if [[ -f "$CONFIG_FILE_PATH" ]]; then + # shellcheck disable=SC1090 source "$CONFIG_FILE_PATH" fi @@ -79,7 +80,7 @@ get_last_id() { store_file() { local -r dir=${1:?} - local -r name=$(basename $dir) + local -r name=$(basename -- "$dir") local request='{ "action": "storeMediaFile", "version": 6, @@ -90,7 +91,7 @@ store_file() { }' request=${request///$name} request=${request//$dir} - ankiconnect_request "$request" >> /dev/null + ankiconnect_request "$request" >>/dev/null } gui_browse() { @@ -157,30 +158,40 @@ update_sound() { } screenshot() { - local geom=$(slop) - local path=$(mktemp /tmp/maim-screenshot.XXXXXX.png) - - maim $path -g $geom - ffmpeg -i $path "/tmp/$(basename $path | cut -d "." -f-2).$IMAGE_FORMAT" -hide_banner -loglevel error - rm $path - path="/tmp/$(basename $path | cut -d "." -f-2).$IMAGE_FORMAT" - echo "$geom" > /tmp/previous-maim-screenshot + local -r geom=$(slop) + local -r path=$(mktemp /tmp/maim-screenshot.XXXXXX.png) + + maim "$path" -g "$geom" + ffmpeg -nostdin \ + -hide_banner \ + -loglevel error \ + -i "$path" \ + "/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" + + rm "$path" + path="/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" + echo "$geom" >/tmp/previous-maim-screenshot store_file "$path" - update_img $(basename $path) + update_img "$(basename -- "$path")" notify_screenshot_add } again() { - local path=$(mktemp /tmp/maim-screenshot.XXXXXX.png) + local -r path=$(mktemp /tmp/maim-screenshot.XXXXXX.png) if [[ -f /tmp/previous-maim-screenshot ]]; then - maim $path -g $(cat /tmp/previous-maim-screenshot) - ffmpeg -i $path "/tmp/$(basename $path | cut -d "." -f-2).$IMAGE_FORMAT" -hide_banner -loglevel error - rm $path - path="/tmp/$(basename $path | cut -d "." -f-2).$IMAGE_FORMAT" + maim "$path" -g "$(cat /tmp/previous-maim-screenshot)" + ffmpeg -nostdin \ + -hide_banner \ + -loglevel error \ + -i "$path" \ + "/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" + + rm "$path" + path="/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" store_file "$path" get_last_id - update_img $(basename $path) + update_img "$(basename -- "$path")" notify_screenshot_add else screenshot @@ -188,31 +199,45 @@ again() { } screenshot_window() { - local path=$(mktemp /tmp/maim-screenshot.XXXXXX.png) - maim $path -i $(xdotool getactivewindow) - ffmpeg -i $path "/tmp/$(basename $path | cut -d "." -f-2).$IMAGE_FORMAT" -hide_banner -loglevel error - rm $path - path="/tmp/$(basename $path | cut -d "." -f-2).$IMAGE_FORMAT" + local -r path=$(mktemp /tmp/maim-screenshot.XXXXXX.png) + maim "$path" -i "$(xdotool getactivewindow)" + ffmpeg -nostdin \ + -hide_banner \ + -loglevel error \ + -i "$path" "/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" + + rm "$path" + path="/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" store_file "$path" - update_img $(basename $path) + update_img "$(basename -- "$path")" notify_screenshot_add } - record() { # this section is a heavily modified version of the linux audio script written by salamander on qm's animecards. - local recordingToggle="/tmp/ffmpeg-recording-audio" + local -r recordingToggle="/tmp/ffmpeg-recording-audio" + if [[ ! -f /tmp/ffmpeg-recording-audio ]]; then - local audioFile=$(mktemp "/tmp/ffmpeg-recording.XXXXXX.$AUDIO_FORMAT") - echo "$audioFile" > "$recordingToggle" + local -r audioFile=$(mktemp "/tmp/ffmpeg-recording.XXXXXX.$AUDIO_FORMAT") + echo "$audioFile" >"$recordingToggle" if [ "$OUTPUT_MONITOR" == "" ]; then - local output=$(pactl info | grep 'Default Sink' | awk '{print $NF ".monitor"}') + local -r output=$(pactl info | grep 'Default Sink' | awk '{print $NF ".monitor"}') else - local output="$OUTPUT_MONITOR" + local -r output="$OUTPUT_MONITOR" fi + ffmpeg -nostdin \ + -y \ + -loglevel error \ + -f pulse \ + -i "$output" \ + -ac 2 \ + -af 'silenceremove=1:0:-50dB' \ + -ab $AUDIO_BITRATE \ + "$audioFile" 1>/dev/null & + if [[ "$LANG" == en* ]]; then notify-send --hint=int:transient:1 -t 500 -u normal "Recording started..." fi @@ -220,14 +245,13 @@ record() { notify-send --hint=int:transient:1 -t 500 -u normal "録音しています..." fi - ffmpeg -f pulse -i $output -ac 2 -af silenceremove=1:0:-50dB -ab $AUDIO_BITRATE -y "$audioFile" 1>/dev/null & + echo "Started recording." else - local audioFile="$(cat "$recordingToggle")" + local -r audioFile="$(cat "$recordingToggle")" rm "$recordingToggle" killall ffmpeg - store_file "${audioFile}" - update_sound $(basename $audioFile) + update_sound "$(basename -- "$audioFile")" if [[ "$LANG" == en* ]]; then notify-send --hint=int:transient:1 -t 500 -u normal "Recording added" From a7ef9c68526a74447737674663362bff805fb85a Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Wed, 10 Feb 2021 11:33:54 +0300 Subject: [PATCH 5/5] dry --- ames.sh | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/ames.sh b/ames.sh index ff5bddf..6f64cd9 100755 --- a/ames.sh +++ b/ames.sh @@ -160,38 +160,38 @@ update_sound() { screenshot() { local -r geom=$(slop) local -r path=$(mktemp /tmp/maim-screenshot.XXXXXX.png) + local -r converted_path="/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" maim "$path" -g "$geom" ffmpeg -nostdin \ -hide_banner \ -loglevel error \ -i "$path" \ - "/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" + "$converted_path" rm "$path" - path="/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" echo "$geom" >/tmp/previous-maim-screenshot - store_file "$path" - update_img "$(basename -- "$path")" + store_file "$converted_path" + update_img "$(basename -- "$converted_path")" notify_screenshot_add } again() { local -r path=$(mktemp /tmp/maim-screenshot.XXXXXX.png) + local -r converted_path="/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" + if [[ -f /tmp/previous-maim-screenshot ]]; then maim "$path" -g "$(cat /tmp/previous-maim-screenshot)" ffmpeg -nostdin \ -hide_banner \ -loglevel error \ -i "$path" \ - "/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" + "$converted_path" rm "$path" - path="/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" - - store_file "$path" + store_file "$converted_path" get_last_id - update_img "$(basename -- "$path")" + update_img "$(basename -- "$converted_path")" notify_screenshot_add else screenshot @@ -200,6 +200,8 @@ again() { screenshot_window() { local -r path=$(mktemp /tmp/maim-screenshot.XXXXXX.png) + local -r converted_path="/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" + maim "$path" -i "$(xdotool getactivewindow)" ffmpeg -nostdin \ -hide_banner \ @@ -207,10 +209,9 @@ screenshot_window() { -i "$path" "/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" rm "$path" - path="/tmp/$(basename -- "$path" | cut -d "." -f-2).$IMAGE_FORMAT" - store_file "$path" - update_img "$(basename -- "$path")" + store_file "$converted_path" + update_img "$(basename -- "$converted_path")" notify_screenshot_add }