+ simple curl caching

dev
Luna 2021-06-06 20:33:30 +02:00
parent 6eb658d442
commit be5423ab9f
2 changed files with 36 additions and 3 deletions

32
cache_func.sh Normal file
View File

@ -0,0 +1,32 @@
#!/bin/bash
cache_folder="/tmp/pkp-tools"
cache_file="DomiIsCute"
cache_path="$cache_folder/$cache_file"
mkdir -p $cache_folder
function __curlWithCache(){
curl $@ > "$cache_path"
out=$(cat "$cache_path")
echo $out
}
function __fromCache(){
out=$(cat "$cache_path")
echo $out
}
function _curl(){
if [[ $cache_flag == "1" ]]; then
echo $(__curlWithCache $@)
elif [[ $cache_flag == "2" ]]; then
echo $(__fromCache $@)
elif [[ $cache_flag == "3" ]]; then
if [[ -f $cache_path ]]; then
echo $(__fromCache $@)
else
echo $(__curlWithCache $@)
fi
else
out=$(curl $@)
echo $out
fi
}

View File

@ -1,8 +1,9 @@
#!/bin/bash
source cache_func.sh
function getStations() {
a="$@"
s=$(jq -nr --arg v "$a" '$v|@uri')
data=$(curl -s "http://infopasazer.intercity.pl/?p=stations&q=$s")
data=$(_curl -s "http://infopasazer.intercity.pl/?p=stations&q=$s")
IFS=$'\n'
links=($(grep -Poh "\?p\=station\&id\=[0-9]+" <<< "$data"))
names=($(grep -i ">$@" <<< "$data" | tr -d '\r' | sed -s 's/<span class="">//g;s/ //g;s/<\/span>//g'))
@ -18,7 +19,7 @@ function getStations() {
function getTrains() {
url="http://infopasazer.intercity.pl/$(grep -Poh "^[^:]+" <<< $1)"
data="$(curl -s $url | tr -d '\r' | tr -d '\n' | grep -Poh "<tr.*?</tr>")"
data="$(_curl -s $url | tr -d '\r' | tr -d '\n' | grep -Poh "<tr.*?</tr>")"
IFS=$'\n'
trains=$(grep -Poh '<a href=".*?">.*?<br/>.*?</a>' <<< "$data")
@ -29,7 +30,7 @@ function getTrains() {
function getTrainInfo() {
IFS=$'\n'
res=($(curl -s "http://infopasazer.intercity.pl/?p=train&id=$1" | tr -d '\n\r' | sed -E 's/ //g' | grep -Poh '<tr class="_TableDelayCol.*?</tr>'))
res=($(_curl -s "http://infopasazer.intercity.pl/?p=train&id=$1" | tr -d '\n\r' | sed -E 's/ //g' | grep -Poh '<tr class="_TableDelayCol.*?</tr>'))
unset IFS
}