Skip to content

Commit

Permalink
support: Continue an incomplete download:
Browse files Browse the repository at this point in the history
  • Loading branch information
李聪 committed Apr 8, 2022
1 parent 61c7b71 commit e124b58
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dl-talebook.exe
dl-talebook
dl-talebook
*.log
23 changes: 13 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import (
)

var (
userAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36`
site = `https://book.codefine.site:6870/`
cookie = ""
dir = "./"
timeout = time.Duration(10) * time.Second
username = ""
password = ""
verbose = false
startIndex = 0
version = false
userAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36`
site = `https://book.codefine.site:6870/`
cookie = ""
dir = "./"
timeout = time.Duration(10) * time.Second
username = ""
password = ""
verbose = false
startIndex = 0
version = false
continueOnStart = false
)

func init() {
Expand All @@ -35,6 +36,7 @@ func init() {
flag.DurationVar(&timeout, "timeout", timeout, "http timeout")
flag.BoolVar(&verbose, "verbose", false, "show debug log")
flag.BoolVar(&version, "version", false, "show progream version")
flag.BoolVar(&continueOnStart, "continue", true, "continue an incomplete download")

flag.IntVar(&startIndex, "start-index", startIndex, "start book id")

Expand All @@ -53,6 +55,7 @@ func main() {
WithTimeOutOption(timeout),
WithStartIndex(startIndex),
WithLoginOption(username, password),
WithContinue(continueOnStart),
)

if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions talbook_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"log"
"net/url"
"os"
"strconv"
"strings"
)

const (
logfile = `.dl-download.log`
)

func saveDownloadHistory(tb TaleBook) {
u, _ := url.Parse(tb.api)

if err := os.WriteFile(logfile, []byte(fmt.Sprintf("%s %d", u.Host, tb.index)), 0644); err != nil {
log.Printf("warning: can not write dl-download infromat to .dl-download.log %s", err)
}
}

func tryReadHistoryIndex(api string) (int, error) {
content, err := os.ReadFile(logfile)
if err != nil {
return 0, err
}
u, err := url.Parse(api)
if err != nil {
return 0, nil
}
result := strings.Split(string(content), " ")
if len(result) >= 2 && result[0] == u.Host {
return strconv.Atoi(result[1])
}
return 0, fmt.Errorf("prase .dl-download.log failed , content: %s", string(content))
}
36 changes: 36 additions & 0 deletions talebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"net/http/cookiejar"
"net/url"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
)

Expand Down Expand Up @@ -59,6 +61,7 @@ type TaleBook struct {
serverInfo ServerInfo
MaxIndex int
Total int
exit func()
}

type Book struct {
Expand Down Expand Up @@ -217,6 +220,28 @@ func NewTableBook(site string, opstions ...func(*TaleBook)) (*TaleBook, error) {

tb.getInfo()

// try to recovery from last download action
if tb.exit != nil {

index, err := tryReadHistoryIndex(tb.api)
if tb.verbose {
log.Printf(err.Error())
}
if tb.index == 0 && index != 0 {
log.Printf("resume download from last id %d, resuming", index)
tb.index = index
}

// save download index before exit
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
tb.exit()
os.Exit(1)
}()
}

return tb, tb.err
}

Expand Down Expand Up @@ -308,6 +333,17 @@ func WithStartIndex(index int) func(*TaleBook) {
tb.index = index
}
}

func WithContinue(c bool) func(*TaleBook) {
return func(tb *TaleBook) {
if c {
tb.exit = func() {
saveDownloadHistory(*tb)
}
}

}
}
func (tb *TaleBook) LastIndex() int {
var m = tb.Total
for _, book := range tb.serverInfo.Books {
Expand Down

0 comments on commit e124b58

Please sign in to comment.