149 lines
4.2 KiB
Go
149 lines
4.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"mime"
|
|
|
|
"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 {
|
|
inputPeer := &tg.InputPeerChannel{ChannelID: channel.ID, AccessHash: channel.AccessHash}
|
|
|
|
request := &tg.MessagesGetHistoryRequest{
|
|
Peer: inputPeer,
|
|
Limit: 10,
|
|
}
|
|
|
|
if modeHistory { // 提供此ID供遍历历史消息
|
|
request.OffsetID = cfg.Offset
|
|
} else {
|
|
request.MinID = cfg.MinID // 提供此ID供新增加的消息
|
|
}
|
|
|
|
request.Limit = 1
|
|
request.OffsetID = 3753
|
|
|
|
messages, err := t.Client.API().MessagesGetHistory(ctx, request)
|
|
if err != nil {
|
|
return errors.Wrap(err, "messages.getHistory")
|
|
}
|
|
if len(messages.(*tg.MessagesChannelMessages).GetMessages()) == 0 {
|
|
t.logger.Info("no new message")
|
|
return errors.New("no new message")
|
|
}
|
|
|
|
downloader := downloader.NewDownloader()
|
|
lo.ForEach(messages.(*tg.MessagesChannelMessages).GetMessages(), func(item tg.MessageClass, index int) {
|
|
defer func() {
|
|
t.logger.Info("update config", zap.Int("offset", cfg.Offset))
|
|
if err := cfg.Update(ctx, item.GetID()); err != nil {
|
|
t.logger.Error("update config failed", zap.Error(err))
|
|
}
|
|
}()
|
|
|
|
switch item.(type) {
|
|
case *tg.MessageEmpty:
|
|
return
|
|
case *tg.MessageService:
|
|
return
|
|
}
|
|
|
|
msg, ok := item.(*tg.Message)
|
|
if !ok {
|
|
t.logger.Error("convert msg to *tg.Message failed")
|
|
return
|
|
}
|
|
|
|
channelMessage := NewChannelMessage(msg.ID, msg.GetDate())
|
|
defer cfg.SaveMessage(ctx, channelMessage)
|
|
|
|
channelMessage.WithMessage(msg.GetMessage())
|
|
if grpID, ok := msg.GetGroupedID(); ok {
|
|
channelMessage.WithGroupID(grpID)
|
|
}
|
|
|
|
if mediaClass, ok := msg.GetMedia(); ok {
|
|
switch mediaClass.(type) {
|
|
case *tg.MessageMediaDocument:
|
|
if docClass, ok := mediaClass.(*tg.MessageMediaDocument).GetDocument(); ok {
|
|
t.logger.Warn("document",
|
|
zap.Int("msg_id", msg.ID),
|
|
zap.String("file_name", docClass.String()),
|
|
)
|
|
|
|
doc := docClass.(*tg.Document)
|
|
if doc.GetSize() > 1024*1024*10 {
|
|
t.logger.Warn("document size too large", zap.Int64("size", doc.GetSize()))
|
|
return
|
|
}
|
|
|
|
thumbSize := ""
|
|
if len(doc.Thumbs) > 1 {
|
|
thumbSize = doc.Thumbs[len(doc.Thumbs)-1].GetType()
|
|
}
|
|
|
|
location := &tg.InputDocumentFileLocation{
|
|
ID: doc.GetID(),
|
|
AccessHash: doc.GetAccessHash(),
|
|
FileReference: doc.GetFileReference(),
|
|
ThumbSize: thumbSize,
|
|
}
|
|
exts, err := mime.ExtensionsByType(doc.GetMimeType())
|
|
if err != nil {
|
|
t.logger.Error("get extension failed", zap.Error(err), zap.String("mime_type", doc.GetMimeType()))
|
|
return
|
|
}
|
|
|
|
saveTo := cfg.Asset(doc.GetID(), exts[0])
|
|
_, err = downloader.Download(t.Client.API(), location).ToPath(ctx, saveTo)
|
|
if err != nil {
|
|
t.logger.Error("download failed", zap.Error(err))
|
|
return
|
|
}
|
|
channelMessage.WithDoc(doc.GetID(), exts[0])
|
|
t.logger.Info("download photo success", zap.String("location", saveTo))
|
|
return
|
|
}
|
|
case *tg.MessageMediaWebPage:
|
|
if page, ok := mediaClass.(*tg.MessageMediaWebPage).GetWebpage().(*tg.WebPage); ok {
|
|
channelMessage.WithWebPage(page.Title, page.URL)
|
|
return
|
|
}
|
|
t.logger.Warn("web_page", zap.String("url", mediaClass.(*tg.MessageMediaWebPage).GetWebpage().String()))
|
|
case *tg.MessageMediaPhoto:
|
|
if photoClass, ok := mediaClass.(*tg.MessageMediaPhoto).GetPhoto(); ok {
|
|
photo := photoClass.(*tg.Photo)
|
|
|
|
thumbSize := ""
|
|
if len(photo.Sizes) > 1 {
|
|
thumbSize = photo.Sizes[len(photo.Sizes)-1].GetType()
|
|
}
|
|
|
|
location := &tg.InputPhotoFileLocation{
|
|
ID: photo.GetID(),
|
|
AccessHash: photo.GetAccessHash(),
|
|
FileReference: photo.GetFileReference(),
|
|
ThumbSize: thumbSize,
|
|
}
|
|
|
|
saveTo := cfg.Asset(photo.GetID(), "jpg")
|
|
_, err := downloader.Download(t.Client.API(), location).ToPath(ctx, saveTo)
|
|
if err != nil {
|
|
t.logger.Error("download failed", zap.Error(err))
|
|
return
|
|
}
|
|
channelMessage.WithPhoto(photo.GetID(), "jpg")
|
|
t.logger.Info("download photo success", zap.String("location", saveTo))
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
return nil
|
|
}
|