commit ecfb2bf6258ab48eec31c35a5494fe5e175b7d2c
parent 431c6ff5e5f7f8847911183b2df2ca392883dd05
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Thu, 10 Sep 2020 11:58:58 -0700
Refactor, implement functions, and use w3.
Diffstat:
1 file changed, 16 insertions(+), 15 deletions(-)
diff --git a/scripts/.local/bin/webq b/scripts/.local/bin/webq
@@ -4,32 +4,33 @@
set -eu
# settings
-user_agent="shell script to use searx without a browser"
+useragent="shell script to use searx without a browser"
ttl=1024
-request_delay=12
+delay=12
# display usage
usage() { echo "usage, `basename $0` [query ...]" ;}
+curl() { command curl -sSH "User-Agent: $useragent" $@ ;}
+format() { cut -d',' -f1,2 | egrep -v ",$" ;}
if [ $# -eq 0 ]; then
usage; exit 1
fi
-requests=0
+first=1
while [ $# -ne 0 ]
do
- # $1 is unquoted so extra whitespace is delt with.
- query="$(echo -n $1 | sed -E "s/ /%20/g")"
- cache="$HOME/.cache/webq/$query"
- [ -f "$cache" ] && lifetime=$((`date +%s` - `stat -f %c $cache`)) || lifetime="-1"
-
- if [ $lifetime -gt $ttl -o $lifetime -lt 0 ]; then
- [ $requests -gt 0 ] && sleep $request_delay
-
- # egrep removes suggestions and such.
- curl -sSH "User-Agent: $user_agent" "$(w3 -o $1)&format=csv" | cut -d"," -f1,2 | egrep -v ",$" > "$cache"
- requests=$(($requests + 1))
+ cache="$HOME/.cache/webq/`echo $1 | sed -E "s/[[:space:]]+/-/g"`"
+ if [ -f "$cache" ]; then
+ lifetime=$((`date +%s` - `stat -f %c $cache`))
+ else
+ lifetime=-1
+ fi
+ if [ $lifetime -gt $ttl ] || [ $lifetime -lt 0 ]; then
+ [ $first -eq 1 ] && sleep $delay
+ curl "`w3 -o "$1"`&format=csv" | format > $cache
+ first=0
fi
- cat "$cache"
+ cat $cache
shift
done