Created
September 30, 2014 06:46
-
-
Save xuchunyang/5872afcca7305fbab085 to your computer and use it in GitHub Desktop.
ydcv in bash
This file contains 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
#!/bin/bash | |
# | |
# ydcv in bash using 'jq' to parse json | |
# | |
# Check argument | |
if [ -z $1 ]; then | |
echo "Usage: $0 word" | |
exit 1 | |
fi | |
# Fetch | |
API_URL="http://fanyi.youdao.com/openapi.do?keyfrom=YouDaoCV&key=659600698&type=data&doctype=json&version=1.1" | |
res=$( curl --silent --data-urlencode "q=$1" "${API_URL}" ) | |
# Extract data from JSON response | |
translation=$( echo $res | jq ".translation" ) | |
explains=$( echo $res | jq ".basic.explains" ) | |
query=$( echo $res | jq -r ".query" ) | |
# Display result | |
echo -e "\033[4m${query}\033[0m" | |
length=$( echo $explains | jq " . | length " ) | |
(( length-- )) | |
for i in $(eval echo {0..$length}); | |
do | |
echo " " $( echo $explains | jq -r ".[$i]" ) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output:
➜ ~ ./ydcv.sh private private n. 列兵;二等兵 adj. 私人的;私有的;私下的