Skip to content

Commit

Permalink
feat: use a background telegram instead of custom fetcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
syhily committed Nov 22, 2022
1 parent e935168 commit fe66474
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 59 deletions.
4 changes: 0 additions & 4 deletions internal/fetcher/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ func IsValidFormat(format file.Format) bool {

// New create a fetcher service for downloading books.
func New(c *Config) (Fetcher, error) {
if c.Category == Telegram {
return newTelegramFetcher(c)
}

s, err := newService(c)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions internal/fetcher/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func newService(c *Config) (service, error) {
return newSobooksService(c)
case TianLang:
return newTianlangService(c)
case Telegram:
return newTelegramService(c)
default:
return nil, fmt.Errorf("no such fetcher service [%s] supported", c.Category)
}
Expand Down
45 changes: 4 additions & 41 deletions internal/fetcher/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,14 @@ import (
"strconv"
"strings"

"github.com/gotd/contrib/middleware/floodwait"
"github.com/gotd/td/session"
client "github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/tg"

"github.com/bookstairs/bookhunter/internal/driver"
"github.com/bookstairs/bookhunter/internal/file"
"github.com/bookstairs/bookhunter/internal/telegram"
)

type telegramFetcher struct {
fetcher *commonFetcher
telegram *telegram.Telegram
}

func newTelegramFetcher(config *Config) (Fetcher, error) {
func newTelegramService(config *Config) (service, error) {
// Create the session file.
path, err := config.ConfigPath()
if err != nil {
Expand All @@ -41,45 +32,17 @@ func newTelegramFetcher(config *Config) (Fetcher, error) {
// Change the process file name.
config.precessFile = strings.ReplaceAll(channelID, "/", "_") + ".db"

// Create the http proxy dial.
dialFunc, err := telegram.CreateProxy(config.Proxy)
tel, err := telegram.New(channelID, mobile, appID, appHash, sessionPath, config.Proxy)
if err != nil {
return nil, err
}

// Create the backend telegram client.
cl := client.NewClient(
int(appID),
appHash,
client.Options{
Resolver: dcs.Plain(dcs.PlainOptions{Dial: dialFunc}),
SessionStorage: &session.FileStorage{Path: sessionPath},
Middlewares: []client.Middleware{
floodwait.NewSimpleWaiter().WithMaxRetries(uint(3)),
},
},
)

tel := telegram.New(channelID, mobile, appID, appHash, cl)

return &telegramFetcher{
fetcher: &commonFetcher{
Config: config,
service: &telegramService{
config: config,
telegram: tel,
},
},
return &telegramService{
config: config,
telegram: tel,
}, nil
}

func (t *telegramFetcher) Download() error {
return t.telegram.Execute(func() error {
return t.fetcher.Download()
})
}

type telegramService struct {
config *Config
telegram *telegram.Telegram
Expand Down
48 changes: 36 additions & 12 deletions internal/telegram/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package telegram
import (
"context"

"github.com/gotd/contrib/bg"
"github.com/gotd/contrib/middleware/floodwait"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/tg"

"github.com/bookstairs/bookhunter/internal/file"
Expand Down Expand Up @@ -36,24 +40,44 @@ type (
)

// New will create a telegram client.
func New(channelID string, mobile string, appID int64, appHash string, client *telegram.Client) *Telegram {
return &Telegram{
func New(channelID, mobile string, appID int64, appHash string, sessionPath, proxy string) (*Telegram, error) {
// Create the http proxy dial.
dialFunc, err := createProxy(proxy)
if err != nil {
return nil, err
}

// Create the backend telegram client.
client := telegram.NewClient(
int(appID),
appHash,
telegram.Options{
Resolver: dcs.Plain(dcs.PlainOptions{Dial: dialFunc}),
SessionStorage: &session.FileStorage{Path: sessionPath},
Middlewares: []telegram.Middleware{
floodwait.NewSimpleWaiter().WithMaxRetries(uint(3)),
},
},
)

ctx := context.Background()
_, err = bg.Connect(client, bg.WithContext(ctx)) // No need to close this client.
if err != nil {
return nil, err
}

t := &Telegram{
ctx: ctx,
channelID: channelID,
mobile: mobile,
appID: appID,
appHash: appHash,
client: client,
}
}

func (t *Telegram) Execute(f func() error) error {
return t.client.Run(context.Background(), func(ctx context.Context) error {
if err := t.Authentication(ctx); err != nil {
return err
}

t.ctx = ctx
if err := t.Authentication(t.ctx); err != nil {
return nil, err
}

return f()
})
return t, nil
}
4 changes: 2 additions & 2 deletions internal/telegram/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func init() {

// This file is used to manually create a proxy with the arguments and system environment.

// CreateProxy is used to create a dcs.DialFunc for the telegram to send request.
// createProxy is used to create a dcs.DialFunc for the telegram to send request.
// We don't support MTProxy now.
func CreateProxy(proxyURL string) (dcs.DialFunc, error) {
func createProxy(proxyURL string) (dcs.DialFunc, error) {
if proxyURL != "" {
log.Debugf("Try to manually create the proxy through %s", proxyURL)

Expand Down

0 comments on commit fe66474

Please sign in to comment.