Skip to content

Instantly share code, notes, and snippets.

@nunq
Last active November 3, 2023 22:52
Show Gist options
  • Save nunq/432eabf4c403abdd0e51b33bd4962689 to your computer and use it in GitHub Desktop.
Save nunq/432eabf4c403abdd0e51b33bd4962689 to your computer and use it in GitHub Desktop.
tools für amazon wishlists mit büchern
#!/bin/bash
# usage: ./genbooklist WISHLIST_ID
# please make sure to set $FILE to the desired output file
set -eu -o pipefail
ID="$1"
FILE=""
# if you're not from germany, change the tld
URL="https://www.amazon.de/hz/wishlist/printview/""$ID"
BOILERPLATE="<!DOCTYPE html><html><head><title>""$2""</title><meta charset='utf-8'><meta name='viewport' content='width=device-width, initial-scale=1'><style>#wrapper {display: grid;grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));grid-gap: 1rem;font-size: 70%;margin: 0;max-width: 5000px;padding-left: 1.5rem;padding-right: 1.5rem;}#wrapper > div {border: 1px solid black;border-radius: 1rem;display: flex;flex-flow: column;align-items: center;padding: 1.25rem;margin: 0;}h2 {margin-top: 1.5rem;margin-bottom: -0.35rem;}h1, h2, h3, #header {text-align: center;}body {margin-bottom: 4rem;}#header {min-height: 6rem;margin-top: 3rem;}img {max-width: 85%;}h1 {margin-bottom: 0;}</style></head><body><div id='header'><h1>""$2""</h1><span><i>generated by <a href='https://gist.github.com/nunq/432eabf4c403abdd0e51b33bd4962689'>genbooklist.sh</a></i></span></div><div id='wrapper'>"
echo "$BOILERPLATE" > "$FILE"
mapfile -t BOOKS <<< "$(curl -s "$URL" --compressed -H "Mozilla/5.0 (X11; Linux x86_64; rv:106.0) Gecko/20100101 Firefox/106.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" | grep -oP '(?<=class="a-align-center g-print-view-row">).*?(?=</tr>)')"
for (( i=0; i<"${#BOOKS[@]}"; i++ )) do
IMGURL="$(grep -oP '(?<=src=").*?(?=")' <<< "${BOOKS[i]}" | sed 's/_SS135_/_SL500_/')"
TITLE="$(grep -oP '(?<=<td class="a-align-center"><span class="a-text-bold">).*?(?=</span><br/>)' <<< "${BOOKS[i]}" | sed -r 's/ \(.*?\)$//g' | sed -r 's/(.*?)[:\\.]/\1/' | sed -r 's/(Ein )?Roman//' | sed -r 's/Leinen.*//')"
AUTHOR="$(grep -oP '(?<=von: ).*?(?=\()' <<< "${BOOKS[i]}")"
sed -i "/<div id='wrapper'>/a <div><img src='""$IMGURL""'><h2>""$TITLE""</h2><h3>""$AUTHOR""</h3></div>" "$FILE"
done
echo "</div></body></html>" >> "$FILE"
# rechnet aus, wie teuer der lesespaß sein wird.
# depends: gnu grep, bc
# nur einmal die id der öffentlichen oder link-geteilten liste vorne angeben
# funktioniert in bash.
AMZN_WL_ID=<WISHLIST ID>; curl -s "$URL" --compressed -H "Mozilla/5.0 (Windows 6.4; WOW64 rv:44.0) Gecko/20100101 Firefox/65.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" https://www.amazon.de/hz/wishlist/printview/$AMZN_WL_ID | grep -oP '(?<=class="a-align-center g-print-view-row">).*?(?=</tr>)' | grep -oP '(?<=</td><td class="a-text-center a-align-center"><span>).*?(?= €</span>)' | sed 's/,/\./g' | tr '\n' '+' | sed 's/+$/\n/' | bc -l
#!//bin/bash
set -eu -p pipefail
# this wraps genbooklist.sh
# example:
~/genbooklist-multi.sh XYZXYZXYZ fiction && echo "fiction done."
~/genbooklist-multi.sh ABCABCABC nonfiction && echo "nonfiction done."
~/genbooklist-multi.sh CDECDECDE philosophy && echo "philosophy done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment