diff --git a/bash/picheck-sms.sh b/bash/picheck-sms.sh new file mode 100755 index 0000000..6efb9b3 --- /dev/null +++ b/bash/picheck-sms.sh @@ -0,0 +1,191 @@ +#!/bin/bash + +# MSRP: +# Pi 4B 8GB - $75 +# Pi 4B 4GB - $55 +# Pi 4B 2GB - $35 +# Pi 4B 1GB - $35 +# Pi 3B+ - $35 +# Pi ZeroW - $10 +# Pi ZeroWH - $16 +# Pi Zero - $5 + +# CRON: +# m h dom mon dow command +#*/2 * * * * /home/cave/scripts/picheck-sms --show-cm --hide zebra +# The --hide zebra is only there to show all results since this is really hacked together + +url="https://rpilocator.com" + +sms_account="YOUR_TWILIO_ACCOUNT_ID" +sms_key="YOUR_TWILIO_API_KEY" +sms_userauth="${sms_account}:${sms_key}" + +sms_to="YOUR_PHONE_NUMBER" +sms_from="YOUR_TWILIO_NUMBER" +sms_body="" + + + +if [[ "$@" == *"--help"* ]]; then + echo "halp" + exit 0 +elif [[ "$@" == *"--url"* ]]; then + echo $url && exit 0 +fi + + +antispam=~/pichecksms-sent +minute=`date +%M` +[[ $minute -eq 0 && -f $antispam ]] && echo "removing $antispam" >> ~/pisms.log && rm $antispam +[ -f $antispam ] && _minute=`cat $antispam` +let mindiff=minute-_minute +[[ $mindiff -le 5 && -f $antispam ]] || rm $antispam +echo "Checking [`date`]" >> ~/pisms.log + +tdir=`mktemp -d` +tfile=pidata.txt +# Hide all CM4 results (1) or show (0) +hidecm=1 # Default behavior +region="US" +[[ "$@" == *"--hide-cm"* ]] && hidecm=1 +[[ "$@" == *"--show-cm"* ]] && hidecm=0 + +[ $hidecm -eq 1 ] && hidestr="cm" || hidestr="" +if [[ "$@" == *"--hide"* ]]; then + findme="--hide";gotit=0 + for i in $@; do + if [ $gotit -eq 1 ]; then + [[ $i == "--"* ]] && echo "${bld}${red}Invalid value for --hide: ${rst}${red}${i}${rst}" && exit 1 + [ -z $hidestr ] && hidestr=$i || hidestr="${hidestr}|${i}" + gotit=0 + fi + [[ $i == "$findme" ]] && gotit=1 + done + [ $gotit -eq 1 ] && echo "${bld}${red}Missing value for ${rst}${red}--hide${rst}" && exit 1 +fi + +delim='|' +num=1 +pseudoarray=() +edata='' +today=`date +%d-%b-%y` +nonday=0 +nondayoffset=2 + +function extract() { + edata='' + offset=${4:-0} + let f2=${offset}+2 + edata=`echo "$1" | cut -d${2} -f${f2} | cut -d${3} -f1` +} + +function goodprice () { + _cprice=`echo $cprice | cut -d. -f1` + [[ `echo $cmodel | grep "4B 1GB"` && $_cprice -gt 35 ]] && gprice=0 + [[ `echo $cmodel | grep "4B 2GB"` && $_cprice -gt 35 ]] && gprice=0 + [[ `echo $cmodel | grep "4B 4GB"` && $_cprice -gt 55 ]] && gprice=0 + [[ `echo $cmodel | grep "4B 8GB"` && $_cprice -gt 75 ]] && gprice=0 + [[ `echo $cmodel | grep "Zero2W"` && $_cprice -gt 15 ]] && gprice=0 +# [[ `echo $cmodel | grep "4B 4GB"` && $_cprice -gt 55 ]] && gprice=0 + [[ `echo $cmodel | grep "CM4"` ]] && gprice=2 +} + +# Lines: +# 1: "table success" lol +# 2: SKU +# 3: Description +# 4: Link +# 5: Last updated +# 6: Retailer +# 7: In stock +# 8: Last Stock +# 9: Price +# Array: (2,3,5,6,8,9) + +waitingmsg="Retrieving data..." +[ $hidecm -eq 1 ] && waitingmsg="$waitingmsg (Hiding `echo $hidestr` results)" +echo -n "$waitingmsg" +curl $url 2>/dev/null| grep -A8 -i "table-success" > $tdir/$tfile +tput el1 +tput cub ${#waitingmsg} + +array='' +while read -r line; do + if [[ $line == "--" ]]; then + num=1 + array=`echo "$array" | cut -c2-` + # https://linuxhint.com/bash_append_array/ + pseudoarray[${#pseudoarray[@]}]=$array + array='' + continue + fi + [[ $num -eq 1 || $num -eq 4 || $num -eq 7 ]] && let num=num+1 && continue + [ $num -eq 9 ] && extract "$line" '>' '<' 1 || extract "$line" '>' '<' + edata=`echo $edata | sed "s/ RAM//g;s/Raspberry Pi/RPi/g;s/Zero /Zero/g;s/ W/W/g;s/ Model //g;s/ -//g;s/ MMC/MMC/g;s/, / /g;s/min\./min/g;s/WithWifi/w\/wifi/g;s/RPi/Pi/g;s/Pi CM4/CM4/g"` + array="${array}${delim}${edata}" + if [[ $num -eq 8 && "$edata" != "$today" ]]; then + nonday=1 + fi + let num=num+1 +done <$tdir/$tfile + +array=`echo "$array" | cut -c2-` +# https://linuxhint.com/bash_append_array/ +pseudoarray[${#pseudoarray[@]}]=$array + +rm -rf $tdir + +checkval=0 +for checkdata in "${pseudoarray[@]}"; do + # https://stackoverflow.com/a/44109240/18499418 + [[ "${checkdata,,}" != *"(${region,,})"* ]] && continue + [[ "${checkdata,,}" =~ ${hidestr,,} ]] || checkval=1 +done +[ $checkval -eq 0 ] && echo "No results found for the given filters (ex: $hidestr) in the given region (${region})." >> ~/pisms.log && exit 2 + +#head=('SKU' 'Description' 'Last Checked' 'Retailer' 'Last Checked' 'Price') +cmodel='' +cprice='' +sms_string="" +for adata in "${pseudoarray[@]}"; do + [[ "${adata,,}" != *"(${region,,})"* ]] && continue + [[ "${adata,,}" =~ ${hidestr,,} ]] && continue + gprice=1 + for i in `seq 1 ${#widths[@]}`; do + [ $i -ge ${#widths[@]} ] && continue + [[ $i -eq 4 && $nonday -eq 0 ]] && continue + let j=i+1 + mdata=`echo -en "$adata" | cut -d"$delim" -f$j` + [ $i -eq 1 ] && cmodel=$mdata + [ $i -eq 5 ] && cprice=$mdata && goodprice + done + if [ $gprice -eq 1 ]; then + if [ -z "$sms_string" ]; then + sms_string="`echo $adata | awk -F'|' '{print $2,"at",$4,"for",$6}'`" + else + sms_string="${sms_string} / `echo $adata | awk -F'|' '{print $2,"at",$4,"for",$6}'`" + fi + fi +done + +minute=`date +%M` +[ $minute -eq 0 ] && rm $antispam + +[ -f $antispam ] && _minute=`cat $antispam` +let mindiff=minute-_minute +[[ $mindiff -le 5 && -f $antispam ]] && echo "antispam-exit" >> ~/pisms.log && exit 3 + +smsdate=`TZ='America/New_York' date +%I:%M%_P` + +echo $minute > $antispam + +echo -e $sms_string +sms_body="[${smsdate}] ${sms_string}" +sms_response=`curl -X POST https://api.twilio.com/2010-04-01/Accounts/$sms_account}/Messages.json --data-urlencode "Body=${sms_body}" --data-urlencode "From=${sms_from}" --data-urlencode "To=${sms_to}" -u ${sms_userauth} 2>/dev/null` + + +echo -e "___________________________________________________________\n" >> ~/pisms.log +echo "$sms_body" >> ~/pisms.log +echo "$sms_response" >> ~/pisms.log +echo -e "___________________________________________________________\n" >> ~/pisms.log