From 9181864dac173179bbc817421935c48a0adf06f2 Mon Sep 17 00:00:00 2001 From: ars Date: Wed, 28 Oct 2020 19:59:57 +0000 Subject: Move curls to a function and add rate limiting. --- oedquery.sh | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) (limited to 'oedquery.sh') diff --git a/oedquery.sh b/oedquery.sh index b01b201..7c979e2 100755 --- a/oedquery.sh +++ b/oedquery.sh @@ -2,10 +2,31 @@ read word word=$(echo $word | tr '[:upper:]' '[:lower:]') -# curl options -OPTS='--silent -L -H "app_id: $appId" -H "app_key: $appKey"' # 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 20 ]] + 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 + ((count=count+1)) +} # function to provide data to a for loop _jq() { @@ -22,18 +43,7 @@ _getdata() { 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) - result=$(eval curl $OPTS $lemmasUrl) - if [[ "$result" == "Authentication failed" ]] - then - echo "Authentication failed, probably exceeded monthly usage." - exit 0 - fi - ret=$? - if [ "$ret" -ne 0 ] - then - echo "cURL error $ret when fetching." - exit - fi + _curl $lemmasUrl # get headword if it exists headWord=$(echo $result | jq '.results[0].lexicalEntries[0].inflectionOf[0].id') @@ -43,13 +53,7 @@ _getdata() { exit fi entriesUrl=https://od-api.oxforddictionaries.com/api/v2/entries/$language/$headWord - result=$(eval curl $OPTS $entriesUrl) - ret=$? - if [ "$ret" -ne 0 ] - then - echo "cURL error $ret when fetching." - exit - fi + _curl $entriesUrl } # Return definition for the first sense of each lexical entry and create output string @@ -136,7 +140,9 @@ _audio() { _wod() { wod=true # Get word from lexico - word=$( curl --silent https://www.lexico.com/ | hxnormalize -x | hxselect -i "a.linkword" | grep -o '>.*<' | sed 's/[><]//g') + lexicoUrl="https://www.lexico.com/" + _curl $lexicoUrl + word=$(echo $result | hxnormalize -x | hxselect -i "a.linkword" | grep -o '>.*<' | sed 's/[><]//g') _getdata _define } -- cgit v1.2.3