refactor(tgc): tgc

This commit is contained in:
iyear 2022-09-25 10:00:32 +08:00
parent 8092056c86
commit 67fec5b463
5 changed files with 25 additions and 51 deletions

View File

@ -4,28 +4,16 @@ import (
"context"
"fmt"
"github.com/fatih/color"
"github.com/gotd/contrib/middleware/floodwait"
"github.com/gotd/contrib/middleware/ratelimit"
"github.com/gotd/td/telegram/query"
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/utils"
"github.com/spf13/viper"
"golang.org/x/time/rate"
"time"
)
func List(ctx context.Context) error {
kvd, err := kv.New(kv.Options{
Path: consts.KVPath,
NS: viper.GetString(consts.FlagNamespace),
})
if err != nil {
return err
}
c, err := tgc.New(viper.GetString(consts.FlagProxy), kvd, false, floodwait.NewSimpleWaiter(), ratelimit.New(rate.Every(time.Millisecond*400), 2))
c, _, err := tgc.NoLogin(ratelimit.New(rate.Every(time.Millisecond*400), 2))
if err != nil {
return err
}

View File

@ -3,24 +3,14 @@ package dl
import (
"context"
"fmt"
"github.com/gotd/contrib/middleware/floodwait"
"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/spf13/viper"
)
func Run(ctx context.Context, urls, files []string) error {
kvd, err := kv.New(kv.Options{
Path: consts.KVPath,
NS: viper.GetString(consts.FlagNamespace),
})
if err != nil {
return err
}
c, err := tgc.New(viper.GetString(consts.FlagProxy), kvd, false, floodwait.NewSimpleWaiter())
c, _, err := tgc.NoLogin()
if err != nil {
return err
}

View File

@ -1,6 +1,7 @@
package tgc
import (
"github.com/gotd/contrib/middleware/floodwait"
tdclock "github.com/gotd/td/clock"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
@ -14,23 +15,31 @@ import (
"time"
)
func New(proxy string, kvd *kv.KV, login bool, middlewares ...telegram.Middleware) (*telegram.Client, error) {
func New(login bool, middlewares ...telegram.Middleware) (*telegram.Client, *kv.KV, error) {
var (
_clock tdclock.Clock
err error
)
kvd, err := kv.New(kv.Options{
Path: consts.KVPath,
NS: viper.GetString(consts.FlagNamespace),
})
if err != nil {
return nil, nil, err
}
_clock = tdclock.System
if ntp := viper.GetString(consts.FlagNTP); ntp != "" {
_clock, err = clock.New()
if err != nil {
return nil, err
return nil, nil, err
}
}
return telegram.NewClient(consts.AppID, consts.AppHash, telegram.Options{
Resolver: dcs.Plain(dcs.PlainOptions{
Dial: utils.Proxy.GetDial(proxy).DialContext,
Dial: utils.Proxy.GetDial(viper.GetString(consts.FlagProxy)).DialContext,
}),
Device: consts.Device,
SessionStorage: storage.NewSession(kvd, login),
@ -40,5 +49,13 @@ func New(proxy string, kvd *kv.KV, login bool, middlewares ...telegram.Middlewar
Middlewares: middlewares,
Clock: _clock,
Logger: logger.Logger.Named("client"),
}), nil
}), kvd, nil
}
func NoLogin(middlewares ...telegram.Middleware) (*telegram.Client, *kv.KV, error) {
return New(false, append(middlewares, floodwait.NewSimpleWaiter())...)
}
func Login(middlewares ...telegram.Middleware) (*telegram.Client, *kv.KV, error) {
return New(true, append(middlewares, floodwait.NewSimpleWaiter())...)
}

View File

@ -5,21 +5,10 @@ import (
"github.com/fatih/color"
"github.com/gotd/td/telegram/auth"
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv"
"github.com/spf13/viper"
)
func Code(ctx context.Context) error {
kvd, err := kv.New(kv.Options{
Path: consts.KVPath,
NS: viper.GetString(consts.FlagNamespace),
})
if err != nil {
return err
}
c, err := tgc.New(viper.GetString(consts.FlagProxy), kvd, true)
c, _, err := tgc.Login()
if err != nil {
return err
}

View File

@ -4,23 +4,13 @@ import (
"context"
"fmt"
"github.com/fatih/color"
"github.com/gotd/contrib/middleware/floodwait"
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/uploader"
"github.com/spf13/viper"
)
func Run(ctx context.Context, paths, excludes []string) error {
kvd, err := kv.New(kv.Options{
Path: consts.KVPath,
NS: viper.GetString(consts.FlagNamespace),
})
if err != nil {
return err
}
files, err := walk(paths, excludes)
if err != nil {
return err
@ -28,7 +18,7 @@ func Run(ctx context.Context, paths, excludes []string) error {
color.Blue("Files count: %d", len(files))
c, err := tgc.New(viper.GetString(consts.FlagProxy), kvd, false, floodwait.NewSimpleWaiter())
c, _, err := tgc.NoLogin()
if err != nil {
return err
}