【免費註冊】 【會員登入】 【個人資料】 【會員列表】 【論壇幫助】 【論壇搜尋】 【登出論壇】

∮Ω奧米加空間∮
∮Ω奧米加空間∮»技術文件區»【轉貼】排程領取與下載 packtpub 電子書

訂覽該主題更新消息 | 將該主題推薦給朋友 發表新主題 發起投票  回覆
作者 主題    
dc
管理員



性別:男
來自:瓦肯星
發表總數:6492
註冊時間:2002-05-07 16:32
(第 1 篇) 【轉貼】排程領取與下載 packtpub 電子書

該電子書網站每天上午7點都會更新當日的免費電子書供領取與下載
網址為 https://www.packtpub.com/packt/offers/free-learning
之前都是手動登入領取錯過了不少精彩的書,本來想自己寫程式但是 Google 了一下發現有現成的~就直接拿來用了~

如果只需要領取不用下載的話把下載註解掉就行~
然後加入 Linux crontab 的排程~可以設定每天上午8點跑一次~有 log 可以查詢~


資料來源:利用 Bash 程式下載 Packt 免費電子書

在網上找到用 Bash 編寫的 Packt 免費電子書下載工具,實在利害。一個程式包含領取及下載,簡潔實用。我把它修改一下配合自己的需要:
代碼:

#!/bin/bash

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

##  Variables
userID="you@mail.com"
password="youpassword"

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 "-----------------------------------------------------------"
echo "--  Packt Free eBook Downloader Version 1.00             --"
echo "--  Written by Pacess                                    --"
echo "--  Copyright 2016 Pacess Studio.  All rights reserved.  --"
echo "-----------------------------------------------------------"
echo ""

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

##----------------------------------------------------------------------------------------
##  Website login
PHLog "Logging in..."
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"





本帖由dc最後編輯於2017-02-21 16:45

Your mind to my mind,

your thought to my thought
發表時間:2017-02-21 16:44
dc的個人資料 傳送郵件給dc dc的個人首頁 dc發表的所有文章 送出悄悄話給dc IP:118.*.*.* 編輯  引言回覆 
所有時間均為GMT+8, 現在是2017-08-24 09:04    
訂覽該主題更新消息 | 將該主題推薦給朋友 發表新主題 發起投票  回覆

快速回覆
主題 ( 回覆文章可以不輸入標題 )
URLs自動分析
有回覆時郵件通知
禁用表情符號
使用簽名

<聯絡我們 - OMEGA - 控制面板>

Powered by Centaur & Joksky & DC, ver 2003.08.14
Copyright ©2002-2008 PHPY.COM


頁面生成時間:0.0089521408081055