92 lines
2.4 KiB
Go
92 lines
2.4 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 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("get channel by id failed", zap.Int64("id", ch.UUID), zap.String("title", ch.Title))
|
|
if ch.Username != "" {
|
|
channel, err = client.ChannelInfoByAlias(ctx, ch.Username)
|
|
if err != nil {
|
|
logger.Error("get channel by username failed", zap.String("username", ch.Username), zap.String("title", ch.Title))
|
|
return err
|
|
}
|
|
logger.Info("channel info", zap.Int64("id", channel.ID), zap.String("title", channel.Title))
|
|
} else {
|
|
return err
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|