refactor(tg): client init

This commit is contained in:
iyear 2022-09-07 08:04:02 +08:00
parent a3e64b4eea
commit aab794fb80
3 changed files with 29 additions and 29 deletions

View file

@ -4,13 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/gotd/contrib/middleware/floodwait" "github.com/gotd/contrib/middleware/floodwait"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/telegram/peers" "github.com/gotd/td/telegram/peers"
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts" "github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/downloader" "github.com/iyear/tdl/pkg/downloader"
"github.com/iyear/tdl/pkg/kv" "github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/utils" "github.com/iyear/tdl/pkg/utils"
) )
@ -23,19 +21,7 @@ func Run(ctx context.Context, ns, proxy string, partSize, threads, limit int, ur
return err return err
} }
c := telegram.NewClient(consts.AppID, consts.AppHash, telegram.Options{ c := tgc.New(proxy, kvd, floodwait.NewSimpleWaiter())
Resolver: dcs.Plain(dcs.PlainOptions{
Dial: utils.Proxy.GetDial(proxy).DialContext,
}),
Device: consts.Device,
SessionStorage: storage.NewSession(kvd, false),
// RetryInterval: time.Second,
MaxRetries: 10,
Middlewares: []telegram.Middleware{
floodwait.NewSimpleWaiter(),
// ratelimit.New(rate.Every(300*time.Millisecond), 3),
},
})
return c.Run(ctx, func(ctx context.Context) error { return c.Run(ctx, func(ctx context.Context) error {
status, err := c.Auth().Status(ctx) status, err := c.Auth().Status(ctx)

25
app/internal/tgc/tgc.go Normal file
View file

@ -0,0 +1,25 @@
package tgc
import (
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/utils"
"time"
)
func New(proxy string, kvd *kv.KV, middlewares ...telegram.Middleware) *telegram.Client {
return telegram.NewClient(consts.AppID, consts.AppHash, telegram.Options{
Resolver: dcs.Plain(dcs.PlainOptions{
Dial: utils.Proxy.GetDial(proxy).DialContext,
}),
Device: consts.Device,
SessionStorage: storage.NewSession(kvd, true),
RetryInterval: time.Second,
MaxRetries: 10,
DialTimeout: 10 * time.Second,
Middlewares: middlewares,
})
}

View file

@ -3,15 +3,11 @@ package login
import ( import (
"context" "context"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/auth" "github.com/gotd/td/telegram/auth"
"github.com/gotd/td/telegram/dcs" "github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts" "github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv" "github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/utils"
"github.com/tcnksm/go-input" "github.com/tcnksm/go-input"
"time"
) )
func Run(ctx context.Context, ns, proxy string) error { func Run(ctx context.Context, ns, proxy string) error {
@ -23,14 +19,7 @@ func Run(ctx context.Context, ns, proxy string) error {
return err return err
} }
c := telegram.NewClient(consts.AppID, consts.AppHash, telegram.Options{ c := tgc.New(proxy, kvd)
Resolver: dcs.Plain(dcs.PlainOptions{
Dial: utils.Proxy.GetDial(proxy).DialContext,
}),
Device: consts.Device,
SessionStorage: storage.NewSession(kvd, true),
RetryInterval: time.Second,
})
return c.Run(ctx, func(ctx context.Context) error { return c.Run(ctx, func(ctx context.Context) error {
if err := c.Ping(ctx); err != nil { if err := c.Ping(ctx); err != nil {