219 lines
6.1 KiB
Go
219 lines
6.1 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"mime"
|
|
"os"
|
|
|
|
"exporter/pkg/errorx"
|
|
|
|
"github.com/dustin/go-humanize"
|
|
"github.com/gotd/td/telegram/downloader"
|
|
"github.com/gotd/td/tg"
|
|
"github.com/pkg/errors"
|
|
"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.OffsetID = 1339
|
|
// request.Limit = 1
|
|
|
|
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", zap.Int64("channel", channel.ID))
|
|
return errorx.ErrNoNewMessage
|
|
}
|
|
|
|
for _, item := range messages.(*tg.MessagesChannelMessages).GetMessages() {
|
|
switch item.(type) {
|
|
case *tg.MessageEmpty:
|
|
continue
|
|
case *tg.MessageService:
|
|
continue
|
|
}
|
|
|
|
msg, ok := item.(*tg.Message)
|
|
if !ok {
|
|
logger.Error("convert msg to *tg.Message failed", zap.Int64("channel", channel.ID))
|
|
continue
|
|
}
|
|
|
|
channelMessage := NewChannelMessage(msg.ID, msg.GetDate())
|
|
|
|
channelMessage.WithMessage(msg.GetMessage())
|
|
if grpID, ok := msg.GetGroupedID(); ok {
|
|
channelMessage.WithGroupID(grpID)
|
|
}
|
|
|
|
mediaClass, ok := msg.GetMedia()
|
|
if !ok {
|
|
continue
|
|
}
|
|
switch mediaClass.(type) {
|
|
case *tg.MessageMediaDocument:
|
|
if docClass, ok := mediaClass.(*tg.MessageMediaDocument).GetDocument(); ok {
|
|
// logger.Warn("document",
|
|
// zap.Int64("channel", channel.ID),
|
|
// 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("channel", channel.ID),
|
|
zap.Int64("size", doc.GetSize()),
|
|
zap.String("SizeHuman", humanize.Bytes(uint64(doc.GetSize()))),
|
|
)
|
|
continue
|
|
}
|
|
|
|
data, err := t.saveDocument(ctx, cfg, doc)
|
|
if err != nil {
|
|
logger.Error("save document failed", zap.Error(err), zap.Int64("channel", channel.ID))
|
|
return err
|
|
}
|
|
channelMessage.WithDocument(doc.GetID(), data)
|
|
}
|
|
case *tg.MessageMediaWebPage:
|
|
if page, ok := mediaClass.(*tg.MessageMediaWebPage).GetWebpage().(*tg.WebPage); ok {
|
|
channelMessage.WithWebPage(page.GetID(), page.Title, page.URL)
|
|
} else {
|
|
logger.Warn("web_page",
|
|
zap.Int64("channel", channel.ID),
|
|
zap.Int64("msg_id", page.GetID()),
|
|
zap.String("url", mediaClass.(*tg.MessageMediaWebPage).GetWebpage().String()),
|
|
)
|
|
}
|
|
case *tg.MessageMediaPhoto:
|
|
if photoClass, ok := mediaClass.(*tg.MessageMediaPhoto).GetPhoto(); ok {
|
|
photo := photoClass.(*tg.Photo)
|
|
if err := t.savePhoto(ctx, cfg, photo); err != nil {
|
|
logger.Error("save photo failed", zap.Error(err), zap.Int64("channel", channel.ID))
|
|
return err
|
|
}
|
|
channelMessage.WithPhoto(photo.GetID(), "jpg")
|
|
}
|
|
}
|
|
|
|
logger.Info("save message", zap.Int("id", channelMessage.ID))
|
|
if err := cfg.SaveMessage(ctx, channelMessage); err != nil {
|
|
logger.Error("save message failed", zap.Error(err), zap.Int64("channel", channel.ID))
|
|
return err
|
|
}
|
|
|
|
logger.Info("update config", zap.Int("offset", cfg.Offset), zap.Int64("channel", channel.ID))
|
|
if err := cfg.Update(ctx, item.GetID()); err != nil {
|
|
logger.Error("update config failed", zap.Error(err), zap.Int64("channel", channel.ID))
|
|
return err
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// savePhoto
|
|
func (t *TClient) savePhoto(ctx context.Context, cfg *DBChannel, photo *tg.Photo) error {
|
|
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")
|
|
downloader := downloader.NewDownloader()
|
|
_, err := downloader.Download(t.Client.API(), location).ToPath(ctx, saveTo)
|
|
if err != nil {
|
|
os.Remove(saveTo)
|
|
logger.Error("download failed", zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
logger.Info("download photo success", zap.String("location", saveTo))
|
|
|
|
return nil
|
|
}
|
|
|
|
// saveDocument
|
|
func (t *TClient) saveDocument(ctx context.Context, cfg *DBChannel, doc *tg.Document) (ChannelMessageDocument, error) {
|
|
location := &tg.InputDocumentFileLocation{
|
|
ID: doc.GetID(),
|
|
AccessHash: doc.GetAccessHash(),
|
|
FileReference: doc.GetFileReference(),
|
|
}
|
|
exts, err := mime.ExtensionsByType(doc.GetMimeType())
|
|
if err != nil {
|
|
logger.Error("get extension failed", zap.Error(err), zap.String("mime_type", doc.GetMimeType()))
|
|
return ChannelMessageDocument{}, err
|
|
}
|
|
|
|
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)
|
|
downloader := downloader.NewDownloader()
|
|
_, err = downloader.Download(t.Client.API(), location).ToPath(ctx, saveTo)
|
|
if err != nil {
|
|
os.Remove(saveTo)
|
|
logger.Error("download failed", zap.Error(err))
|
|
return ChannelMessageDocument{}, err
|
|
}
|
|
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(),
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
logger.Info("download document success", zap.String("location", saveTo), zap.Any("document", data))
|
|
return data, nil
|
|
}
|