diff --git a/.vscode/launch.json b/.vscode/launch.json index 81338d3..f3182e8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,8 +11,9 @@ "mode": "auto", "program": "${workspaceFolder}/main.go", "args": [ - "publish", - // "export", + "channel", + "export", + "--pk=3" // https://t.me/yunpanshare/26640 // "--alias", "yunpanshare", // https://t.me/Aliyun_4K_Movies/26640 diff --git a/internal/client_channel.go b/internal/client_channel.go index 92e74b4..f5e40b1 100644 --- a/internal/client_channel.go +++ b/internal/client_channel.go @@ -28,6 +28,7 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChann request.OffsetID = cfg.MinID + limit request.MinID = cfg.MinID + logger.Info("get channel message history", zap.Int("min_id", request.MinID), zap.Int("offset", request.OffsetID), zap.Int("limit", request.Limit)) history, err := t.Client.API().MessagesGetHistory(ctx, request) if err != nil { return errors.Wrap(err, "messages.getHistory") @@ -36,7 +37,11 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChann messages := history.(*tg.MessagesChannelMessages).GetMessages() if len(messages) == 0 { if cfg.OffsetID > 0 && request.OffsetID < cfg.OffsetID { - cfg.Update(ctx, request.OffsetID) + logger.Info("no new message, but update config", zap.Int64("channel", channel.ID), zap.Int("offset", request.OffsetID)) + if err := cfg.Update(ctx, request.OffsetID); err != nil { + logger.Error("update config failed", zap.Error(err), zap.Int64("channel", channel.ID)) + return err + } return nil } logger.Info("no new message", zap.Int64("channel", channel.ID)) @@ -52,14 +57,35 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChann for _, item := range messages { switch item.(type) { case *tg.MessageEmpty: + + logger.Info("update config", zap.Int64("channel", channel.ID), zap.Int("msg_id", item.GetID())) + if err := cfg.Update(ctx, item.GetID()); err != nil { + logger.Error("update config failed", zap.Error(err), zap.Int64("channel", channel.ID)) + return err + } + continue case *tg.MessageService: + + logger.Info("update config", zap.Int64("channel", channel.ID), zap.Int("msg_id", item.GetID())) + if err := cfg.Update(ctx, item.GetID()); err != nil { + logger.Error("update config failed", zap.Error(err), zap.Int64("channel", channel.ID)) + return err + } + continue } msg, ok := item.(*tg.Message) if !ok { logger.Error("convert msg to *tg.Message failed", zap.Int64("channel", channel.ID)) + + logger.Info("update config", zap.Int64("channel", channel.ID), zap.Int("msg_id", item.GetID())) + if err := cfg.Update(ctx, item.GetID()); err != nil { + logger.Error("update config failed", zap.Error(err), zap.Int64("channel", channel.ID)) + return err + } + continue } diff --git a/internal/cmd_channel_export.go b/internal/cmd_channel_export.go index 7a9a96c..9757b16 100644 --- a/internal/cmd_channel_export.go +++ b/internal/cmd_channel_export.go @@ -41,11 +41,13 @@ func channelExportCmd(ctx context.Context) error { return err } + logger.Info("get remote channel info", zap.Int64("uuid", cfg.UUID), zap.String("title", cfg.Title)) channel, err := client.ChannelInfoByID(ctx, cfg.UUID) if err != nil { return err } + logger.Info("exporting history msg", zap.Int64("uuid", cfg.UUID)) var continueRetryTimes int for { if err := client.Channel(ctx, channel, cfg, false); err != nil { diff --git a/internal/common.go b/internal/common.go index 679be1d..bb0a10f 100644 --- a/internal/common.go +++ b/internal/common.go @@ -21,8 +21,8 @@ func InitLogger(cfg *config.Config) error { logWriter := zapcore.AddSync(&lj.Logger{ Filename: cfg.LogFile, MaxBackups: 3, - MaxSize: 1, // megabytes - MaxAge: 7, // days + MaxSize: 100, // megabytes + MaxAge: 7, // days }) logCore := zapcore.NewCore( zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),