Skip to content

Commit

Permalink
feat: add hsu.life support.
Browse files Browse the repository at this point in the history
  • Loading branch information
syhily committed Jan 16, 2024
1 parent 8726c9d commit d80e360
Show file tree
Hide file tree
Showing 21 changed files with 426 additions and 520 deletions.
3 changes: 1 addition & 2 deletions cmd/flags/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var (
// Common flags.

Website = ""
UserAgent = client.DefaultUserAgent
Proxy = ""
ConfigRoot = ""
Keywords []string
Expand Down Expand Up @@ -56,7 +55,7 @@ var (
)

func NewClientConfig() (*client.Config, error) {
return client.NewConfig(Website, UserAgent, Proxy, ConfigRoot)
return client.NewConfig(Website, Proxy, ConfigRoot)
}

// NewFetcher will create the fetcher by the command line arguments.
Expand Down
70 changes: 70 additions & 0 deletions cmd/hsu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"github.com/spf13/cobra"

"github.com/bookstairs/bookhunter/cmd/flags"
"github.com/bookstairs/bookhunter/internal/fetcher"
"github.com/bookstairs/bookhunter/internal/log"
)

const hsuWebsite = "https://book.hsu.life"

var hsuCmd = &cobra.Command{
Use: "hsu",
Short: "A tool for downloading book from hsu.life",
Run: func(cmd *cobra.Command, args []string) {
log.NewPrinter().
Title("hsu.life Download Information").
Head(log.DefaultHead).
Row("Username", flags.Username).
Row("Password", flags.HideSensitive(flags.Password)).
Row("Config Path", flags.ConfigRoot).
Row("Proxy", flags.Proxy).
Row("Formats", flags.Formats).
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()

flags.Website = hsuWebsite

// Create the fetcher.
f, err := flags.NewFetcher(fetcher.Hsu, map[string]string{
"username": flags.Username,
"password": flags.Password,
})
log.Exit(err)

// Start downloading the books.
err = f.Download()
log.Exit(err)

// Finished all the tasks.
log.Info("Successfully download all the books.")
},
}

func init() {
// Add flags for use info.
f := hsuCmd.Flags()

// Talebook related flags.
f.StringVarP(&flags.Username, "username", "u", flags.Username, "The hsu.life username")
f.StringVarP(&flags.Password, "password", "p", flags.Password, "The hsu.life password")

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

// Mark some flags as required.
_ = hsuCmd.MarkFlagRequired("username")
_ = hsuCmd.MarkFlagRequired("password")
}
1 change: 0 additions & 1 deletion cmd/k12.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var k12Cmd = &cobra.Command{
Head(log.DefaultHead...).
Row("Config Path", flags.ConfigRoot).
Row("Proxy", flags.Proxy).
Row("UserAgent", flags.UserAgent).
Row("Download Path", flags.DownloadPath).
Row("Thread", flags.Thread).
Row("Thread Limit (req/min)", flags.RateLimit).
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
rootCmd.AddCommand(telegramCmd)
rootCmd.AddCommand(sobooksCmd)
rootCmd.AddCommand(k12Cmd)
rootCmd.AddCommand(hsuCmd)

// Tool commands.
rootCmd.AddCommand(aliyunCmd)
Expand All @@ -45,8 +46,7 @@ func init() {
// Common flags.
persistentFlags.StringVarP(&flags.ConfigRoot, "config", "c", flags.ConfigRoot, "The config path for bookhunter")
persistentFlags.StringVar(&flags.Proxy, "proxy", flags.Proxy, "The request proxy")
persistentFlags.StringVarP(&flags.UserAgent, "user-agent", "a", flags.UserAgent, "The request user-agent")
persistentFlags.IntVarP(&flags.Retry, "retry", "r", flags.Retry, "The retry times for a failed download")
persistentFlags.IntVarP(&flags.Retry, "retry", "", flags.Retry, "The retry times for a failed download")
persistentFlags.BoolVarP(&flags.SkipError, "skip-error", "s", flags.SkipError,
"Continue to download the next book if the current book download failed")
persistentFlags.StringSliceVarP(&flags.Keywords, "keyword", "k", flags.Keywords, "The keywords for books")
Expand Down
1 change: 0 additions & 1 deletion cmd/sobooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var sobooksCmd = &cobra.Command{
Row("SoBooks Code", flags.SoBooksCode).
Row("Config Path", flags.ConfigRoot).
Row("Proxy", flags.Proxy).
Row("UserAgent", flags.UserAgent).
Row("Formats", flags.Formats).
Row("Extract Archive", flags.Extract).
Row("Download Path", flags.DownloadPath).
Expand Down
6 changes: 2 additions & 4 deletions cmd/talebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var talebookDownloadCmd = &cobra.Command{
Row("Password", flags.HideSensitive(flags.Password)).
Row("Config Path", flags.ConfigRoot).
Row("Proxy", flags.Proxy).
Row("UserAgent", flags.UserAgent).
Row("Formats", flags.Formats).
Row("Download Path", flags.DownloadPath).
Row("Initial ID", flags.InitialBookID).
Expand All @@ -49,7 +48,7 @@ var talebookDownloadCmd = &cobra.Command{
})
log.Exit(err)

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

Expand All @@ -74,12 +73,11 @@ You can use this register command for creating account`,
Row("Password", flags.Password).
Row("Email", flags.Email).
Row("Config Path", flags.ConfigRoot).
Row("UserAgent", flags.UserAgent).
Row("Proxy", flags.Proxy).
Print()

// Create client config.
config, err := client.NewConfig(flags.Website, flags.UserAgent, flags.Proxy, flags.ConfigRoot)
config, err := client.NewConfig(flags.Website, flags.Proxy, flags.ConfigRoot)
log.Exit(err)

// Create http client.
Expand Down
1 change: 0 additions & 1 deletion cmd/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var telegramCmd = &cobra.Command{
Head(log.DefaultHead...).
Row("Config Path", flags.ConfigRoot).
Row("Proxy", flags.Proxy).
Row("UserAgent", flags.UserAgent).
Row("Channel ID", flags.ChannelID).
Row("Mobile", flags.HideSensitive(flags.Mobile)).
Row("AppID", flags.HideSensitive(strconv.FormatInt(flags.AppID, 10))).
Expand Down
33 changes: 17 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,51 @@ module github.com/bookstairs/bookhunter
go 1.21

require (
github.com/EDDYCJY/fake-useragent v0.2.0
github.com/PuerkitoBio/goquery v1.8.1
github.com/bits-and-blooms/bitset v1.10.0
github.com/go-resty/resty/v2 v2.10.0
github.com/bits-and-blooms/bitset v1.13.0
github.com/go-resty/resty/v2 v2.11.0
github.com/gotd/contrib v0.19.0
github.com/gotd/td v0.89.0
github.com/jedib0t/go-pretty/v6 v6.4.9
github.com/gotd/td v0.93.0
github.com/jedib0t/go-pretty/v6 v6.5.3
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
github.com/schollz/progressbar/v3 v3.13.1
github.com/schollz/progressbar/v3 v3.14.1
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/spf13/cobra v1.7.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
go.uber.org/ratelimit v0.3.0
golang.org/x/net v0.17.0
golang.org/x/term v0.13.0
golang.org/x/text v0.13.0
golang.org/x/net v0.20.0
golang.org/x/term v0.16.0
golang.org/x/text v0.14.0
)

require (
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-faster/errors v0.6.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/go-faster/jx v1.1.0 // indirect
github.com/go-faster/xor v1.0.0 // indirect
github.com/gotd/ige v0.2.2 // indirect
github.com/gotd/neo v0.1.5 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
nhooyr.io/websocket v1.8.10 // indirect
rsc.io/qr v0.2.0 // indirect
Expand Down
Loading

0 comments on commit d80e360

Please sign in to comment.