Skip to content

Instantly share code, notes, and snippets.

@xuchunyang
Created September 30, 2014 06:46
Show Gist options
  • Save xuchunyang/5872afcca7305fbab085 to your computer and use it in GitHub Desktop.
Save xuchunyang/5872afcca7305fbab085 to your computer and use it in GitHub Desktop.
ydcv in bash
#!/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
@xuchunyang
Copy link
Author

Sample output:

~  ./ydcv.sh private
private
   n. 列兵;二等兵
   adj. 私人的;私有的;私下的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment