fix: offset
This commit is contained in:
@@ -11,36 +11,35 @@ import (
|
||||
"github.com/gotd/td/telegram/downloader"
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChannel, modeHistory bool) error {
|
||||
func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChannel) error {
|
||||
inputPeer := &tg.InputPeerChannel{ChannelID: channel.ID, AccessHash: channel.AccessHash}
|
||||
|
||||
limit := 50
|
||||
request := &tg.MessagesGetHistoryRequest{
|
||||
Peer: inputPeer,
|
||||
Limit: 10,
|
||||
Limit: limit,
|
||||
}
|
||||
|
||||
if modeHistory { // 提供此ID供遍历历史消息
|
||||
request.OffsetID = cfg.Offset
|
||||
} else {
|
||||
request.MinID = cfg.MinID // 提供此ID供新增加的消息
|
||||
}
|
||||
request.OffsetID = cfg.MinID + limit
|
||||
request.MinID = cfg.MinID
|
||||
|
||||
// request.OffsetID = 1339
|
||||
// request.Limit = 1
|
||||
|
||||
messages, err := t.Client.API().MessagesGetHistory(ctx, request)
|
||||
history, err := t.Client.API().MessagesGetHistory(ctx, request)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "messages.getHistory")
|
||||
}
|
||||
if len(messages.(*tg.MessagesChannelMessages).GetMessages()) == 0 {
|
||||
if len(history.(*tg.MessagesChannelMessages).GetMessages()) == 0 {
|
||||
logger.Info("no new message", zap.Int64("channel", channel.ID))
|
||||
return errorx.ErrNoNewMessage
|
||||
}
|
||||
|
||||
for _, item := range messages.(*tg.MessagesChannelMessages).GetMessages() {
|
||||
messages := history.(*tg.MessagesChannelMessages).GetMessages()
|
||||
messages = lo.Reverse(messages)
|
||||
|
||||
for _, item := range messages {
|
||||
switch item.(type) {
|
||||
case *tg.MessageEmpty:
|
||||
continue
|
||||
@@ -82,6 +81,7 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChann
|
||||
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
|
||||
}
|
||||
@@ -120,7 +120,7 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChann
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Info("update config", zap.Int("offset", cfg.Offset), zap.Int64("channel", channel.ID))
|
||||
logger.Info("update config", zap.Int64("channel", channel.ID), zap.Int("msg_id", item.GetID()))
|
||||
if err := cfg.Update(ctx, item.GetID()); err != nil {
|
||||
logger.Error("update config failed", zap.Error(err), zap.Int64("channel", channel.ID))
|
||||
return err
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
|
||||
var (
|
||||
channelID int64
|
||||
modeHistory bool
|
||||
channelAlias string
|
||||
)
|
||||
|
||||
@@ -30,7 +29,6 @@ func ExportCmd() *cobra.Command {
|
||||
}
|
||||
|
||||
cmd.Flags().Int64Var(&channelID, "channel", 0, "channel id")
|
||||
cmd.Flags().BoolVar(&modeHistory, "history", false, "history mode")
|
||||
cmd.Flags().StringVar(&channelAlias, "alias", "", "channel alias")
|
||||
|
||||
return cmd
|
||||
@@ -66,7 +64,7 @@ func exportCmd(ctx context.Context) error {
|
||||
|
||||
// https://t.me/yunpanshare/37426
|
||||
for {
|
||||
if err := client.Channel(ctx, channel, cfg, modeHistory); err != nil {
|
||||
if err := client.Channel(ctx, channel, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ func watchCmd(ctx context.Context) error {
|
||||
}
|
||||
|
||||
for {
|
||||
if err := client.Channel(ctx, channel, cfg, modeHistory); err != nil {
|
||||
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 {
|
||||
|
||||
@@ -23,7 +23,6 @@ type DBChannel struct {
|
||||
UUID int64
|
||||
Username string
|
||||
Title string
|
||||
Offset int
|
||||
MinID int
|
||||
}
|
||||
|
||||
@@ -86,28 +85,18 @@ func (c *DBChannel) Get(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
c.MinID = int(m.MinID)
|
||||
c.Offset = int(m.Offset)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *DBChannel) Update(ctx context.Context, offsetID int) error {
|
||||
if c.Offset == 0 {
|
||||
c.Offset = offsetID
|
||||
}
|
||||
|
||||
if c.Offset > offsetID {
|
||||
c.Offset = offsetID
|
||||
}
|
||||
|
||||
if c.MinID < offsetID {
|
||||
c.MinID = offsetID
|
||||
func (c *DBChannel) Update(ctx context.Context, msgID int) error {
|
||||
if c.MinID < msgID {
|
||||
c.MinID = msgID
|
||||
}
|
||||
|
||||
tbl := table.Channels
|
||||
_, err := tbl.UPDATE().
|
||||
SET(
|
||||
tbl.Offset.SET(Int64(int64(c.Offset))),
|
||||
tbl.MinID.SET(Int64(int64(c.MinID))),
|
||||
).
|
||||
WHERE(tbl.UUID.EQ(Int64(c.UUID))).
|
||||
|
||||
Reference in New Issue
Block a user