From aab794fb808d9921192f11c2bea67d0a4183d43a Mon Sep 17 00:00:00 2001 From: iyear Date: Wed, 7 Sep 2022 08:04:02 +0800 Subject: [PATCH] refactor(tg): client init --- app/dl/dlurl/url.go | 18 ++---------------- app/internal/tgc/tgc.go | 25 +++++++++++++++++++++++++ app/login/login.go | 15 ++------------- 3 files changed, 29 insertions(+), 29 deletions(-) create mode 100644 app/internal/tgc/tgc.go diff --git a/app/dl/dlurl/url.go b/app/dl/dlurl/url.go index fc722b0ecd..637c173c72 100644 --- a/app/dl/dlurl/url.go +++ b/app/dl/dlurl/url.go @@ -4,13 +4,11 @@ import ( "context" "fmt" "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/iyear/tdl/app/internal/tgc" "github.com/iyear/tdl/pkg/consts" "github.com/iyear/tdl/pkg/downloader" "github.com/iyear/tdl/pkg/kv" - "github.com/iyear/tdl/pkg/storage" "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 } - c := 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, false), - // RetryInterval: time.Second, - MaxRetries: 10, - Middlewares: []telegram.Middleware{ - floodwait.NewSimpleWaiter(), - // ratelimit.New(rate.Every(300*time.Millisecond), 3), - }, - }) + c := tgc.New(proxy, kvd, floodwait.NewSimpleWaiter()) return c.Run(ctx, func(ctx context.Context) error { status, err := c.Auth().Status(ctx) diff --git a/app/internal/tgc/tgc.go b/app/internal/tgc/tgc.go new file mode 100644 index 0000000000..ec3e73ed03 --- /dev/null +++ b/app/internal/tgc/tgc.go @@ -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, + }) +} diff --git a/app/login/login.go b/app/login/login.go index 1a84f25376..f618c8d6b8 100644 --- a/app/login/login.go +++ b/app/login/login.go @@ -3,15 +3,11 @@ package login import ( "context" "github.com/fatih/color" - "github.com/gotd/td/telegram" "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/kv" - "github.com/iyear/tdl/pkg/storage" - "github.com/iyear/tdl/pkg/utils" "github.com/tcnksm/go-input" - "time" ) func Run(ctx context.Context, ns, proxy string) error { @@ -23,14 +19,7 @@ func Run(ctx context.Context, ns, proxy string) error { return err } - c := 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, - }) + c := tgc.New(proxy, kvd) return c.Run(ctx, func(ctx context.Context) error { if err := c.Ping(ctx); err != nil {