Skip to content

Commit 53ec906

Browse files
committed
optimisations + added EPUB support
1 parent 95ab4c2 commit 53ec906

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

packt.sh

100644100755
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
source "packt.cfg"
66

77
function log {
8-
echo "$(date '+%d.%m.%Y. %H:%M:%S') $1" >> "$log"
8+
echo "$(date '+%Y-%m-%d %H:%M:%S ') $1" >> "$log"
9+
echo "$1"
10+
}
11+
12+
function getBook {
13+
curl -s -L --retry $rtry -A "$agent" -b "$cookie" -c "$cookie" "https://www.packtpub.com/ebook_download/$book/$1" > "$dldir/$title.$1"
14+
cex=$?; test "$cex" -ne "0" && { log "curl exit error code: $cex"; exit; }
15+
log "$1 downloaded"
916
}
1017

1118
# initial clean up
@@ -24,35 +31,24 @@ curl -s --retry $rtry -m $tout -A "$agent" -b "$cookie" -c "$cookie" https://www
2431
cex=$?; test "$cex" -ne "0" && { log "curl exit error code: $cex"; exit; }
2532

2633
title=$(grep "dotd-title" -A 2 packt_daily.html | tail -1 | sed 's/^[^0-9A-Za-z]*//;s/[\t ]*<\/h2>$//')
27-
echo "Today's free e-book: $title"
2834
log "Today's free e-book: $title"
2935

3036
# claim
3137
claim=$(grep -oE "freelearning-claim/[0-9]+/[0-9]+" packt_daily.html)
3238
curl -s --retry $rtry -m $tout -A "$agent" -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"
3339
cex=$?; test "$cex" -ne "0" && { log "curl exit error code: $cex"; exit; }
34-
echo "e-Book claimed"
3540
log "e-Book claimed"
3641

3742
# download link
3843
book=$(echo $claim | sed 's/.*\/\([0-9]*\)\/.*/\1/')
3944

40-
# PDF download
41-
curl -s -L --retry $rtry -A "$agent" -b "$cookie" -c "$cookie" "https://www.packtpub.com/ebook_download/$book/pdf" > "$dldir/$title.pdf"
42-
cex=$?; test "$cex" -ne "0" && { log "curl exit error code: $cex"; exit; }
43-
echo "PDF downloaded"
44-
log "PDF downloaded"
45-
46-
# Mobi download
47-
curl -s -L --retry $rtry -A "$agent" -b "$cookie" -c "$cookie" "https://www.packtpub.com/ebook_download/$book/mobi" > "$dldir/$title.mobi"
48-
cex=$?; test "$cex" -ne "0" && { log "curl exit error code: $cex"; exit; }
49-
echo "Mobi downloaded"
50-
log "Mobi downloaded"
45+
getBook mobi
46+
getBook epub
47+
getBook pdf
5148

5249
# Packt logout
5350
curl -s --retry $rtry -m $tout -A "$agent" -b "$cookie" -c "$cookie" https://www.packtpub.com/logout > packt_logout.html
5451
cex=$?; test "$cex" -ne "0" && { log "curl exit error code: $cex"; exit; }
55-
echo "Packt logout"
5652
log "Packt logout"
5753

5854
rm -f $cookie packt*.html

0 commit comments

Comments
 (0)