2016年8月25日 星期四

利用 Bash 程式下載 Packt 免費電子書並上傳 Git 及發出通知

之前改編的「利用 Bash 程式下載 Packt 免費電子書」完成後,還是想做更多。要是下載後能直接上傳 GitLab、自動輸入備註、再發送電郵通知就完滿了。於是著手動工。 原本的 Bash 程式在 CentOS 下不用改也能順利執行。因此,只需要加入提交到 Git 及發送電郵便能成事:
#!/bin/bash

##----------------------------------------------------------------------------------------
##  Packt Free eBook Downloader
##----------------------------------------------------------------------------------------
##  Platform: CentOS7 + bash
##  Written by Pacess
##  Copyright 2016 Pacess Studio.  All rights reserved.
##----------------------------------------------------------------------------------------

##----------------------------------------------------------------------------------------
##  Update History:
##----------------------------------------------------------------------------------------
##  2016.08.25
##  - Added auto commit to GitLab
##----------------------------------------------------------------------------------------

##  Variables
userID="sita@chan.com"
password="Sita0310"

timeOut=5
noOfRetry=3
sleepTimeBetweenRequest=1

userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" 
downloadDirectory="/Users/pacessho/MEGA/eBooks"
log="packt_igustin.log"
cookie="cookie.txt"

##----------------------------------------------------------------------------------------
function PHLog  {
    echo "$1"
    echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$log"
}

##----------------------------------------------------------------------------------------
##  Program start
echo "$(date '+%Y-%m-%d %H:%M:%S')"
echo "-----------------------------------------------------------"
echo "--  Packt Free eBook Downloader Version 1.10             --"
echo "--  Written by Pacess                                    --"
echo "--  Copyright 2016 Pacess Studio.  All rights reserved.  --"
echo "-----------------------------------------------------------"
echo ""

##  Move to home directory first
cd /root/PacktDownloader

##  Remove previous temp file
rm -f $cookie packt*.html

##----------------------------------------------------------------------------------------
##  Website login
PHLog "Logging in $userID..."
curl -s --retry $noOfRetry -m $timeOut -A "$userAgent" -b "$cookie" -c "$cookie" -d "email=$userID" -d "password=$password" -d "op=Login" -d "form_build_id=form-73ba86bbfb2a50719049129632c84810" -d "form_token=2f1d586bf7df196b77d0761709d03199" -d "form_id=packt_user_login_form" https://www.packtpub.com
errorCode=$?;  test "$errorCode" -ne "0" && { PHLog "curl exit error code: $errorCode";  exit; }

curl -s --retry $noOfRetry -m $timeOut -A "$userAgent" -b "$cookie" -c "$cookie" https://www.packtpub.com/packt/offers/free-learning > packt_daily.html
errorCode=$?;  test "$errorCode" -ne "0" && { PHLog "curl exit error code: $errorCode";  exit; }

##----------------------------------------------------------------------------------------
##  Extract claim URL
claim=$(grep -oE "freelearning-claim/[0-9]+/[0-9]+" packt_daily.html)
PHLog "Claim URL: $claim"

##  Extract book title, trim and remove invalid characters
title=$(grep "dotd-title" -A 2 packt_daily.html | tail -1 | sed 's/^[^0-9A-Za-z]*//;s/[\t ]*<\/h2>$//')
title="$(echo -e "${title}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/[^A-Za-z0-9 ._-]//g')"
PHLog "eBook Found: $title"

##  Extract book ID
bookID=$(echo $claim | sed "s/.*\/\([0-9]*\)\/.*/\1/")
echo "";

##  Claim ebook now
curl -s --retry $noOfRetry -m $timeOut -A "$userAgent" -b "$cookie" -c "$cookie" -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.5' -H 'Connection: keep-alive' -H 'Host: www.packtpub.com' -H 'Referer: https://www.packtpub.com/packt/offers/free-learning' "https://www.packtpub.com/$claim"
errorCode=$?;  test "$errorCode" -ne "0" && { PHLog "curl exit error code: $errorCode";  exit; }

##----------------------------------------------------------------------------------------
##  Download PDF version
PHLog "Downloading PDF...$title.pdf"
curl -s -L --retry $noOfRetry -A "$userAgent" -b "$cookie" -c "$cookie" "https://www.packtpub.com/ebook_download/$bookID/pdf" > "$downloadDirectory/$title.pdf"
errorCode=$?;  test "$errorCode" -ne "0" && { PHLog "curl exit error code: $errorCode";  exit; }

##  Download EPUB version
PHLog "Downloading EPUB...$title.epub"
curl -s -L --retry $noOfRetry -A "$userAgent" -b "$cookie" -c "$cookie" "https://www.packtpub.com/ebook_download/$bookID/epub" > "$downloadDirectory/$title.epub"
errorCode=$?;  test "$errorCode" -ne "0" && { PHLog "curl exit error code: $errorCode";  exit; }

##  Download MOBI version
PHLog "Downloading MOBI...$title.mobi"
curl -s -L --retry $noOfRetry -A "$userAgent" -b "$cookie" -c "$cookie" "https://www.packtpub.com/ebook_download/$bookID/mobi" > "$downloadDirectory/$title.mobi"
errorCode=$?;  test "$errorCode" -ne "0" && { PHLog "curl exit error code: $errorCode";  exit; }

##----------------------------------------------------------------------------------------
##  Job done, log out now
PHLog "Logging out..."
curl -s --retry $noOfRetry -m $timeOut -A "$userAgent" -b "$cookie" -c "$cookie" https://www.packtpub.com/logout > packt_logout.html
errorCode=$?;  test "$errorCode" -ne "0" && { PHLog "curl exit error code: $errorCode";  exit; }

##  Remove temporary file
rm -f $cookie packt*.html
echo "" >> "$log"

##----------------------------------------------------------------------------------------
##  Commit to GitLab
PHLog "Commiting to GitLab..."
git config --global user.name "Sita Chan"
git config --global user.email "sita@pacess.com"

git add *
git commit -m "Added '$title'"
git push -u origin master

##----------------------------------------------------------------------------------------
##  Send email alert
echo -e "Dear Pacess,\n\nA Packt ebook '$title' have been downloaded and commit to GitLab.  Thanks!\n\nSita-bot" | mail -r "sita@pacess.com (Sita-bot)" -s "[GitLab] Added Packt ebook '$title'..." pacess@pacess.com

沒有留言: