2016年12月10日 星期六

利用 Bash 程式下載 Packt 免費電子書及以 LINE 作為通知


八月時寫的「利用 Bash 程式下載 Packt 免費電子書」一直在公司都運作暢順;本來每天在下載後會自動上傳到 GitLab 及發送電郵通知給我;可是早幾天開始沒有收到電郵。除了因為公司的網絡不穩導致下載失敗外,原來 Google 把發送通知的電郵封鎖了。於是我改用 LINE 通知,把程式稍為修改一下:
#!/bin/bash

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

##----------------------------------------------------------------------------------------
##  Update History:
##----------------------------------------------------------------------------------------
##  2016.08.25  Version 1.00
##  - Added auto commit to GitLab
##
##  2016.12.10  Version 1.10
##  - Removed download
##  - Removed auto commit to GitLab
##  - Removed email alert due to blocked by Google
##  - Added LINE alert
##----------------------------------------------------------------------------------------

##  Variables
userID="your@email.com"
password="password"

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="/var/www/"
log="packt_free_ebook.log"
cookie="cookie.txt"

##  LINE-Bot related
lineUser="Uee8ff21b9d850419330cb5446fe66c9a"

##----------------------------------------------------------------------------------------
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 app directory first
cd /var/www/

##  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; }

##----------------------------------------------------------------------------------------
##  Send LINE alert
PHLog "Sending LINE alert..."
curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer s8zJezZGperi5WKRaTaQViB3jXNAgJxkDu+JxlEOfUt2jkRg/EILEZhm2oH1famWe3/kiQu95DlOIQ0zF4sr9vaycPSik63B9UW5gsBCccANZ/eg4DztSr2/7sZVgL/tfskAH2Ni+kwdB04t89O/w15fpcyilFU=' \
-d '{"to":"'"$lineUser"'",
    "messages":[
        {
            "type":"text",
            "text":"A Packt ebook ['"$title"'] have been redeemed to '"$userID"'.  Thanks!"
        }
    ]}' https://api.line.me/v2/bot/message/push

##----------------------------------------------------------------------------------------
PHLog ""

沒有留言: