feat: login

This commit is contained in:
Rogee
2024-09-02 14:50:51 +08:00
parent 97e0731e2e
commit d739ca0184
7 changed files with 80 additions and 51 deletions

2
.vscode/launch.json vendored
View File

@@ -11,7 +11,7 @@
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": [
"channels"
"login"
]
}
]

BIN
exporter Executable file

Binary file not shown.

View File

@@ -30,9 +30,10 @@ type TClient struct {
// t.block <- struct{}{}
// }
func NewClient(config *config.Config) *TClient {
func NewClient(logger *zap.Logger, config *config.Config) *TClient {
c := &TClient{
Config: config,
logger: logger,
Client: telegram.NewClient(config.AppID, config.AppHash, telegram.Options{
Logger: logger,
UpdateHandler: nil,
@@ -50,28 +51,8 @@ func (t *TClient) WithMedia() *TClient {
return t
}
// func (t *TClient) Run(ctx context.Context) {
// err := t.Client.Run(ctx, func(ctx context.Context) error {
// flow := auth.NewFlow(Terminal{PhoneNumber: t.Config.Phone}, auth.SendCodeOptions{})
// if err := t.Client.Auth().IfNecessary(ctx, flow); err != nil {
// return errors.Wrap(err, "auth")
// }
// t.api = t.Client.API()
// t.waitLogin <- nil
// <-t.block
// return nil
// })
// if err != nil {
// t.waitLogin <- err
// }
// }
// func (t *TClient) Wait() <-chan error {
// return t.waitLogin
// }
func (t *TClient) Login(ctx context.Context) error {
t.logger.Info("login phone", zap.String("phone", t.Config.Phone))
flow := auth.NewFlow(Terminal{PhoneNumber: client.Config.Phone}, auth.SendCodeOptions{})
if err := t.Client.Auth().IfNecessary(context.Background(), flow); err != nil {
return errors.Wrap(err, "auth")

View File

@@ -2,7 +2,9 @@ package internal
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/gotd/td/telegram/downloader"
@@ -64,3 +66,33 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, offset int)
return nil
}
type ChannelConfig struct {
ID int64 `json:"id"`
Offset int `json:"offset"`
}
func (t *TClient) SaveChannelConfig(ctx context.Context, channelID int64, offset int) (*ChannelConfig, error) {
channelConfigFile := fmt.Sprintf("outputs/%d/config.json", channelID)
// if file not exists then create it
if _, err := os.Stat(channelConfigFile); os.IsNotExist(err) {
// create config file
data, _ := json.Marshal(&ChannelConfig{ID: channelID})
if err := os.WriteFile(channelConfigFile, data, 0o644); err != nil {
return nil, errors.Wrap(err, "write channel config")
}
}
// read config file
data, err := os.ReadFile(channelConfigFile)
if err != nil {
return nil, errors.Wrap(err, "read channel config")
}
var config *ChannelConfig
if err := json.Unmarshal(data, config); err != nil {
return nil, errors.Wrap(err, "unmarshal channel config")
}
return config, nil
}

View File

@@ -19,31 +19,7 @@ func ExportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "export channels",
RunE: wrapE(func(ctx context.Context) error {
if channelID == 0 && channelAlias == "" {
return errors.New("channel id or alias is required")
}
var channel *tg.Channel
var err error
if channelAlias != "" {
channel, err = client.ChannelInfoByAlias(ctx, channelAlias)
if err != nil {
return err
}
} else {
channel, err = client.ChannelInfoByID(ctx, channelID)
if err != nil {
return err
}
}
if downloadMedia {
client.WithMedia()
}
return client.Channel(ctx, channel, offsetID)
}),
RunE: wrapE(exportCmd),
}
cmd.Flags().Int64Var(&channelID, "channel", 0, "channel id")
@@ -53,3 +29,35 @@ func ExportCmd() *cobra.Command {
return cmd
}
func exportCmd(ctx context.Context) error {
if channelID == 0 && channelAlias == "" {
return errors.New("channel id or alias is required")
}
var channel *tg.Channel
var err error
if channelAlias != "" {
channel, err = client.ChannelInfoByAlias(ctx, channelAlias)
if err != nil {
return err
}
} else {
channel, err = client.ChannelInfoByID(ctx, channelID)
if err != nil {
return err
}
}
if downloadMedia {
client.WithMedia()
}
cfg, err := client.SaveChannelConfig(ctx, channel.ID, offsetID)
if err != nil {
return err
}
return nil
return client.Channel(ctx, channel, cfg.Offset)
}

View File

@@ -1,6 +1,8 @@
package internal
import (
"context"
"github.com/spf13/cobra"
)
@@ -8,7 +10,9 @@ func LoginCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Short: "login account",
RunE: wrapE(client.Login),
RunE: wrapE(func(ctx context.Context) error {
return client.Login(ctx)
}),
}
return cmd

View File

@@ -30,7 +30,7 @@ func InitClient(cfg *config.Config) error {
)
logger = zap.New(logCore)
client = NewClient(cfg)
client = NewClient(logger, cfg)
return nil
}
@@ -39,8 +39,12 @@ func Close() error {
return nil
}
func getClient() *TClient {
return client
}
func wrapE(f func(context.Context) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
return client.Client.Run(context.Background(), f)
return getClient().Client.Run(context.Background(), f)
}
}