Created
November 15, 2020 08:09
-
-
Save watbulb/af6da33143baa5fe70228e3ef230a232 to your computer and use it in GitHub Desktop.
Simple posix shell script to check PS5 availability against multiple stores (curl & pup). Extend as appropriate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Simple script to check PS5 availability against multiple stores | |
# @watbulb | |
[[ -n $DEBUG ]] && set -x || : | |
set -e -o posix | |
AGENT="Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" | |
declare -a STORES=( | |
# Bestbuy (bestbuy.html) | |
https://www.bestbuy.ca/en-ca/product/playstation-5-digital-edition-console-online-only/14962184 | |
# Walmart (walmart.html) | |
'https://www.walmart.ca/search?q=playstation%205' | |
# XXX: Extend as appropriate | |
) | |
declare -a STORES_PUPS=( | |
'span.availabilityMessage_1MO75 text{}:Coming soon' | |
'div[data-product-id="6000202198823"] text{}:Not available' | |
# XXX: Extend as appropriate | |
) | |
function iterate_stores() { | |
for i in ${!STORES[@]}; do | |
# Grab our store name, selector and compare text | |
store_name=$(echo "${STORES[$i]}" | cut -d/ -f1-3) | |
pup_selector=$(echo "${STORES_PUPS[$i]}" | cut -d: -f1 ) | |
selector_cmp=$(echo "${STORES_PUPS[$i]}" | cut -d: -f2 ) | |
# Grab stock response and parse using pup | |
response=$(curl \ | |
-A "${AGENT}" \ | |
-H 'Cache-Control: no-cache' \ | |
-s "${STORES[$i]}" | pup "${pup_selector}") | |
# Check for nil-selector response | |
if [[ -z $response ]]; then | |
echo $store_name '-> NOT FOUND' | |
continue | |
fi | |
# Compare results | |
case $response in | |
*${selector_cmp}*) | |
echo $store_name '-> NOT IN STOCK';; | |
*) echo $store_name '-> IN STOCK';; | |
esac | |
done | |
} | |
# $1: update_interval: int | |
function main() { | |
if ! which -s pup; then | |
echo "[!] $(basename $0) requires tooling dependencies: (pup)" | |
return 255 | |
elif [[ $1 -le 0 ]]; then | |
echo "[!] usage: $(basename $0) <update-interval(seconds)>" | |
return 255 | |
fi | |
while true; do | |
iterate_stores | |
sleep $1 | |
done | |
return 0 | |
} | |
main $@ | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/ericchiang/pup