/
verkkaaja.sh
executable file
·110 lines (101 loc) · 2.93 KB
/
verkkaaja.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
while [[ $# -gt 0 ]]; do
case $1 in
--login)
EMAIL="$2"
shift 2
;;
--password)
PASS="$2"
shift 2
;;
--url)
PRODUCT_URL="$2"
shift 2
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
if [ -z "$EMAIL" ]; then
printf 'Email: '
read -r EMAIL
fi
if [ -z "$PASS" ]; then
printf 'Password: '
read -sr PASS
fi
if [ -z "$PRODUCT_URL" ]; then
printf '\nProduct URL: '
read -r PRODUCT_URL
fi
# Login
if TOKEN=$(curl -s -f 'https://www.verkkokauppa.com/resp-api/login' \
-H 'Origin: https://www.verkkokauppa.com' \
-H 'Accept-Encoding: gzip, deflate, br' \
-H 'Accept-Language: en-US,en;q=0.8,fi;q=0.6' \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36' \
-H 'Content-Type: application/json' \
-H 'Accept: */*' \
-H 'Referer: https://www.verkkokauppa.com/fi/account/login/' \
-H 'Connection: keep-alive' \
--data-binary "{\"email\":\"$EMAIL\",\"password\":\"$PASS\"}" --compressed); then
echo Login succesful...
TOKEN=$(echo "$TOKEN" | jq '.track' | sed 's/\"//g')
else
echo ERROR: Login failed
exit 1
fi
# Get shopping cart
if CART=$(curl -s 'https://www.verkkokauppa.com/api/v2/mycart' \
-H 'Authorization: Bearer '"$TOKEN" \
-H 'Accept-Encoding: gzip, deflate, br' \
-H 'Accept-Language: en-US,en;q=0.8,fi;q=0.6' \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36' \
-H 'Accept: */*' \
-H 'Referer: https://www.verkkokauppa.com/fi/account/' \
-H 'Connection: keep-alive' --compressed); then
echo Shopping cart fetched...
CART=$(echo "$CART" | jq '.cartUuid' | sed 's/\"//g')
else
echo ERROR: Could not fetch shopping cart
exit 1
fi
# Get product SKU
if SKU=$(curl -s "$PRODUCT_URL"); then
echo Product information fetched...
SKU=$(echo "$SKU" | pup 'meta[itemprop="sku"] attr{content}')
else
echo ERROR: Could not fetch product info
exit 1
fi
echo Trying to add product to shopping cart...
# Release the hounds
while true; do
ERRORS=$(curl -s "https://www.verkkokauppa.com/api/v2/cart/${CART}?pid=${SKU}&quantity=1" \
-X PUT \
-H 'Authorization: Bearer '"$TOKEN" \
-H 'Origin: https://www.verkkokauppa.com' \
-H 'Accept-Encoding: gzip, deflate, br' \
-H 'Accept-Language: en-US,en;q=0.8,fi;q=0.6' \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36' \
-H 'Accept: */*' \
-H 'Referer: '"$PRODUCT_URL" \
-H 'Connection: keep-alive' \
-H 'Content-Length: 0' \
--compressed | jq '.errors')
if [ 4 -eq $? ]; then
echo Failed to parse response...
sleep 1
else
NO_ERRORS=$(echo "$ERRORS" | jq '. | length == 0')
if $NO_ERRORS; then
echo Product reserved. Quitting...
exit 0
fi
echo "$ERRORS"
fi
echo Trying again...
done