Skip to content

Commit

Permalink
refactor(tg): client init
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Sep 7, 2022
1 parent a3e64b4 commit aab794f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
18 changes: 2 additions & 16 deletions app/dl/dlurl/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions app/internal/tgc/tgc.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
15 changes: 2 additions & 13 deletions app/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit aab794f

Please sign in to comment.