81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
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 WatchCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "watch",
|
|
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(watchCmd),
|
|
}
|
|
|
|
return cmd
|
|
}
|
|
|
|
func watchCmd(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.Get(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); 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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|