Skip to content

Commit

Permalink
product-pricelimiter.sh configurable decimal places
Browse files Browse the repository at this point in the history
  • Loading branch information
oh2fih committed Aug 8, 2024
1 parent 3f7612b commit 41da1ad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Miscellaneous scripts for different purposes. Mostly unrelated to each other.
| Infosec<br>Automation | [`make-mac-prefixes.py`](bin/make-mac-prefixes.py)<br>Python 3 | Processes registered MAC address prefixes from [IEEE MA-L Assignments (CSV)](https://standards.ieee.org/products-programs/regauth/) (stdin) to Nmap's [`nmap-mac-prefixes`](https://github.com/nmap/nmap/blob/master/nmap-mac-prefixes) (stdout) with a few additional unregistered OUIs.<br>`curl https://standards-oui.ieee.org/oui/oui.csv \| make-mac-prefixes.py > nmap-mac-prefixes` |
| WordPress | [`test-cache-enabler.py`](bin/test-cache-enabler.py)<br>Python 3 | Tests whether the Cache Enabler by KeyCDN (WordPress) is working properly on the URLs given as arguments.<br>`test-cache-enabler.py https://example.com [...]` |
| Web | [`detect-modified-html-element.sh`](bin/detect-modified-html-element.sh)<br>Shell (bash) | Checks HTML element changes on a web page since last run. Configured via environment variables.<br>Recommended to be executed as a SystemD [service](systemd/detect-modified-html-element.service.example). |
| Web | [`product-pricelimiter.sh`](bin/product-pricelimiter.sh)<br>Shell (bash) | Compare product price on a web page with a given maximum price. Use, e.g., developer tools on your browser to find the HTML element containing the price.<br>`product-pricelimiter.sh -u ProductURL -s Selector [-m MaxPrice] [-n N]` |
| Web | [`product-pricelimiter.sh`](bin/product-pricelimiter.sh)<br>Shell (bash) | Compare product price on a web page with a given maximum price. Use, e.g., developer tools on your browser to find the HTML element containing the price.<br>`product-pricelimiter.sh -u URL -s Selector [-m MaxPrice] [-n N] [-d N]` |
| Web | <del>`koronarokotusaika.sh`</del><br>Shell (bash) | This script has been removed as koronarokotusaika.fi (bookcovidvaccine.fi) has been shut down on April 28, 2023. |
| Web | <del>`xxl-product-pricelimiter.sh`</del><br>Shell (bash) | This script has been replaced by the generalized [`product-pricelimiter.sh`](bin/product-pricelimiter.sh) as the HTML structure of XXL.fi has changed. |

Expand Down
41 changes: 28 additions & 13 deletions bin/product-pricelimiter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ read -r -d '' USAGE << EOM
# ------------------------------------------------------------------------------
# Compare product price on a web page with a given maximum price.
#
# Usage: product-pricelimiter.sh -u ProductURL -s Selector [-m MaxPrice] [-n N]
# Usage: product-pricelimiter.sh -u URL -s Selector [-m MaxPrice] [-n N] [-d N]
#
# -u ProductURL web page URL to fetch the current price from
# -s Selector the HTML element containing the price; search the price from
# elements or attributes that match a (CSS) selector (e.g.
# h3, #id, .class or combinations like "#id div.class")
# -m MaxPrice maximum price used for comparison; float number
# -n N in case there are multiple floats in the element, choose Nth
# -u URL web page URL to fetch the current price from
# -s Selector the HTML element containing the price; search the price from
# elements or attributes that match a (CSS) selector (e.g.
# h3, #id, .class or combinations like "#id div.class")
# -m MaxPrice maximum price used for comparison; float number
# -n N in case there are multiple floats in the element, choose Nth
# -d N use N decimals in the currency; requires 'bc' (default: 2)
#
# Exit codes:
#
# 0 OK Price is found and lower than or equal with the MaxPrice.
# 1 ERROR An error has occured; unable to tell the result.
# 2 WAIT Price is found but higher than the MaxPrice.
#
# Requires html-xml-utils (hxselect & hxnormalize).
# It is recommended to have 'bc' installed for more accurate comparison.
#
# Author : Esa Jokinen (oh2fih)
Expand All @@ -33,9 +35,10 @@ UA+="(KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
# Validate arguments

n=1
d=2
INVALID=0

while getopts ":hu:s:m:n:" opt; do
while getopts ":hu:s:m:n:d:" opt; do
case ${opt} in
h)
# Allow -h for help; invalid or missing arguments prints the usage anyway.
Expand All @@ -61,10 +64,22 @@ while getopts ":hu:s:m:n:" opt; do
fi
;;
n)
if [[ "$OPTARG" =~ ^[0-9]+$ ]] ; then
if [[ "$OPTARG" =~ ^[0-9]+$ ]]; then
n="$OPTARG"
else
echo -e "\033[0;31mN (-n) should be an integer!\033[0m" >&2
echo -e "\033[0;31mNth element (-n) should be an integer!\033[0m" >&2
((INVALID=INVALID+1))
fi
;;
d)
if [[ "$OPTARG" =~ ^[0-9]+$ ]]; then
d="$OPTARG"
else
echo -e "\033[0;31mDecimals (-d) should be an integer!\033[0m" >&2
((INVALID=INVALID+1))
fi
if (( d > 14)); then
echo -e "\033[0;31mNo currency has >14 decimals (-d)!\033[0m" >&2
((INVALID=INVALID+1))
fi
;;
Expand Down Expand Up @@ -156,7 +171,7 @@ fi

price=$(echo "$prices" | head -n "$n" | tail -n 1)
if command -v bc &> /dev/null; then
price=$(echo "scale=2; ${price}/1" | bc | sed 's/^\./0./')
price=$(echo "scale=${d}; ${price}/1" | bc | sed 's/^\./0./')
fi
echo -e "\033[0;32mThe price (#${n} in '${selector}') is now ${price}\033[0m"

Expand All @@ -165,9 +180,9 @@ echo -e "\033[0;32mThe price (#${n} in '${selector}') is now ${price}\033[0m"

if [ -n "$maxprice" ]; then
if command -v bc &> /dev/null; then
maxprice=$(echo "scale=2; ${maxprice}/1" | bc | sed 's/^\./0./')
maxprice=$(echo "scale=${d}; ${maxprice}/1" | bc | sed 's/^\./0./')
diff=$(
echo "scale=2; (${maxprice} - ${price})/1" \
echo "scale=${d}; (${maxprice} - ${price})/1" \
| bc \
| sed 's/-//' \
| sed 's/^\./0./'
Expand Down

0 comments on commit 41da1ad

Please sign in to comment.