-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacktpub-free-learning.sh
executable file
·79 lines (63 loc) · 2.5 KB
/
packtpub-free-learning.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
#!/bin/bash
export PATH=/bin:/usr/bin:$PATH
# Login Credentials
USERNAME=""
PASSWORD=""
CREDENTIALS_FILE="$HOME/.packt"
# Options
DOWNLOAD=$PACKT_DOWNLOAD
DOWNLOAD_PATH="$HOME/packt"
# Log running information
echo "Date: $(date)"
if [ -z "$DOWNLOAD" ]; then
DOWNLOAD="Y"
fi
if [ -f $CREDENTIALS_FILE ];
then
credentials_base64=`cat $CREDENTIALS_FILE`
credentials=`echo $credentials_base64 | base64 --decode`
USERNAME=`echo $credentials | awk -F":" '{print $1}'`
PASSWORD=`echo $credentials | awk -F":" '{print $2}'`
fi
if [ -z "$USERNAME" ] || [ -z $PASSWORD ]
then
echo "Please enter your Packt username (email): "
read USERNAME
echo "Please enter your Packt password: "
read PASSWORD
credentials_base64=`echo "$USERNAME:$PASSWORD" | base64`
echo $credentials_base64 > $CREDENTIALS_FILE
chmod 400 $CREDENTIALS_FILE
echo "Your credentials was saved to $CREDENTIALS_FILE in base64 encoded format."
fi
# Constant
TMP_FILE="/tmp/free-learning.txt"
URL_LOGIN="https://www.packtpub.com"
COMMAND_LOGIN="curl -s -i -X POST -d email=$USERNAME&password=$PASSWORD&op=Login&form_id=packt_user_login_form $URL_LOGIN"
URL_FREE_LEARNING="https://www.packtpub.com/packt/offers/free-learning"
COMMAND_FREE_LEARNING="curl -s -X GET $URL_FREE_LEARNING"
response_login=$($COMMAND_LOGIN > $TMP_FILE)
login_cookie=$(cat $TMP_FILE | grep Set-Cookie | tail -1 | grep -Po "Set-Cookie: (SESS_live=*\w*)" | cut -d\ -f2)
if [[ -z "$login_cookie" ]]; then
login_cookie=$(cat $TMP_FILE | grep Set-Cookie | tail -2 | grep -Po "Set-Cookie: (SESS_live=*\w*)" | cut -d\ -f2)
fi
response_freelearning=$($COMMAND_FREE_LEARNING)
book_title=$(echo $response_freelearning | grep -Po "(?<=<div class=\"dotd-title\"> <h2> )[\w .-:]+(?= <\/h2> <\/div>)")
book_href=$(echo $response_freelearning | grep -Po "(?<=<a href=\")[\w \/-]+(?=\" class=\"twelve-days-claim\")")
book_number=$(echo $book_href | cut -d/ -f3)
book_url=$URL_LOGIN$book_href
echo "Today's free book: $book_title"
echo "Claim url: $book_url"
COMMAND_CLAIM_FREE_BOOK="curl -i -s --cookie $login_cookie $book_url"
$COMMAND_CLAIM_FREE_BOOK > /dev/null 2>&1
if [ "Y" = "$DOWNLOAD" ]; then
mkdir -p $DOWNLOAD_PATH
URL_DOWNLOAD_BOOK="https://www.packtpub.com/ebook_download/$book_number/pdf"
BOOK_LOCATION="$DOWNLOAD_PATH"/"$book_title".pdf
COMMAND_DOWNLOAD_BOOK="curl -s -L --cookie $login_cookie $URL_DOWNLOAD_BOOK"
echo "Downloading to $BOOK_LOCATION..."
$COMMAND_DOWNLOAD_BOOK > "$BOOK_LOCATION"
echo "Downloaded to $BOOK_LOCATION"
else
echo "Free book has added to your Packt account."
fi