Files
tg_exporter/internal/client_channel.go
2024-09-02 23:39:22 +08:00

184 lines
5.2 KiB
Go

package internal
import (
"context"
"mime"
"github.com/dustin/go-humanize"
"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供新增加的消息
}
messages, err := t.Client.API().MessagesGetHistory(ctx, request)
if err != nil {
return errors.Wrap(err, "messages.getHistory")
}
if len(messages.(*tg.MessagesChannelMessages).GetMessages()) == 0 {
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() {
logger.Info("update config", zap.Int("offset", cfg.Offset))
if err := cfg.Update(ctx, item.GetID()); err != nil {
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 {
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 {
logger.Warn("document",
zap.Int("msg_id", msg.ID),
zap.String("file_name", docClass.String()),
)
doc := docClass.(*tg.Document)
if doc.GetSize() > int64(t.Config.GetMaxSize()) {
logger.Warn(
"document size too large",
zap.Int64("size", doc.GetSize()),
zap.String("SizeHuman", humanize.Bytes(uint64(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 {
logger.Error("get extension failed", zap.Error(err), zap.String("mime_type", doc.GetMimeType()))
return
}
if len(exts) == 0 {
logger.Warn("no extension found", zap.String("mime_type", doc.GetMimeType()))
switch doc.GetMimeType() {
case "application/rar":
exts = []string{".rar"}
}
}
ext := exts[len(exts)-1]
saveTo := cfg.Asset(doc.GetID(), ext)
_, err = downloader.Download(t.Client.API(), location).ToPath(ctx, saveTo)
if err != nil {
logger.Error("download failed", zap.Error(err))
return
}
docAttr := doc.GetAttributes()
data := ChannelMessageDocument{
Ext: ext,
MimeType: doc.GetMimeType(),
Size: doc.GetSize(),
}
if len(docAttr) > 0 {
for _, attr := range docAttr {
switch attr.(type) {
case *tg.DocumentAttributeFilename:
data.Filename = attr.(*tg.DocumentAttributeFilename).GetFileName()
case *tg.DocumentAttributeVideo:
m := attr.(*tg.DocumentAttributeVideo)
data.Video = &ChannelMessageDocumentVideo{
Duration: m.GetDuration(),
Width: m.GetW(),
Height: m.GetH(),
}
}
}
}
channelMessage.WithDocument(data)
logger.Info("download document success", zap.String("location", saveTo), zap.Any("document", data))
return
}
case *tg.MessageMediaWebPage:
if page, ok := mediaClass.(*tg.MessageMediaWebPage).GetWebpage().(*tg.WebPage); ok {
channelMessage.WithWebPage(page.Title, page.URL)
return
}
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 {
logger.Error("download failed", zap.Error(err))
return
}
channelMessage.WithPhoto(photo.GetID(), "jpg")
logger.Info("download photo success", zap.String("location", saveTo))
}
}
}
})
return nil
}