diff --git a/.vscode/launch.json b/.vscode/launch.json index 4f79b0d..625fb81 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,7 @@ "mode": "auto", "program": "${workspaceFolder}/main.go", "args": [ - "channels" + "login" ] } ] diff --git a/exporter b/exporter new file mode 100755 index 0000000..3fc6230 Binary files /dev/null and b/exporter differ diff --git a/internal/client.go b/internal/client.go index cf3a536..93763bb 100644 --- a/internal/client.go +++ b/internal/client.go @@ -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") diff --git a/internal/client_channel.go b/internal/client_channel.go index 425c465..7ba9db1 100644 --- a/internal/client_channel.go +++ b/internal/client_channel.go @@ -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 +} diff --git a/internal/cmd_export.go b/internal/cmd_export.go index eefec1c..c8479a3 100644 --- a/internal/cmd_export.go +++ b/internal/cmd_export.go @@ -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) +} diff --git a/internal/cmd_login.go b/internal/cmd_login.go index 633b7f2..3366922 100644 --- a/internal/cmd_login.go +++ b/internal/cmd_login.go @@ -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 diff --git a/internal/common.go b/internal/common.go index bc40d9c..995625e 100644 --- a/internal/common.go +++ b/internal/common.go @@ -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) } }