This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
humblesync.sh
executable file
·163 lines (149 loc) · 3.68 KB
/
humblesync.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
# configuration
# set your username and password
USER=''
PASS=''
HUMBLE_URL="https://www.humblebundle.com"
# help
if [ "$1" == "--help" ]
then
echo ""
echo "./humblesync.sh [DIRECTORY] [KEY1 KEY2 ...]"
echo " DIRECTORY defaults to the current directory"
echo " if no keys are provided all available keys are retrieved"
echo ""
exit 0
fi
# get arguments
DATADIR="$1"
shift
KEYS=($@)
# check if username and password are set
if [ -z "$USER" ] || [ -z "$PASS" ]
then
echo "set your username and password by editing this script"
exit 1
fi
# check for required programs
MISSING=""
$(jq --version >/dev/null 2>&1) || MISSING="$MISSING jq"
$(curl --version >/dev/null 2>&1) || MISSING="$MISSING curl"
if [ -n "$MISSING" ]
then
echo >&2 "please install the following programs:$MISSING"
exit 1
fi
# functions
GET() {
local FILENAME="$1"
shift
curl -o "$FILENAME" -s --cookie ./logincookie \
--header "Accept: application/json" \
--header "Accept-Charset: utf-8" \
--header "Keep-Alive: true" \
--header "X-Requested-By: hb_android_app" \
--header "User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)" \
$@
}
ALIVE() {
local LOGINCHECK=$(GET - "$HUMBLE_URL/api/v1/" | wc -l)
[ $LOGINCHECK -gt 0 ] && return 0
return 1
}
LOGIN() {
curl --cookie-jar ./logincookie \
--header "Accept: application/json" \
--header "Accept-Charset: utf-8" \
--header "Keep-Alive: true" \
--header "X-Requested-By: hb_android_app" \
--header "User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)" \
--data "username=$USER" \
--data "password=$PASS" \
"${HUMBLE_URL}/login" &> /dev/null
}
CONNECT() {
ALIVE || LOGIN
ALIVE || echo >&2 "login failed"
}
extract() {
jq ".subproducts[].downloads[].download_struct[] | [ .md5, .file_size, .url.web ]" | \
sed -E -e 's/^[ ]*"?//g' \
-e 's/"?,?[ ]*$//g' \
-e 's/^\]/##NEWLINEPLACEHOLDER##/g' \
-e '/^[ ]*\[[ ]*$/d' | \
tr "\n" " " | \
sed -E -e 's/[ ]*##NEWLINEPLACEHOLDER##[ ]*/\n/g'
}
getkeys() {
CONNECT
GET - "$HUMBLE_URL/api/v1/user/order" | \
grep -o -E '"[A-Za-z0-9]{16}"' | \
tr -d '"'
}
getfiles() {
local KEY="$1"
CONNECT
GET - "$HUMBLE_URL/api/v1/order/${KEY}" | extract | \
while read line
do
local ITEM=($line)
local MD5="${ITEM[0]}"
local SIZE="${ITEM[1]}"
local URL="${ITEM[2]}"
local NAME=$(echo "$URL" | \
grep -o '/[^/]\+$' | \
sed -E -e 's/\?.*//g' -e 's/^\///g')
local FILENAME="$DATADIR/$NAME"
if [ -n "$FILENAME" ] && [ "$SIZE" != "null" ]
then
echo " '${NAME}' ($SIZE b)"
if [ -e "$FILENAME" ] && [ "$SIZE" == $(stat -c%s "$FILENAME") ]
then
echo " exists"
continue
fi
if ! ALIVE
then
echo " logout"
return 1
fi
echo " download"
GET "$FILENAME" "${URL}"
if [ -e "$FILENAME" ]
then
echo " done"
local SIZECHECK=$(stat -c%s "$FILENAME")
if [ "$SIZE" == "$SIZECHECK" ]
then
echo " size:$SIZECHECK b ok"
local MD5CHECK=$(md5sum "$FILENAME" | cut -d" " -f1)
if [ "$MD5" == "$MD5CHECK" ]
then
echo " md5sum:$MD5CHECK ok"
else
echo " md5sum:$MD5CHECK wrong. should be $MD5"
fi
else
echo " size:$SIZECHECK b wrong. should be $SIZE b"
fi
else
echo " error"
fi
fi
done && return 0
return 1
}
# main
[ -n "$DATADIR" ] || DATADIR='.'
[ ${#KEYS[@]} -gt 0 ] || KEYS=($(getkeys))
echo "sync ${#KEYS[@]} key(s)"
mkdir -p "$DATADIR"
for KEY in ${KEYS[@]}
do
while true
do
echo " '$KEY'"
getfiles "$KEY" && break
echo " retry"
done
done