-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenew.sh
212 lines (191 loc) · 8.07 KB
/
renew.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
# this script will renew all myturn.com checked out items to the max
# it requires that you have curl and jq installed on your machine
# set the login information variables below or the script will prompt you for them
# you can optionally have the script retain answers to the prompts which is slightly more secure
# syntax: ./renew.sh [-f|p|c|d|i]
# options:
# -f fetch cookie
# -p pull the latest loan list
# -c read out the loan items
# -d delete the files & settings created by this script
# -h print help
# global variables
## login information to fetch cookie
USERNAME="" # your username
PASSWORD="" # your password
## file naming
PULL="raw-current-loans.json"
CURRENT="current-loans.json"
COOKIE="cookie"
ENV="env-renew"
## URLs
BASE=""
URL="https://login.myturn.com/library/"
# check if curl and jq are install
function cmdExists() { command -v "$1" >/dev/null 2>&1; }
FAIL=false
if ! cmdExists curl; then echo "curl is not installed. Please install it to proceed." && FAIL=true; fi
if ! cmdExists jq; then echo "jq is not installed. Please install it to proceed." && FAIL=true; fi
[[ $FAIL == true ]] && exit 1
# if the username and password variables are set in this file, remove them from the env file if it exists
[ -n "$USERNAME" ] && [ -f $ENV ] && sed -i '/USERNAME/d' $ENV
[ -n "$PASSWORD" ] && [ -f $ENV ] && sed -i '/WORD/d' $ENV
# if the env file exists source it for saved variables
[ -f $ENV ] && source $ENV
# function to test if valid cookie file exists for pull; else fetch cookie
function Cookie() {
if [ -f $COOKIE ] && [ -n "$BASE" ] && find $COOKIE -mmin -10 | grep -q .; then
echo "recent cookie found and being tested"
CCODE=$( curl ${BASE}myAccount/editMembership -b $COOKIE -s -w "%{http_code}" -o /dev/null)
[ "$CCODE" -ne "200" ] && echo "cookie was tested and found to be useless; we will attempt to replace her" && fetchCookie
else
fetchCookie
fi
}
# function to fetch a cookie from the server
function fetchCookie() {
if [ -z "$USERNAME" ]; then
read -p "username: " USERNAME
MEM_U=true
[ -f $ENV ] && sed -i -e '/USERNAME/d' -e '/PASSWORD/d' $ENV
fi
ZAP=$(echo -n "$USERNAME" | openssl dgst -md5 | cut -d ' ' -f 2)
[ -n "$PASSWORD" ] && PASSWORD=$(echo -n "$PASSWORD" | openssl enc -aes-256-cbc -salt -pass pass:"$ZAP" -pbkdf2 -base64 -A)
[ -n "$WORD" ] && PASSWORD="$WORD"
if [ -z "$PASSWORD" ]; then
read -s -p "password: " PASSWORD && echo
PASSWORD=$(echo -n "$PASSWORD" | openssl enc -aes-256-cbc -salt -pass pass:"$ZAP" -pbkdf2 -base64 -A)
read -p "Do you want to save these credentials? (y/n): " MEM_L
fi
echo "attempting to fetch cookie from myturn.com as user \"${USERNAME}\""
[ -f $COOKIE ] && find $COOKIE -mmin -10 | grep -q . && mv $COOKIE ${COOKIE}_bkp
PAZ=$(echo "$PASSWORD" | openssl enc -d -aes-256-cbc -salt -pass pass:"$ZAP" -pbkdf2 -base64)
SESH=$(curl ${URL}j_spring_security_check -d "j_username=${USERNAME}&j_password=${PAZ}" -c $COOKIE -s -w "%header{location}")
if [[ "$SESH" == *"authfail"* ]]; then
[ -f $ENV ] && source $ENV
[ -f $ENV ] && rm -v $ENV
echo "cookie fetch has failed; check your username and password"
[ -f ${COOKIE}_bkp ] && mv ${COOKIE}_bkp $COOKIE
[ -n "$BASE" ] && echo "export BASE=\""$BASE"\"" >> $ENV
exit 1
elif [[ "$SESH" == *"library"* ]]; then
echo "cookie fetch success!"
if [[ $MEM_L == "y" ]]; then
[ -f $ENV ] && sed -i -e '/USERNAME/d' -e '/PASSWORD/d' $ENV
[[ $MEM_U == true ]] && echo "export USERNAME=\"$USERNAME\"" >> $ENV
echo "export WORD=\""$PASSWORD"\"" >> $ENV
fi
[ -f ${COOKIE}_bkp ] && rm -f ${COOKIE}_bkp
[ -f $ENV ] && source $ENV
[ -n "$BASE" ] && sed -i '/BASE/d' $ENV
BASE=$(curl ${URL}login/redirectToOrg -b $COOKIE -s -w "%header{location}")
echo "export BASE=\""$BASE"\"" >> $ENV
sed -i 's/_login/_/; s/FALSE/TRUE/' $COOKIE
[ -z "$MEMBERSHIP_ID" ] && getId
return 0
else
[ -f $ENV ] && rm -v $ENV
echo "unknown login error at location \""$SESH"\"; check domain"
exit 1
fi
}
# function to retrieve the membership type id from the server and save it to the env for renewal requests
function getId() {
[ -f $ENV ] && sed -i '/MEMBERSHIP_ID/d' $ENV
MEMBERSHIP_ID=$(curl -s ${BASE}myAccount/editMembership -b $COOKIE | grep -oP 'checkAgreementSignatures\(\K\d+')
echo "export MEMBERSHIP_ID=\"$MEMBERSHIP_ID\"">> $ENV
echo "your membership type id has been retrieved as \"$MEMBERSHIP_ID\""
}
# function to save to download the json of the users loans and save it as a file containing only the currently checked out items
function Pull() {
LSTMP=$(mktemp)
echo "attempting to pull the loans list from the server"
CODE=$(curl ${BASE}myLoans/listLoansJSON -b $COOKIE -w "%{http_code}" -o $LSTMP)
if [ $CODE -ne 200 ]; then
[ -f $ENV ] && rm -v $ENV
echo "the pull failed with http response code: \""$CODE"\""
echo "make sure you are logged in with option -l"
exit 1
fi
jq '[.data[]
| select(.isCheckedOut == true)
| walk(if . == null then "" else . end)]' $LSTMP > $PULL
echo "loan list pull success!"
}
# function to display the relevant info from the loan list file and save it as a new file
function Current() {
[ ! -f $PULL ] && echo "first pull the loan list with option -p" && exit 1
echo "displaying relevant info from $PULL and saving it as $CURRENT"; echo
jq '[.[]
| {item: (.item.displayName | gsub("\\\"";"in")), id: .item.internalId, renewable: .selfRenewalStatus.isRenewableNow, remaining: .selfRenewalStatus.renewalsLeft, max: .selfRenewalStatus.maxRenewDate, out: .checkedOutTimestamp, due: .dueDate}
| (.out, .due, .max) |= sub("T.*";"")
| with_entries(select(.value != "")) ]
| sort_by(.renewable == true | if . then 1 else 0 end)' $PULL > $CURRENT
jq '.' $CURRENT
}
# function called by the renew function to gather the data from the loan list file and prerform the renewal requests to the server
function renewLoop() {
jq -c '.[]
| select(.selfRenewalStatus.isRenewableNow == true)
| {item: (.item.displayName | gsub("\\\"";"in")), loans: .id, itemId: .item.id, dueDate_date: (.selfRenewalStatus.maxRenewDate | sub("T.*";"")), max: (.selfRenewalStatus.maxRenewDate | sub("T.*";""))}
| .dueDate_date |= (split("-") | "\(.|.[1] | tonumber)/\(.|.[2] | tonumber)/\(.|.[0])") | join("|")' $PULL | \
while IFS='|' read -r ITEM LOANS ITEM_ID DUE_DATE_DATE MAX; do
d=--data-urlencode
echo "renewing $ITEM until $MAX"
curl ${BASE}myLoans/renew -b $COOKIE \
$d "loans=$LOANS" \
$d "membershipId=$MEMBERSHIP_ID" \
$d "itemId=$ITEM_ID" \
$d "dueDate=struct" \
$d "dueDate_date=$DUE_DATE_DATE"
done
}
# function to login, pull the loan list, and loop through all renewable loan items to their maximum renewal date
# and then loop through re-pulling the loan list until all items are renewed as many times as allowed
function Renew() {
Cookie
Pull
while $(jq 'any(.selfRenewalStatus.isRenewableNow == true)' $PULL); do
[ -z "$MEMBERSHIP_ID" ] && getId
echo "renewable item(s) found in $PULL"
renewLoop
Pull
done
Current
echo;echo "there are zero renewable items; try again tomorrow";echo
}
function Delete() {
DELETE=("$PULL" "$CURRENT" "$COOKIE" "${COOKIE}_bkp" "$ENV")
for FILE in "${DELETE[@]}"; do
[ -f "$FILE" ] && rm -v "$FILE"
done
echo;echo "all files created by this script have been removed";echo
}
# function to display basic usage of this script
function Help() {
echo
echo "this script will renew all myturn.com checked out items to the max"
echo
echo "syntax: ./renew.sh [-f|p|c|d|h]"
echo "options:"
echo "-f fetch cookie"
echo "-p pull the latest loan list"
echo "-c read out the loan items"
echo "-d delete all files created by this script"
echo "-h print this help"
echo
}
# options to control this script
while getopts ":fpcdh" opt; do
case $opt in
f) fetchCookie; exit;;
p) Pull; exit;;
c) Current; exit;;
d) Delete; exit;;
h) Help; exit;;
\?) echo "Invalid option: -${OPTARG}."; exit 1;;
esac
done
# if no options are called default to running the renew function
Renew