Skip to content

Instantly share code, notes, and snippets.

@nunq
Last active October 22, 2021 12:56
Show Gist options
  • Save nunq/fb7757fa678a25e7504eccd5d2c44515 to your computer and use it in GitHub Desktop.
Save nunq/fb7757fa678a25e7504eccd5d2c44515 to your computer and use it in GitHub Desktop.
hugendubel "ist $buch in $laden verfügbar" checker -- doesnt work anymore bc javascript is now required to search on the site ... >.>
#!/bin/bash
# hugendubel ladenvorrat checker
# usage: ./instock.sh <ISBN13>
set -e -o pipefail
[[ -z "$1" ]] && echo -e "error: no isbn\n usage: ./instock.sh <ISBN13>" && exit 1
# check if $1 matches ISBN13 format
[[ ! $(grep -P '978[0-9\-]{10}' <<< "$1") ]] && echo "error: that's not an ISBN" && exit 1
# print info for X stores
NUM_RESULTS=3
fail_bookurl() {
echo "error: couldn't get \$BOOK_URL (likely because of reCAPTCHA)"
echo "please visit: https://www.hugendubel.de/captcha/?r=https://www.hugendubel.de/de/"
exit 1
}
printf "HUGENDUBEL IN STOCK CHECKER\n===========================\n\nchecking...\r"
# mask user agent
USER_AGENT="User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
# strip ISBN of any dashes
ISBN="$(tr -d "-" <<< "$1")"
# get coords for nearby retail stores
URLENC_COORDS="$(curl -sS -H "$USER_AGENT" https://ipapi.co | grep -oP '(?<=<td class="ipval" data-clipboard-text=").*?(?=")' | sed -n 7p | sed 's/^/\&lat=/;s/, /\&lon=/')"
# get book url of isbn using the search function
BOOK_URL="$(curl --compressed -sS -L -H "$USER_AGENT" -H 'Referer:https://www.hugendubel.de/de/' 'https://www.hugendubel.de/de/quickSearch?searchString='"$ISBN"'&facetNodeId=-1' | grep -oP '(?<=<link rel="canonical" href=").*?(?="/>)')" || fail_bookurl
# get book name
BOOK_NAME="$(curl --compressed -s -L -H "$USER_AGENT" -H 'Referer:https://www.hugendubel.de/de/' "$BOOK_URL" | grep -oP '(?<=<title>).*?(?=</title>)' | sed 's/ - portofrei//')" || BOOK_NAME="null (error)"
echo -e ""$BOOK_NAME"\n"
# get article id from book url
ARTICLE_ID="$(grep -oP '[0-9]*-produkt-details.html$' <<< "$BOOK_URL" | sed 's/-produkt-details.html//')"
# get info about article id
STORE_INFO_JSON="$(curl -sS 'https://www.hugendubel.de/ws/affiliate/getstockinfoaround?artiid='"$ARTICLE_ID""$URLENC_COORDS"'&dist=25')"
mapfile -t storeCity <<< "$(grep -oP '(?<="city":").*?(?=")' <<< "$STORE_INFO_JSON")"
mapfile -t storeName <<< "$(grep -oP '(?<="name":").*?(?=")' <<< "$STORE_INFO_JSON")"
mapfile -t inStock <<< "$(grep -oP '(?<="inStock":).*?(?=,)' <<< "$STORE_INFO_JSON")"
_pipeintocolumn="city;store name;in stock\n"
for (( i=0; i<"$NUM_RESULTS"; i++)); do
_pipeintocolumn+=""${storeCity[$i]}";"${storeName[$i]}";"${inStock[$i]}"\n"
done
echo -e "$_pipeintocolumn" | column -ts ";"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment