feat: add channel cmds
This commit is contained in:
@@ -71,56 +71,55 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChann
|
|||||||
}
|
}
|
||||||
|
|
||||||
mediaClass, ok := msg.GetMedia()
|
mediaClass, ok := msg.GetMedia()
|
||||||
if !ok {
|
if ok {
|
||||||
continue
|
switch mediaClass.(type) {
|
||||||
}
|
case *tg.MessageMediaDocument:
|
||||||
switch mediaClass.(type) {
|
if docClass, ok := mediaClass.(*tg.MessageMediaDocument).GetDocument(); ok && cfg.ExportMedia {
|
||||||
case *tg.MessageMediaDocument:
|
// logger.Warn("document",
|
||||||
if docClass, ok := mediaClass.(*tg.MessageMediaDocument).GetDocument(); ok {
|
// zap.Int64("channel", channel.ID),
|
||||||
// logger.Warn("document",
|
// zap.Int("msg_id", msg.ID),
|
||||||
// zap.Int64("channel", channel.ID),
|
// zap.String("file_name", docClass.String()),
|
||||||
// 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()) {
|
if doc.GetSize() > int64(t.Config.GetMaxSize()) {
|
||||||
logger.Warn(
|
logger.Warn(
|
||||||
"document size too large",
|
"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("channel", channel.ID),
|
||||||
zap.Int64("size", doc.GetSize()),
|
zap.Int64("msg_id", page.GetID()),
|
||||||
zap.String("SizeHuman", humanize.Bytes(uint64(doc.GetSize()))),
|
zap.String("url", mediaClass.(*tg.MessageMediaWebPage).GetWebpage().String()),
|
||||||
zap.String("Limit", t.Config.MaxSize),
|
|
||||||
)
|
)
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
case *tg.MessageMediaPhoto:
|
||||||
data, err := t.saveDocument(ctx, cfg, doc)
|
if photoClass, ok := mediaClass.(*tg.MessageMediaPhoto).GetPhoto(); ok && cfg.ExportMedia {
|
||||||
if err != nil {
|
photo := photoClass.(*tg.Photo)
|
||||||
logger.Error("save document failed", zap.Error(err), zap.Int64("channel", channel.ID))
|
if err := t.savePhoto(ctx, cfg, photo); err != nil {
|
||||||
return err
|
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")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ func ChannelCmd() *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(
|
cmd.AddCommand(
|
||||||
ChannelAddCmd(),
|
|
||||||
ChannelListCmd(),
|
ChannelListCmd(),
|
||||||
|
ChannelAddCmd(),
|
||||||
|
ChannelSetCmd(),
|
||||||
ChannelExportCmd(),
|
ChannelExportCmd(),
|
||||||
ChannelWatchCmd(),
|
ChannelWatchCmd(),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbChannelID int64
|
var pkID int64
|
||||||
|
|
||||||
func ChannelExportCmd() *cobra.Command {
|
func ChannelExportCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@@ -28,17 +28,17 @@ func ChannelExportCmd() *cobra.Command {
|
|||||||
RunE: wrapE(channelExportCmd),
|
RunE: wrapE(channelExportCmd),
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().Int64Var(&dbChannelID, "channel", 0, "channel id")
|
cmd.Flags().Int64Var(&pkID, "pk", 0, "channel pk id")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func channelExportCmd(ctx context.Context) error {
|
func channelExportCmd(ctx context.Context) error {
|
||||||
if dbChannelID == 0 {
|
if pkID == 0 {
|
||||||
return errors.New("db channel id required")
|
return errors.New("db channel id required")
|
||||||
}
|
}
|
||||||
|
|
||||||
channel, err := client.ChannelInfoByID(ctx, channelID)
|
channel, err := client.ChannelInfoByID(ctx, pkID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
75
internal/cmd_channel_set.go
Normal file
75
internal/cmd_channel_set.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user