feat: add new feature

This commit is contained in:
Rogee
2024-09-02 11:41:32 +08:00
parent b00e24efc4
commit 97e0731e2e
2 changed files with 66 additions and 56 deletions

View File

@@ -2,14 +2,12 @@ package internal
import (
"context"
"fmt"
"path/filepath"
"exporter/config"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/auth"
"github.com/gotd/td/telegram/downloader"
"github.com/gotd/td/tg"
"github.com/pkg/errors"
"github.com/samber/lo"
@@ -122,57 +120,3 @@ func (t *TClient) ChannelInfoByID(ctx context.Context, channelID int64) (*tg.Cha
channel := chats[0].(*tg.Channel)
return channel, nil
}
func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, offset int) error {
inputPeer := &tg.InputPeerChannel{ChannelID: channel.ID, AccessHash: channel.AccessHash}
messages, err := t.api.MessagesGetHistory(ctx, &tg.MessagesGetHistoryRequest{
Peer: inputPeer,
Limit: 10,
OffsetID: offset,
})
if err != nil {
return errors.Wrap(err, "messages.getHistory")
}
downloader := downloader.NewDownloader()
lo.ForEach(messages.(*tg.MessagesChannelMessages).GetMessages(), func(item tg.MessageClass, index int) {
msg, ok := item.(*tg.Message)
if !ok {
fmt.Println("ID: get failed")
return
}
if !t.downloadMedia {
return
}
if mediaClass, ok := msg.GetMedia(); ok {
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 := lo.Must(filepath.Abs(fmt.Sprintf("./photos/%d.jpg", photo.GetID())))
storage, err := downloader.Download(t.api, location).ToPath(ctx, saveTo)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Downloaded : ", storage)
}
}
}
})
return nil
}

View File

@@ -0,0 +1,66 @@
package internal
import (
"context"
"fmt"
"path/filepath"
"github.com/gotd/td/telegram/downloader"
"github.com/gotd/td/tg"
"github.com/pkg/errors"
"github.com/samber/lo"
)
func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, offset int) error {
inputPeer := &tg.InputPeerChannel{ChannelID: channel.ID, AccessHash: channel.AccessHash}
messages, err := t.api.MessagesGetHistory(ctx, &tg.MessagesGetHistoryRequest{
Peer: inputPeer,
Limit: 10,
OffsetID: offset,
})
if err != nil {
return errors.Wrap(err, "messages.getHistory")
}
downloader := downloader.NewDownloader()
lo.ForEach(messages.(*tg.MessagesChannelMessages).GetMessages(), func(item tg.MessageClass, index int) {
msg, ok := item.(*tg.Message)
if !ok {
fmt.Println("ID: get failed")
return
}
if !t.downloadMedia {
return
}
if mediaClass, ok := msg.GetMedia(); ok {
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 := lo.Must(filepath.Abs(fmt.Sprintf("./photos/%d.jpg", photo.GetID())))
storage, err := downloader.Download(t.api, location).ToPath(ctx, saveTo)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Downloaded : ", storage)
}
}
}
})
return nil
}