Skip to content

Commit

Permalink
More notification
Browse files Browse the repository at this point in the history
  • Loading branch information
cdpath committed Sep 30, 2018
1 parent 18c3b32 commit 5df777b
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions sources/anki.popclipext/anki.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
# Created by cdpath on 2018/4/19.
# Copyright © 2018 cdpath. All rights reserved.

set -xeuo pipefail
#set -xeuo pipefail


## PopClip Env
entry=$POPCLIP_TEXT
safe_entry=$POPCLIP_URLENCODED_TEXT
dict_svc=$POPCLIP_OPTION_DICT_SVC
target_deck=$POPCLIP_OPTION_TARGET_DECK
note_type=$POPCLIP_OPTION_NOTE_TYPE
front_field=$POPCLIP_OPTION_FRONT_FIELD
back_field=$POPCLIP_OPTION_BACK_FIELD
tag=$POPCLIP_OPTION_TAG
entry=${POPCLIP_TEXT:-debug}
safe_entry=${POPCLIP_URLENCODED_TEXT:-debug}
dict_service=${POPCLIP_OPTION_DICT_SVC:-shanbay}
target_deck=${POPCLIP_OPTION_TARGET_DECK:-Default}
note_type=${POPCLIP_OPTION_NOTE_TYPE:-Basic}
front_field=${POPCLIP_OPTION_FRONT_FIELD:-Front}
back_field=${POPCLIP_OPTION_BACK_FIELD:-Back}
tag=${POPCLIP_OPTION_TAG:-debug}
app_tag=${POPCLIP_APP_NAME// /_} # replace spaces with underscore


Expand All @@ -31,6 +31,7 @@ dialog() {
## Dictionary Services
_shanbay()
{
local safe_entry=$1
url="https://api.shanbay.com/bdc/search/?word=$safe_entry"
local definition=$(curl -sSL $url | perl -pe 's/^.*?(?<="definition":)(.*?[^\\]")(?=\,).*?$/$1/' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/^"//' -e 's/"$//')
if [[ $definition = *'"status_code": 1'* ]]; then
Expand All @@ -42,6 +43,7 @@ _shanbay()

_youdao()
{
local safe_entry=$1
url="http://dict.youdao.com/m/search?q=$safe_entry"
curl -sSL $url \
| sed -ne '/网络释义/,/更多释义/p' \
Expand All @@ -52,20 +54,21 @@ _youdao()

look_up()
{
local definition=''
if [ "$dict_svc" = "shanbay" ]
local safe_entry=$1
if [ "$dict_service" = "shanbay" ]
then
definition=$(_shanbay)
elif [ "$dict_svc" = "youdao" ]
definition=$(_shanbay $safe_entry)
elif [ "$dict_service" = "youdao" ]
then
definition=$(_youdao)
definition=$(_youdao $safe_entry)
else
definition=''
echo "Not Implemented"
exit 1
fi

if [[ -z "$definition" ]]; then
dialog "$dict_svc" "未找到单词" 3
dialog "$dict_service" "未找到单词" 3
exit 1
else
echo $definition
Expand All @@ -76,6 +79,7 @@ look_up()
## AnkiConnect
gen_post_data()
{
local definition=$1
cat <<EOF
{
"action": "addNote",
Expand All @@ -84,7 +88,7 @@ gen_post_data()
"note": {
"fields": {
"$front_field": "$entry",
"$back_field": "$(look_up)"
"$back_field": "$definition"
},
"modelName": "$note_type",
"deckName": "$target_deck",
Expand All @@ -100,25 +104,31 @@ EOF

check_result()
{
if [[ $1 != *'"error": null'* ]]; then
if [[ $1 = "null" ]]; then
local resp=$1
local definition=$2
if [[ $resp != *'"error": null'* ]]; then
if [[ $resp = "null" ]]; then
msg="Invalid post data for AnkiConnect"
else
msg=$(echo "$1" | perl -pe 's/^.*?(?<="error": ")(.*?[^\\])(?=[\."]).*?$/$1/' | sed -e 's/^"//' -e 's/"$//')
msg=$(echo "$resp" | perl -pe 's/^.*?(?<="error": ")(.*?[^\\])(?=[\."]).*?$/$1/' | sed -e 's/^"//' -e 's/"$//')
fi
if [[ -z "$1" ]]; then
if [[ -z "$resp" ]]; then
msg="Did you open anki?"
fi
dialog "AnkiConnect" "$msg" 5
else
dialog "$entry" "Saved to $target_deck" 5
fi
}


## main
main()
{
local res=$(curl -X POST -d "$(gen_post_data)" "localhost:8765")
check_result "$res"
local definition=$(look_up $safe_entry)
payload=$(gen_post_data "$definition")
res=$(curl -sX POST -d "$payload" "localhost:8765")
check_result "$res" "$definition"
}


Expand Down

0 comments on commit 5df777b

Please sign in to comment.