summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorars <asav1410@gmail.com>2020-10-28 19:59:57 +0000
committerars <asav1410@gmail.com>2020-10-28 19:59:57 +0000
commit9181864dac173179bbc817421935c48a0adf06f2 (patch)
tree4ce560db27760e7f2885cf6c64c98a6e359318f8
parentd31c1a3b3180f37c40585f870e830e3265cd3f1e (diff)
Move curls to a function and add rate limiting.
-rwxr-xr-xoedquery.sh50
1 files changed, 28 insertions, 22 deletions
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
}