Rich Mingin (PLUG) on 11 Jul 2016 08:21:09 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] Packt free ebook grabber script


A while back, Pennth (Rachel) mentioned yet another Packt free ebook that I'd just missed out on, and I got to Googling. I eventually found the script below (and attached), though I then did some additional hackery (I wanted to grab epub and mobi, not just pdf). I've been running this on my Raspberry Pi IRC host for some weeks now via cron, and it seems to be working well. I didn't write this, just mutilated it and am running it, but it seems pretty clean and simple, and I've gotten good results.

Like most FOSS licenses, if it breaks you can keep both pieces.



~/packt » crontab -l
0 14 * * * /home/forge/packt/fetch.sh >> /tmp/cron_output



~/packt » cat fetch.sh
#!/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="" \/-]+(?=\" 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 -i --cookie $login_cookie $URL_DOWNLOAD_BOOK"

        echo "Downloading PDF to $BOOK_LOCATION..."
        $COMMAND_DOWNLOAD_BOOK > "$BOOK_LOCATION"
        echo "Downloaded PDF to $BOOK_LOCATION"

        URL_epub_DOWNLOAD_BOOK="https://www.packtpub.com/ebook_download/$book_number/epub"
        BOOK_epub_LOCATION="$DOWNLOAD_PATH"/"$book_title".epub
        COMMAND_DOWNLOAD_epub_BOOK="curl -s -i --cookie $login_cookie $URL_epub_DOWNLOAD_BOOK"

        echo "Downloading epub to $BOOK_epub_LOCATION..."
        $COMMAND_DOWNLOAD_epub_BOOK > "$BOOK_epub_LOCATION"
        echo "Downloaded epub to $BOOK_epub_LOCATION"


        URL_mobi_DOWNLOAD_BOOK="https://www.packtpub.com/ebook_download/$book_number/mobi"
        BOOK_mobi_LOCATION="$DOWNLOAD_PATH"/"$book_title".mobi
        COMMAND_DOWNLOAD_mobi_BOOK="curl -s -i --cookie $login_cookie $URL_mobi_DOWNLOAD_BOOK"

        echo "Downloading mobi to $BOOK_mobi_LOCATION..."
        $COMMAND_DOWNLOAD_mobi_BOOK > "$BOOK_mobi_LOCATION"
        echo "Downloaded mobi to $BOOK_mobi_LOCATION"


else
        echo "Free book has added to your Packt account."
fi

Attachment: packtpub-free-learning.sh.gz
Description: GNU Zip compressed data

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug