-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: clean up code and create new command line config.
- Loading branch information
Showing
52 changed files
with
1,009 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,90 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/bookstairs/bookhunter/pkg/log" | ||
"github.com/bookstairs/bookhunter/pkg/spider" | ||
"github.com/bookstairs/bookhunter/sanqiu" | ||
"github.com/bookstairs/bookhunter/internal/argument" | ||
"github.com/bookstairs/bookhunter/internal/client" | ||
"github.com/bookstairs/bookhunter/internal/fetcher" | ||
"github.com/bookstairs/bookhunter/internal/log" | ||
) | ||
|
||
const lowestBookID = 163 | ||
|
||
// Used for downloading books from sanqiu website. | ||
var c = spider.NewConfig() | ||
const ( | ||
lowestBookID = 163 | ||
sanqiuWebsite = "https://www.sanqiu.mobi" | ||
) | ||
|
||
// sanqiuCmd used for download books from sanqiu.mobi | ||
var sanqiuCmd = &cobra.Command{ | ||
Use: "sanqiu", | ||
Short: "A tool for downloading books from sanqiu.mobi", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// Validate config. | ||
spider.ValidateDownloadConfig(c) | ||
|
||
// Set the default start index. | ||
if c.InitialBookID < lowestBookID { | ||
c.InitialBookID = lowestBookID | ||
if argument.InitialBookID < lowestBookID { | ||
argument.InitialBookID = lowestBookID | ||
} | ||
|
||
// Print download configuration. | ||
log.PrintTable("Download Config Info", table.Row{"Config Key", "Config Value"}, c, false) | ||
log.NewPrinter(). | ||
Title("Sanqiu Download Information"). | ||
Head(log.DefaultHead...). | ||
Row("Config Path", argument.ConfigRoot). | ||
Row("Proxy", argument.Proxy). | ||
Row("UserAgent", argument.UserAgent). | ||
Row("Formats", argument.Formats). | ||
Row("Extract Archive", argument.Extract). | ||
Row("Download Path", argument.DownloadPath). | ||
Row("Initial ID", argument.InitialBookID). | ||
Row("Rename File", argument.Rename). | ||
Row("Thread", argument.Thread). | ||
Row("Aliyun RefreshToken", "******"). | ||
Print() | ||
|
||
// Create the downloader. | ||
downloader := sanqiu.NewDownloader(c) | ||
// Create the fetcher config. | ||
cc, err := client.NewConfig(sanqiuWebsite, argument.UserAgent, argument.Proxy, argument.ConfigRoot) | ||
log.Fatal(err) | ||
fs, err := fetcher.ParseFormats(argument.Formats) | ||
log.Fatal(err) | ||
|
||
for i := 0; i < c.Thread; i++ { | ||
// Create a thread and download books in this thread. | ||
downloader.Fork() | ||
} | ||
// Create the fetcher. | ||
f, err := fetcher.New(&fetcher.Config{ | ||
Config: cc, | ||
Category: fetcher.SanQiu, | ||
Formats: fs, | ||
Extract: argument.Extract, | ||
DownloadPath: argument.DownloadPath, | ||
InitialBookID: argument.InitialBookID, | ||
Rename: argument.Rename, | ||
Thread: argument.Thread, | ||
Properties: map[string]string{ | ||
"refreshToken": argument.RefreshToken, | ||
}, | ||
}) | ||
log.Fatal(err) | ||
|
||
// Wait all the thread have finished. | ||
downloader.Join() | ||
// Wait all the threads have finished. | ||
err = f.Download() | ||
log.Fatal(err) | ||
|
||
// Finished all the tasks. | ||
log.Info("Successfully download all the books.") | ||
}, | ||
} | ||
|
||
func init() { | ||
sanqiuCmd.Flags().StringVarP(&c.Website, "website", "w", sanqiu.DefaultWebsite, | ||
"The website for sanqiu. You don't need to override the default url.") | ||
sanqiuCmd.Flags().StringVar(&spider.AliyunConfig.RefreshToken, "refreshToken", "", | ||
"The refreshToken for AliYun Drive.") | ||
flags := sanqiuCmd.Flags() | ||
|
||
// Set common download config arguments. | ||
spider.BindDownloadArgs(sanqiuCmd, c) | ||
// Common download flags. | ||
flags.StringSliceVarP(&argument.Formats, "format", "f", argument.Formats, "The file formats you want to download.") | ||
flags.BoolVarP(&argument.Extract, "extract", "e", argument.Extract, "Extract the archive file for filtering.") | ||
flags.StringVarP(&argument.DownloadPath, "download", "d", argument.DownloadPath, | ||
"The book directory you want to use, default would be current working directory.") | ||
flags.IntVarP(&argument.InitialBookID, "initial", "i", argument.InitialBookID, | ||
"The book id you want to start download. It should exceed 0.") | ||
flags.BoolVarP(&argument.Rename, "rename", "r", argument.Rename, "Rename the book file by book ID.") | ||
flags.IntVarP(&argument.Thread, "thread", "t", argument.Thread, "The number of concurrent download thead.") | ||
|
||
sanqiuCmd.Flags().IntVarP(&c.Thread, "thread", "t", c.Thread, "The number of download threads.") | ||
// Drive ISP flags. | ||
flags.StringVarP(&argument.RefreshToken, "refreshToken", "", argument.RefreshToken, | ||
"We would try to download from the aliyun drive if you provide this token.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.