Skip to content

Commit

Permalink
refactor: extract common fetcher creating logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
syhily committed Nov 15, 2022
1 parent 1c885da commit 6d7677b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 61 deletions.
22 changes: 4 additions & 18 deletions cmd/sanqiu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/spf13/cobra"

"github.com/bookstairs/bookhunter/internal/argument"
"github.com/bookstairs/bookhunter/internal/client"
"github.com/bookstairs/bookhunter/internal/fetcher"
"github.com/bookstairs/bookhunter/internal/log"
)
Expand Down Expand Up @@ -40,25 +39,12 @@ var sanqiuCmd = &cobra.Command{
Row("Aliyun RefreshToken", "******").
Print()

// 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)
// Set the fetcher config.
argument.Website = sanqiuWebsite

// 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,
},
f, err := argument.NewFetcher(fetcher.SanQiu, map[string]string{
"refreshToken": argument.RefreshToken,
})
log.Fatal(err)

Expand Down
22 changes: 3 additions & 19 deletions cmd/talebook/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/spf13/cobra"

"github.com/bookstairs/bookhunter/internal/argument"
"github.com/bookstairs/bookhunter/internal/client"
"github.com/bookstairs/bookhunter/internal/fetcher"
"github.com/bookstairs/bookhunter/internal/log"
)
Expand All @@ -31,25 +30,10 @@ var DownloadCmd = &cobra.Command{
Row("Thread", argument.Thread).
Print()

// 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)

// 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,
},
f, err := argument.NewFetcher(fetcher.Talebook, map[string]string{
"username": argument.Username,
"password": argument.Password,
})
log.Fatal(err)

Expand Down
29 changes: 6 additions & 23 deletions cmd/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"

"github.com/bookstairs/bookhunter/internal/argument"
"github.com/bookstairs/bookhunter/internal/client"
"github.com/bookstairs/bookhunter/internal/fetcher"
"github.com/bookstairs/bookhunter/internal/log"
)
Expand Down Expand Up @@ -55,29 +54,13 @@ var telegramCmd = &cobra.Command{
Row("Thread", argument.Thread).
Print()

// 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)

// Create the fetcher.
f, err := fetcher.New(&fetcher.Config{
Config: cc,
Category: fetcher.Telegram,
Formats: fs,
Extract: argument.Extract,
DownloadPath: argument.DownloadPath,
InitialBookID: argument.InitialBookID,
Rename: argument.Rename,
Thread: argument.Thread,
Properties: map[string]string{
"channelID": argument.ChannelID,
"mobile": argument.Mobile,
"reLogin": strconv.FormatBool(argument.ReLogin),
"appID": strconv.FormatInt(argument.AppID, 10),
"appHash": argument.AppHash,
},
f, err := argument.NewFetcher(fetcher.Telegram, map[string]string{
"channelID": argument.ChannelID,
"mobile": argument.Mobile,
"reLogin": strconv.FormatBool(argument.ReLogin),
"appID": strconv.FormatInt(argument.AppID, 10),
"appHash": argument.AppHash,
})
log.Fatal(err)

Expand Down
30 changes: 29 additions & 1 deletion internal/argument/arguments.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package argument

import "github.com/bookstairs/bookhunter/internal/fetcher"
import (
"github.com/bookstairs/bookhunter/internal/client"
"github.com/bookstairs/bookhunter/internal/fetcher"
)

var (
// Flags for talebook registering.
Expand Down Expand Up @@ -37,3 +40,28 @@ var (
AppID = int64(0)
AppHash = ""
)

// NewFetcher will create the fetcher by the command line arguments.
func NewFetcher(category fetcher.Category, properties map[string]string) (fetcher.Fetcher, error) {
cc, err := client.NewConfig(Website, UserAgent, Proxy, ConfigRoot)
if err != nil {
return nil, err
}

fs, err := fetcher.ParseFormats(Formats)
if err != nil {
return nil, err
}

return fetcher.New(&fetcher.Config{
Config: cc,
Category: category,
Formats: fs,
Extract: Extract,
DownloadPath: DownloadPath,
InitialBookID: InitialBookID,
Rename: Rename,
Thread: Thread,
Properties: properties,
})
}

0 comments on commit 6d7677b

Please sign in to comment.