feat: add cmds
This commit is contained in:
81
internal/cmd_channel_watch.go
Normal file
81
internal/cmd_channel_watch.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"exporter/config"
|
||||
"exporter/database/telegram_resource/public/model"
|
||||
"exporter/database/telegram_resource/public/table"
|
||||
"exporter/pkg/errorx"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func ChannelWatchCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "watch",
|
||||
Aliases: []string{"w"},
|
||||
Short: "watch channels",
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
log.Println("init client")
|
||||
defer log.Println("init client done")
|
||||
return InitClient(config.C)
|
||||
},
|
||||
RunE: wrapE(channelWatchCmd),
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func channelWatchCmd(ctx context.Context) error {
|
||||
tbl := table.Channels
|
||||
|
||||
var lastTravelAt time.Time
|
||||
for {
|
||||
if time.Since(lastTravelAt) < 5*time.Minute {
|
||||
time.Sleep(time.Minute)
|
||||
continue
|
||||
}
|
||||
|
||||
lastTravelAt = time.Now()
|
||||
var channels []model.Channels
|
||||
if err := tbl.SELECT(tbl.AllColumns).QueryContext(ctx, db, &channels); err != nil {
|
||||
logger.Fatal("failed to get channels", zap.Error(err))
|
||||
}
|
||||
|
||||
logger.Info("pending channels", zap.Int("count", len(channels)))
|
||||
for _, ch := range channels {
|
||||
logger.Info("crawl channel", zap.String("channel", ch.Title), zap.Int64("uuid", ch.UUID))
|
||||
|
||||
channel, err := client.ChannelInfoByID(ctx, ch.UUID)
|
||||
if err != nil {
|
||||
logger.Error("failed to get channel info", zap.Error(err), zap.Int64("uuid", ch.UUID))
|
||||
}
|
||||
|
||||
if channel.GetID() == 0 {
|
||||
return errors.New("channel not found")
|
||||
}
|
||||
|
||||
cfg := NewDBChannel(channel.GetID(), channel.Username, channel.Title)
|
||||
if err := cfg.GetOrCreate(ctx); err != nil {
|
||||
logger.Error("failed to get channel config", zap.Error(err), zap.Int64("uuid", ch.UUID))
|
||||
continue
|
||||
}
|
||||
|
||||
for {
|
||||
if err := client.Channel(ctx, channel, cfg, false); err != nil {
|
||||
if errors.Is(err, errorx.ErrNoNewMessage) {
|
||||
logger.Info("no new message", zap.Int64("uuid", ch.UUID))
|
||||
} else {
|
||||
logger.Error("failed to get channel messages", zap.Error(err), zap.Int64("uuid", ch.UUID))
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user