diff --git a/internal/client_channel.go b/internal/client_channel.go index b355865..8761d45 100644 --- a/internal/client_channel.go +++ b/internal/client_channel.go @@ -71,56 +71,55 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChann } mediaClass, ok := msg.GetMedia() - if !ok { - continue - } - switch mediaClass.(type) { - case *tg.MessageMediaDocument: - if docClass, ok := mediaClass.(*tg.MessageMediaDocument).GetDocument(); ok { - // logger.Warn("document", - // zap.Int64("channel", channel.ID), - // zap.Int("msg_id", msg.ID), - // zap.String("file_name", docClass.String()), - // ) + if ok { + switch mediaClass.(type) { + case *tg.MessageMediaDocument: + if docClass, ok := mediaClass.(*tg.MessageMediaDocument).GetDocument(); ok && cfg.ExportMedia { + // logger.Warn("document", + // zap.Int64("channel", channel.ID), + // zap.Int("msg_id", msg.ID), + // zap.String("file_name", docClass.String()), + // ) - doc := docClass.(*tg.Document) + doc := docClass.(*tg.Document) - if doc.GetSize() > int64(t.Config.GetMaxSize()) { - logger.Warn( - "document size too large", + if doc.GetSize() > int64(t.Config.GetMaxSize()) { + logger.Warn( + "document size too large", + zap.Int64("channel", channel.ID), + zap.Int64("size", doc.GetSize()), + zap.String("SizeHuman", humanize.Bytes(uint64(doc.GetSize()))), + zap.String("Limit", t.Config.MaxSize), + ) + break + } + + data, err := t.saveDocument(ctx, cfg, doc) + if err != nil { + logger.Error("save document failed", zap.Error(err), zap.Int64("channel", channel.ID)) + return err + } + channelMessage.WithDocument(doc.GetID(), data) + } + case *tg.MessageMediaWebPage: + if page, ok := mediaClass.(*tg.MessageMediaWebPage).GetWebpage().(*tg.WebPage); ok { + channelMessage.WithWebPage(page.GetID(), page.Title, page.URL) + } else { + logger.Warn("web_page", zap.Int64("channel", channel.ID), - zap.Int64("size", doc.GetSize()), - zap.String("SizeHuman", humanize.Bytes(uint64(doc.GetSize()))), - zap.String("Limit", t.Config.MaxSize), + zap.Int64("msg_id", page.GetID()), + zap.String("url", mediaClass.(*tg.MessageMediaWebPage).GetWebpage().String()), ) - break } - - data, err := t.saveDocument(ctx, cfg, doc) - if err != nil { - logger.Error("save document failed", zap.Error(err), zap.Int64("channel", channel.ID)) - return err + case *tg.MessageMediaPhoto: + if photoClass, ok := mediaClass.(*tg.MessageMediaPhoto).GetPhoto(); ok && cfg.ExportMedia { + photo := photoClass.(*tg.Photo) + if err := t.savePhoto(ctx, cfg, photo); err != nil { + logger.Error("save photo failed", zap.Error(err), zap.Int64("channel", channel.ID)) + return err + } + channelMessage.WithPhoto(photo.GetID(), "jpg") } - channelMessage.WithDocument(doc.GetID(), data) - } - case *tg.MessageMediaWebPage: - if page, ok := mediaClass.(*tg.MessageMediaWebPage).GetWebpage().(*tg.WebPage); ok { - channelMessage.WithWebPage(page.GetID(), page.Title, page.URL) - } else { - logger.Warn("web_page", - zap.Int64("channel", channel.ID), - zap.Int64("msg_id", page.GetID()), - zap.String("url", mediaClass.(*tg.MessageMediaWebPage).GetWebpage().String()), - ) - } - case *tg.MessageMediaPhoto: - if photoClass, ok := mediaClass.(*tg.MessageMediaPhoto).GetPhoto(); ok { - photo := photoClass.(*tg.Photo) - if err := t.savePhoto(ctx, cfg, photo); err != nil { - logger.Error("save photo failed", zap.Error(err), zap.Int64("channel", channel.ID)) - return err - } - channelMessage.WithPhoto(photo.GetID(), "jpg") } } diff --git a/internal/cmd_channel.go b/internal/cmd_channel.go index 9ac0ab3..621fe42 100644 --- a/internal/cmd_channel.go +++ b/internal/cmd_channel.go @@ -10,8 +10,9 @@ func ChannelCmd() *cobra.Command { } cmd.AddCommand( - ChannelAddCmd(), ChannelListCmd(), + ChannelAddCmd(), + ChannelSetCmd(), ChannelExportCmd(), ChannelWatchCmd(), ) diff --git a/internal/cmd_channel_export.go b/internal/cmd_channel_export.go index 82390c0..ee05a70 100644 --- a/internal/cmd_channel_export.go +++ b/internal/cmd_channel_export.go @@ -13,7 +13,7 @@ import ( "go.uber.org/zap" ) -var dbChannelID int64 +var pkID int64 func ChannelExportCmd() *cobra.Command { cmd := &cobra.Command{ @@ -28,17 +28,17 @@ func ChannelExportCmd() *cobra.Command { RunE: wrapE(channelExportCmd), } - cmd.Flags().Int64Var(&dbChannelID, "channel", 0, "channel id") + cmd.Flags().Int64Var(&pkID, "pk", 0, "channel pk id") return cmd } func channelExportCmd(ctx context.Context) error { - if dbChannelID == 0 { + if pkID == 0 { return errors.New("db channel id required") } - channel, err := client.ChannelInfoByID(ctx, channelID) + channel, err := client.ChannelInfoByID(ctx, pkID) if err != nil { return err } diff --git a/internal/cmd_channel_set.go b/internal/cmd_channel_set.go new file mode 100644 index 0000000..e56f2d3 --- /dev/null +++ b/internal/cmd_channel_set.go @@ -0,0 +1,75 @@ +package internal + +import ( + "context" + "strconv" + "strings" + + "exporter/database/telegram_resource/public/table" + + . "github.com/go-jet/jet/v2/postgres" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +func ChannelSetCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "set", + Short: "set a channel config", + RunE: func(cmd *cobra.Command, args []string) error { + return channelSetCmd(context.Background(), args) + }, + } + cmd.Flags().Int64Var(&pkID, "pk", 0, "channel pk id") + + return cmd +} + +func channelSetCmd(ctx context.Context, args []string) error { + if pkID == 0 { + return errors.New("channel id or alias is required") + } + + tbl := table.Channels + val := []interface{}{} + + for _, arg := range args { + kv := strings.Split(arg, "=") + if len(kv) != 2 { + return errors.New("invalid arg format") + } + + switch kv[0] { + case "min_id": + v, err := strconv.ParseInt(kv[1], 10, 64) + if err != nil { + return errors.Wrap(err, "invalid min_id") + } + val = append(val, tbl.MinID.SET(Int(v))) + case "offset": + v, err := strconv.ParseInt(kv[1], 10, 64) + if err != nil { + return errors.Wrap(err, "invalid offset") + } + val = append(val, tbl.Offset.SET(Int(v))) + case "media", "export_media": + v := kv[1] == "true" + val = append(val, tbl.ExportMedia.SET(Bool(v))) + + default: + return errors.New("invalid arg key") + } + } + + if len(val) == 0 { + return errors.New("no arg provided") + } + + stmt := tbl.UPDATE().SET(val[0]).WHERE(tbl.ID.EQ(Int(pkID))) + if len(val) > 1 { + stmt = tbl.UPDATE().SET(val[0], val[1:]...).WHERE(tbl.ID.EQ(Int(pkID))) + } + + _, err := stmt.ExecContext(ctx, db) + return err +}