Skip to content

Commit

Permalink
refactor: clean up code and create new command line config.
Browse files Browse the repository at this point in the history
  • Loading branch information
syhily committed Nov 15, 2022
1 parent d73571a commit 1c885da
Show file tree
Hide file tree
Showing 52 changed files with 1,009 additions and 317 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ linters-settings:
goimports:
local-prefixes: github.com/bookstairs/bookhunter
lll:
line-length: 140
line-length: 120
misspell:
locale: US
nolintlint:
Expand Down
13 changes: 12 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"os"

"github.com/spf13/cobra"

"github.com/bookstairs/bookhunter/internal/argument"
"github.com/bookstairs/bookhunter/internal/log"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "bookhunter",
Short: "A command line base downloader for downloading books from internet.",
Short: "A downloader for downloading books from internet.",
Long: `You can use this command to download book from these websites.
1. Self-hosted talebook websites
Expand All @@ -31,4 +34,12 @@ func init() {
rootCmd.AddCommand(sanqiuCmd)
rootCmd.AddCommand(telegramCmd)
rootCmd.AddCommand(versionCmd)

persistentFlags := rootCmd.PersistentFlags()

// Common flags.
persistentFlags.StringVarP(&argument.ConfigRoot, "config", "c", argument.ConfigRoot, "The config path for bookhunter.")
persistentFlags.StringVarP(&argument.Proxy, "proxy", "", argument.Proxy, "The request proxy.")
persistentFlags.StringVarP(&argument.UserAgent, "user-agent", "a", argument.UserAgent, "The request user-agent.")
persistentFlags.BoolVarP(&log.EnableDebug, "verbose", "", false, "Print all the logs for debugging.")
}
89 changes: 60 additions & 29 deletions cmd/sanqiu.go
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.")
}
77 changes: 58 additions & 19 deletions cmd/talebook/download.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,61 @@
package talebook

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/talebook"
"github.com/bookstairs/bookhunter/internal/argument"
"github.com/bookstairs/bookhunter/internal/client"
"github.com/bookstairs/bookhunter/internal/fetcher"
"github.com/bookstairs/bookhunter/internal/log"
)

// Used for downloading books from talebook website.
var downloadConfig = spider.NewConfig()

// DownloadCmd represents the download command
var DownloadCmd = &cobra.Command{
Use: "download",
Short: "Download the book from talebook.",
Run: func(cmd *cobra.Command, args []string) {
// Validate config
spider.ValidateDownloadConfig(downloadConfig)

// Print download configuration.
log.PrintTable("Download Config Info", table.Row{"Config Key", "Config Value"}, downloadConfig, true)
log.NewPrinter().
Title("Talebook Download Information").
Head(log.DefaultHead...).
Row("Website", argument.Website).
Row("Username", argument.Username).
Row("Password", "******").
Row("Config Path", argument.ConfigRoot).
Row("Proxy", argument.Proxy).
Row("UserAgent", argument.UserAgent).
Row("Formats", argument.Formats).
Row("Download Path", argument.DownloadPath).
Row("Initial ID", argument.InitialBookID).
Row("Rename File", argument.Rename).
Row("Thread", argument.Thread).
Print()

// Create the downloader
downloader := talebook.NewDownloader(downloadConfig)
// Create the fetcher config.
cc, err := client.NewConfig(argument.Website, argument.UserAgent, argument.Proxy, argument.ConfigRoot)
log.Fatal(err)
fs, err := fetcher.ParseFormats(argument.Formats)
log.Fatal(err)

// Start download books.
downloader.Download()
// Create the fetcher.
f, err := fetcher.New(&fetcher.Config{
Config: cc,
Category: fetcher.Talebook,
Formats: fs,
DownloadPath: argument.DownloadPath,
InitialBookID: argument.InitialBookID,
Rename: argument.Rename,
Thread: argument.Thread,
Properties: map[string]string{
"username": argument.Username,
"password": argument.Password,
},
})
log.Fatal(err)

// Start download the books.
err = f.Download()
log.Fatal(err)

// Finished all the tasks.
log.Info("Successfully download all the books.")
Expand All @@ -36,11 +64,22 @@ var DownloadCmd = &cobra.Command{

func init() {
// Add flags for use info.
DownloadCmd.Flags().StringVarP(&downloadConfig.Website, "website", "w", "", "The talebook website.")
DownloadCmd.Flags().StringVarP(&downloadConfig.Username, "username", "u", "", "The account login name.")
DownloadCmd.Flags().StringVarP(&downloadConfig.Password, "password", "p", "", "The account password.")
flags := DownloadCmd.Flags()

// Talebook related flags.
flags.StringVarP(&argument.Username, "username", "u", argument.Username, "The account login name.")
flags.StringVarP(&argument.Password, "password", "p", argument.Password, "The account password.")
flags.StringVarP(&argument.Website, "website", "w", argument.Website, "The talebook website.")

spider.BindDownloadArgs(DownloadCmd, downloadConfig)
// Common download flags.
flags.StringSliceVarP(&argument.Formats, "format", "f", argument.Formats, "The file formats you want to download.")
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.")

// Mark some flags as required.
_ = DownloadCmd.MarkFlagRequired("website")
}

0 comments on commit 1c885da

Please sign in to comment.