-
Notifications
You must be signed in to change notification settings - Fork 49
/
sobooks.go
79 lines (66 loc) · 2.62 KB
/
sobooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package cmd
import (
"github.com/spf13/cobra"
"github.com/bookstairs/bookhunter/cmd/flags"
"github.com/bookstairs/bookhunter/internal/driver"
"github.com/bookstairs/bookhunter/internal/fetcher"
"github.com/bookstairs/bookhunter/internal/log"
)
const (
lowestSobooksBookID = 18000
sobooksWebsite = "https://sobooks.net"
)
// sobooksCmd used for download books from sobooks.net
var sobooksCmd = &cobra.Command{
Use: "sobooks",
Short: "A tool for downloading books from sobooks.net",
Run: func(cmd *cobra.Command, args []string) {
// Set the default start index.
if flags.InitialBookID < lowestSobooksBookID {
flags.InitialBookID = lowestSobooksBookID
}
// Print download configuration.
log.NewPrinter().
Title("SoBooks Download Information").
Head(log.DefaultHead...).
Row("SoBooks Code", flags.SoBooksCode).
Row("Config Path", flags.ConfigRoot).
Row("Proxy", flags.Proxy).
Row("Formats", flags.Formats).
Row("Extract Archive", flags.Extract).
Row("Download Path", flags.DownloadPath).
Row("Initial ID", flags.InitialBookID).
Row("Rename File", flags.Rename).
Row("Thread", flags.Thread).
Row("Keywords", flags.Keywords).
Row("Thread Limit (req/min)", flags.RateLimit).
Print()
// Set the domain for using in the client.Client.
flags.Website = sobooksWebsite
flags.Driver = string(driver.LANZOU)
// Create the fetcher.
properties := flags.NewDriverProperties()
properties["code"] = flags.SoBooksCode
f, err := flags.NewFetcher(fetcher.SoBooks, properties)
log.Exit(err)
// Wait all the threads have finished.
err = f.Download()
log.Exit(err)
// Finished all the tasks.
log.Info("Successfully download all the books.")
},
}
func init() {
f := sobooksCmd.Flags()
// Common download flags.
f.StringSliceVarP(&flags.Formats, "format", "f", flags.Formats, "The file formats you want to download")
f.BoolVarP(&flags.Extract, "extract", "e", flags.Extract, "Extract the archive file for filtering")
f.StringVarP(&flags.DownloadPath, "download", "d", flags.DownloadPath, "The book directory you want to use")
f.Int64VarP(&flags.InitialBookID, "initial", "i", flags.InitialBookID, "The book id you want to start download")
f.BoolVarP(&flags.Rename, "rename", "r", flags.Rename, "Rename the book file by book id")
f.IntVarP(&flags.Thread, "thread", "t", flags.Thread, "The number of download thead")
f.IntVar(&flags.RateLimit, "ratelimit", flags.RateLimit, "The allowed requests per minutes for every thread")
// SoBooks books flags.
f.StringVar(&flags.SoBooksCode, "code", flags.SoBooksCode, "The secret code for SoBooks")
_ = sobooksCmd.MarkFlagRequired("code")
}