fix: issues
This commit is contained in:
@@ -2,24 +2,29 @@ package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"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, offset int) error {
|
||||
func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *ChannelConfig, modeHistory bool) error {
|
||||
inputPeer := &tg.InputPeerChannel{ChannelID: channel.ID, AccessHash: channel.AccessHash}
|
||||
messages, err := t.api.MessagesGetHistory(ctx, &tg.MessagesGetHistoryRequest{
|
||||
Peer: inputPeer,
|
||||
Limit: 10,
|
||||
OffsetID: offset,
|
||||
})
|
||||
|
||||
request := &tg.MessagesGetHistoryRequest{
|
||||
Peer: inputPeer,
|
||||
Limit: 1,
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
@@ -28,13 +33,19 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, offset int)
|
||||
lo.ForEach(messages.(*tg.MessagesChannelMessages).GetMessages(), func(item tg.MessageClass, index int) {
|
||||
msg, ok := item.(*tg.Message)
|
||||
if !ok {
|
||||
fmt.Println("ID: get failed")
|
||||
t.logger.Error("convert msg to *tg.Message failed")
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err := cfg.Update(ctx, msg.ID); err != nil {
|
||||
t.logger.Error("update config failed", zap.Error(err))
|
||||
}
|
||||
}()
|
||||
|
||||
if !t.downloadMedia {
|
||||
return
|
||||
}
|
||||
channelMessage := NewChannelMessage(msg.ID)
|
||||
defer cfg.SaveMessage(channelMessage)
|
||||
|
||||
channelMessage.WithMessage(msg.GetMessage())
|
||||
|
||||
if mediaClass, ok := msg.GetMedia(); ok {
|
||||
if photoClass, ok := mediaClass.(*tg.MessageMediaPhoto).GetPhoto(); ok {
|
||||
@@ -52,47 +63,17 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, offset int)
|
||||
ThumbSize: thumbSize,
|
||||
}
|
||||
|
||||
saveTo := lo.Must(filepath.Abs(fmt.Sprintf("./photos/%d.jpg", photo.GetID())))
|
||||
storage, err := downloader.Download(t.api, location).ToPath(ctx, saveTo)
|
||||
|
||||
saveTo := cfg.Asset(photo.GetID(), "jpg")
|
||||
_, err := downloader.Download(t.Client.API(), location).ToPath(ctx, saveTo)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println("Downloaded : ", storage)
|
||||
t.logger.Error("download failed", zap.Error(err))
|
||||
return
|
||||
}
|
||||
channelMessage.WithPhoto(photo.GetID(), "jpg")
|
||||
t.logger.Info("download failed", zap.String("asset", saveTo))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type ChannelConfig struct {
|
||||
ID int64 `json:"id"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
|
||||
func (t *TClient) SaveChannelConfig(ctx context.Context, channelID int64, offset int) (*ChannelConfig, error) {
|
||||
channelConfigFile := fmt.Sprintf("outputs/%d/config.json", channelID)
|
||||
// if file not exists then create it
|
||||
if _, err := os.Stat(channelConfigFile); os.IsNotExist(err) {
|
||||
// create config file
|
||||
data, _ := json.Marshal(&ChannelConfig{ID: channelID})
|
||||
if err := os.WriteFile(channelConfigFile, data, 0o644); err != nil {
|
||||
return nil, errors.Wrap(err, "write channel config")
|
||||
}
|
||||
}
|
||||
|
||||
// read config file
|
||||
data, err := os.ReadFile(channelConfigFile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "read channel config")
|
||||
}
|
||||
|
||||
var config *ChannelConfig
|
||||
if err := json.Unmarshal(data, config); err != nil {
|
||||
return nil, errors.Wrap(err, "unmarshal channel config")
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user