Skip to content

Instantly share code, notes, and snippets.

@JohnMertz JohnMertz/price.sh
Created Aug 12, 2017

Embed
What would you like to do?
Bash Price-Checker Robot
#!/bin/bash
# Price-Checker Robot
# All dependencies should be pre-installed (at least on Debian): bash, curl, echo, grep, sed, tail, date, bsd-mailx, bc, cut
# Alerts user if the price has dropped for defined items.
# Reduced items result in alerts to the defined recipient.
# Errors are sent to the maintainer
# All are logged to $HOME/.price.log
# Best run by a cronjob
getPrice() {
# An alogorithm must be defined for each $site to return the current price as a positive real number (without $). The following works for Mark's.
if [[ $site == "Marks" ]]; then
new=`curl $(echo $url) | grep actualPrice | sed 's/.*:"//' | sed 's/".*$//' | tail -n 1`
# elif [[ $site == "Some other site" ]]; then ...
# If site is not defined then return error and alert to maintainer
else
echo "! `date +%Y-%m-%d` No search algorithm for $site (item: $item). Sending error report." >> $HOME/.price.log
error="I don't have an algorithm defined for $site."
sendError
return
fi
# Price must be of format ##.## or #. If a character other than a dot or numeral is seen, report an error.
if grep -Pq [^\.0-9] <<< `echo $new`; then
echo "! `date +%Y-%m-%d` Parsed price doesn't meet requirements for $item. Sending error report." >> $HOME/.price.log
error="The price I got back when parsing the page was not a number. I found $new"
sendError
return
elif grep -Pq ^$ <<< `echo $new`; then
echo "! `date +%Y-%m-%d` Failed to parse a price for $item. Sending error report." >> $HOME/.price.log
error="When I parsed the page, I didn't find any results."
sendError
return
else
compare
return
fi
}
sendError() {
# Error alerts should be hard-coded to *your* maintainer address.
mail git@john.me.tz -s "Price-Checker has encountered a problem" <<< `echo "Help!
I've run into a problem while looking up the price for $item from $site at $url
$error
Please help!
Price-Checker Robot"`
error=""
return
}
compare() {
if [[ `bc <<< $(echo $old-$new) | cut -b 1` != 0 && `bc <<< $(echo $old-$new) | cut -b 1` != '-' ]]; then
echo "- `date +%Y-%m-%d` Price of $item from $site has dropped from $old to $new. Sending email to $alert" >> $HOME/.price.log
mail $alert -s "Price drop for $site $item" <<< `echo "Hello!
It is my pleasure to inform you that the $item that you were interested in from $site has dropped in price!
The price was formerly $old and is now $new.
You can find the item here: $url
Happy shopping!
Price-Checker Robot"`
elif [[ `bc <<< $(echo $old-$new) | cut -b 1` == 0 ]]; then
echo "= `date +%Y-%m-%d` Price of $item from $site is still at the old price of $old (now $new)" >> $HOME/.price.log
else
echo "+ `date +%Y-%m-%d` Price of $item from $site has increased from $old to $new" >> $HOME/.price.log
fi
return
}
# Alert email address. Re-define before subsequent items if you want the email sent to a different address.
# I block these alerts unless they originate from my internal network, so here is a working example
alert="git@john.me.tz"
item="Khakis"
site="Marks"
url="https://www.marks.com/en/categories/mens/other/sale-clearance/product/flextech-360-modern-slim-tapered-khakis-31128.html"
old="59.99"
getPrice
# Other's watched items
# alert="Set alert email here"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.