#!/bin/bash read word word=$(echo $word | tr '[:upper:]' '[:lower:]') # read config file source oedquery.conf # curl options OPTS='--silent -L -H "app_id: $appId" -H "app_key: $appKey"' curlCount=0 _curl() { if [[ $curlCount -ge 3 ]] then echo "cURL loop, exiting." exit 0 fi result=$(eval curl $OPTS $1) ret=$? if [ "$ret" -ne 0 ] then echo "cURL error $ret when fetching." exit elif [[ "$result" == "Authentication failed" ]] then echo "Authentication failed, probably exceeded monthly usage." exit 0 fi ((curlCount=curlCount+1)) } # function to provide data inside a for loop _jq() { echo ${json} | base64 --decode | jq -r ${1} } # Get data from oed for given word _getdata() { # Take everything after final space as the requested word (assuming everything before the last space is a flag) word=$(echo $word | sed 's/.* //') if [[ "$word" = "null" ]] then echo "Missing word. Usage: !oed [option] [word]. Use -h for help." fi lemmasUrl=https://od-api.oxforddictionaries.com/api/v2/lemmas/$language/$word # get lemmas to link an inflected form back to its headword (required to get definition) _curl $lemmasUrl # get headword if it exists headWord=$(echo $result | jq '.results[0].lexicalEntries[0].inflectionOf[0].id') if [ "$headWord" == "null" ] then echo "Word not found. Use -h for help." exit fi entriesUrl=https://od-api.oxforddictionaries.com/api/v2/entries/$language/$headWord _curl $entriesUrl } # Return definition for the first sense of each lexical entry and create output string _define() { _getdata for json in $(echo "${result}" | jq -r '.results[0].lexicalEntries[] | @base64') do output="$output $(_jq '.lexicalCategory.text'): " # Sometimes there's no definition and so we can fall back to shortDefinition definition=$(_jq '.entries[0].senses[0].definitions[0]') if [[ -z $defintion ]] then definition=$(_jq '.entries[0].senses[0].shortDefinitions[0]') if [[ $definition == "null" ]] then definition=$(_jq '.entries[0].senses[0].crossReferenceMarkers[0]') if [[ $definition == "null" ]] then echo "No definiton found, if you're sure it's a word try the headword or use -h for help." exit fi fi fi output="$output $definition " examplePhrase=$(_jq '.entries[0].senses[0].examples[0].text') if [[ "$examplePhrase" != "null" ]] then output=$output" \"$examplePhrase\"" fi done echo $output exit 0 } # Return etymology if available _etym() { _getdata output=$(echo "${result}" | jq '.results[].lexicalEntries[0].entries[0].etymologies[0]' | tr -d \") if [[ "$output" = "null" ]] then echo "No etymology found." else echo $output fi exit 0 } # Return synonyms of first sense of first lexical entry (or second if no synonyms for the first) _thes() { _getdata # Loop to get synonyms for the first sense of a given lexical entry _thesLoop () { count=0 for json in $(echo "${result}" | jq -r '.results[].lexicalEntries['$1'].entries[0].senses[0].synonyms[]? | @base64'); do output="$output $(_jq '.text'), " ((count=count+1)) # Limit output to a resonable number of synonyms if [[ $count -ge $maxSynonyms ]] then break fi done } # Get synonyms for first lexical entry and try the second if there aren't any _thesLoop 0 if [[ -z $output ]] then _thesLoop 1 if [[ -z $output ]] then echo "No synonyms found." exit 0 fi fi # remove trailing comma output=$(echo $output | sed '$s/,$//') echo $output exit 0 } # Get URL for first audio file in returned data _audio() { _getdata output=$(echo "${result}" | jq '.results[0].lexicalEntries[0].entries[0].pronunciations[0].audioFile' | tr -d \") if [[ "$output" != "null" ]] then echo $output else echo "No audio file found." fi exit 0 } # Return first defintion with lexical category for Lexico's word of the day. _wod() { # Get word from lexico lexicoUrl="https://www.lexico.com/" _curl $lexicoUrl word=$(echo $result | hxnormalize -x | hxselect -i "a.linkword" | grep -o '>.*<' | sed 's/[><]//g') # Add the word of the day to the output. output="$output $word - " _define } _help() { echo "Usage: !oed [option] [word]. Options: -h help, -t thesaurus, -e etymology, -d define, -a audio, -wod word of the day, -s source." exit 0 } if [[ ${word} == "-h" ]] || [[ ${word} == "--help" ]] || [[ -z $word ]] then _help elif [[ ${word} == "--source" ]] || [[ ${word} == "-s" ]] then echo "https://www.blatech.co.uk/ars/oeddefine" exit 0 elif [[ ${word} =~ "-t" ]] then _thes elif [[ ${word} =~ "-et" ]] then _etym elif [[ ${word} =~ "-a" ]] then _audio elif [[ ${word} =~ "-wod" ]] then _wod else _define fi