diff --git a/.gitignore b/.gitignore index d9c650e..184d224 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dl-talebook.exe -dl-talebook \ No newline at end of file +dl-talebook +*.log \ No newline at end of file diff --git a/main.go b/main.go index affb3c9..3757a60 100644 --- a/main.go +++ b/main.go @@ -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() { @@ -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") @@ -53,6 +55,7 @@ func main() { WithTimeOutOption(timeout), WithStartIndex(startIndex), WithLoginOption(username, password), + WithContinue(continueOnStart), ) if err != nil { diff --git a/talbook_log.go b/talbook_log.go new file mode 100644 index 0000000..c46d960 --- /dev/null +++ b/talbook_log.go @@ -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)) +} diff --git a/talebook.go b/talebook.go index b5ea661..30ad898 100644 --- a/talebook.go +++ b/talebook.go @@ -10,8 +10,10 @@ import ( "net/http/cookiejar" "net/url" "os" + "os/signal" "path/filepath" "strings" + "syscall" "time" ) @@ -59,6 +61,7 @@ type TaleBook struct { serverInfo ServerInfo MaxIndex int Total int + exit func() } type Book struct { @@ -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 } @@ -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 {