anki-update
This commit is contained in:
parent
d18717f7e3
commit
eb22176b2a
|
|
@ -1,5 +1,8 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
anki_connect="localhost:8765"
|
||||
afield="sent_a"
|
||||
aext="ogg"
|
||||
ifield="img"
|
||||
|
|
@ -11,25 +14,16 @@ rfield="sent_r"
|
|||
lock="$TMPDIR/ankiupdatelock"
|
||||
|
||||
log() {
|
||||
notify-send "$1"
|
||||
notify-send "anki-update: $1"
|
||||
printf "\e[33m%s\e[0m\n" "$1"
|
||||
}
|
||||
|
||||
#####
|
||||
|
||||
if ! [ -e "$lock" ] && [ "$1" != "-s" ]; then
|
||||
log 'recording started'
|
||||
record-audio \
|
||||
-af silenceremove=1:0:-50dB \
|
||||
"$TMPDIR/aud-$$.$aext" &
|
||||
echo "$$,$!" > "$lock"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
#####
|
||||
|
||||
fail() {
|
||||
log "$1"
|
||||
bell
|
||||
exit 1
|
||||
}
|
||||
anki() {
|
||||
curl -s localhost:8765 -X POST -d "$(printf '{"action":"%s","version":6,"params":%s}' "$1" "$2")"
|
||||
curl -s "$anki_connect" -X POST -d "$(printf '{"action":"%s","version":6,"params":%s}' "$1" "$2")"
|
||||
}
|
||||
stop_record() {
|
||||
kill "$pid2"
|
||||
|
|
@ -41,13 +35,14 @@ screenshot() {
|
|||
tag="$(xprop -id "$window" | sed -n 's/^WM_NAME(STRING) = "\(.*\)"$/\1/p' | tr ' ' _)"
|
||||
echo "* tag: $tag"
|
||||
}
|
||||
get_note() {
|
||||
id="$(anki findNotes '{"query":"added:1"}' | jq .result[-1])"
|
||||
get_latest() {
|
||||
id="$(anki findNotes '{"query":"added:1"}')" || fail "anki connect unreachable"
|
||||
id="$(echo "$id" | jq .result[-1])"
|
||||
if [ "$id" = "null" ]; then
|
||||
log "no note to update"
|
||||
bell
|
||||
exit 1
|
||||
fail "no note to update"
|
||||
fi
|
||||
}
|
||||
get_note_field() {
|
||||
note="$(anki notesInfo "$(printf '{"notes":[%d]}' "$id")")"
|
||||
old_sent="$(echo "$note" | jq -r .result[0].fields.['"'"$sfield"'"'].value)"
|
||||
echo "* old sentence: $old_sent"
|
||||
|
|
@ -65,24 +60,53 @@ get_reading() {
|
|||
echo "* clipboard: $sent"
|
||||
case "$sent" in
|
||||
*"$old_term"*) ;;
|
||||
*) log "clipboard does not contain term"; bell; exit 1 ;;
|
||||
*) fail "clipboard does not contain term" ;;
|
||||
esac
|
||||
reading="$(furigana "$sent")"
|
||||
sent="$(echo $sent | a="$old_term" pyp -b 's=os.environ["a"]' 'x.replace(s, "<b>%s</b>"%s)')"
|
||||
echo "* new sentence: $sent"
|
||||
echo "* reading: $sent"
|
||||
}
|
||||
update() {
|
||||
anki updateNote "$(printf '{"note":{
|
||||
"id":%d,
|
||||
"tags":"%s",
|
||||
"fields":{"%s":"%s","%s":"%s"},
|
||||
"picture":{
|
||||
"filename":"%s",
|
||||
"path":"%s",
|
||||
"fields":["%s"]
|
||||
}%s
|
||||
}}' "$id" "$tag" "$sfield" "$sent" "$rfield" "$reading" "$iname" "$image" "$ifield" "$1")"
|
||||
log "updated note $id"
|
||||
}
|
||||
|
||||
if [ "$1" = "-s" ]; then
|
||||
image="$TMPDIR/img-$$.$iext"
|
||||
trap "rm '$image'" EXIT
|
||||
|
||||
get_latest
|
||||
screenshot
|
||||
get_note
|
||||
get_note_field
|
||||
get_name
|
||||
get_reading
|
||||
update
|
||||
elif [ "$1" = "-x" ]; then
|
||||
[ -e "$lock" ] || fail "not running"
|
||||
IFS=, read pid1 pid2 id < "$lock"
|
||||
kill "$pid2"
|
||||
audio="$TMPDIR/aud-$pid1.$aext"
|
||||
rm "$audio" "$lock"
|
||||
log "cancelled anki update"
|
||||
elif ! [ -e "$lock" ]; then
|
||||
get_latest
|
||||
log 'recording started'
|
||||
record-audio \
|
||||
-af silenceremove=1:0:-50dB \
|
||||
"$TMPDIR/aud-$$.$aext" &
|
||||
echo "$$,$!,$id" > "$lock"
|
||||
else
|
||||
IFS=, read pid1 pid2 < "$lock"
|
||||
IFS=, read pid1 pid2 id < "$lock"
|
||||
audio="$TMPDIR/aud-$pid1.$aext"
|
||||
image="$TMPDIR/img-$pid1.$iext"
|
||||
trap "rm '$audio' '$image' '$lock'" EXIT
|
||||
|
|
@ -90,24 +114,12 @@ else
|
|||
log "stopping recording"
|
||||
stop_record
|
||||
screenshot
|
||||
get_note
|
||||
get_note_field
|
||||
get_name
|
||||
get_reading
|
||||
|
||||
append="$(printf ', "audio":{
|
||||
update "$(printf ', "audio":{
|
||||
"filename":"%s",
|
||||
"path":"%s",
|
||||
"fields":["%s"]
|
||||
}' "$aname" "$audio" "$afield")"
|
||||
fi
|
||||
anki updateNote "$(printf '{"note":{
|
||||
"id":%d,
|
||||
"tags":"%s",
|
||||
"fields":{"%s":"%s","%s":"%s"},
|
||||
"picture":{
|
||||
"filename":"%s",
|
||||
"path":"%s",
|
||||
"fields":["%s"]
|
||||
}%s
|
||||
}}' "$id" "$tag" "$sfield" "$sent" "$rfield" "$reading" "$iname" "$image" "$ifield" "$append")"
|
||||
log "updated note $id"
|
||||
|
|
|
|||
Loading…
Reference in a new issue