Created
October 13, 2012 03:36
-
-
Save rainly/3883133 to your computer and use it in GitHub Desktop.
Youdao for Shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ######################################### | |
| RED_ORG="\033[0;31m" | |
| GREEN_ORG="\033[0;32m" | |
| NO_COLOR_ORG="\033[0m" | |
| # youdao helpers | |
| function t { | |
| if [ $1 == "-a" ]; then | |
| all=1 | |
| query=$(curl -s -o /dev/null -w %{url_effective} --get --data-urlencode "$2" "" | sed -e "s/\/\?//g") | |
| else | |
| all=0 | |
| query=$(curl -s -o /dev/null -w %{url_effective} --get --data-urlencode "$1" "" | sed -e "s/\/\?//g") | |
| fi | |
| response="`curl -s \"http://fanyi.youdao.com/openapi.do?type=data&doctype=json&version=1.1&relatedUrl=http%3A%2F%2Ffanyi.youdao.com%2F&keyfrom=fanyiweb&key=null&translate=on&q=$query\"`" | |
| echo "$response" | awk -F'[\{\}]' -v all=$all -v resultcolor=$RED_ORG -v entrycolor=$GREEN_ORG -v nocolor=$NO_COLOR_ORG '{ | |
| #for(i=1; i<=NF; i++){ | |
| # print $i | |
| #} | |
| gsub("\\\\u2026", "…") | |
| # parse error code | |
| for(i=1; i<=NF; i++){ | |
| if($i ~ "errorCode"){ | |
| gsub(/{|}|"/,"", $i) | |
| split($i, tmp, ",") | |
| split(tmp[3], r, ":") | |
| if(r[2] != 0){ | |
| print resultcolor "Oops~" nocolor | |
| print entrycolor "There is an error, please try again." nocolor | |
| exit | |
| }else{ | |
| break | |
| } | |
| } | |
| } | |
| # parse translation | |
| for(i=1; i<=NF; i++){ | |
| if($i ~ "translation"){ | |
| gsub(/"/, "", $i) | |
| split($i, tmp, ",") | |
| split(tmp[1], r, ":") | |
| print resultcolor r[2] nocolor | |
| break | |
| } | |
| } | |
| # parse phonetic | |
| for(i=1; i<=NF; i++){ | |
| if($i ~ "phonetic"){ | |
| split($i, tmp, "\",\"") | |
| split(tmp[1], r, "\":\"") | |
| print resultcolor "/" r[2] "/" nocolor | |
| break | |
| } | |
| } | |
| # parse explains | |
| for(i=1; i<=NF; i++){ | |
| if($i ~ "explains"){ | |
| gsub(/\["|"\]/, "", $i) | |
| split($i, tmp, "\"explains\":") | |
| len=split(tmp[2], r, "\",\"") | |
| for(j=1; j<=len; j++){ | |
| print entrycolor j ". " r[j] nocolor | |
| } | |
| break | |
| } | |
| } | |
| # parse examples | |
| if(all == 1){ | |
| print "============" | |
| for(i=1; i<=NF; i++){ | |
| if($i ~ "key" && $i ~ "value"){ | |
| split($i, tmp, ":") | |
| gsub(/"/, "", tmp[3]) | |
| gsub(/"|,"key"|\[|\]/, "", tmp[2]) | |
| print tmp[3] ": " tmp[2] | |
| } | |
| } | |
| } | |
| }' | |
| return 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment