Skip to content

Commit 7fc556c

Browse files
committed
feat: Update version to 0.10.0, remove unused imports, and improve artwork posting
1 parent 16d8897 commit 7fc556c

File tree

5 files changed

+57
-18
lines changed

5 files changed

+57
-18
lines changed

bot/handlers_admin.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func postArtworkCb(ctx context.Context, bot *telego.Bot, query telego.CallbackQu
283283
if asR18 {
284284
artwork.R18 = true
285285
}
286-
if err := fetcher.PostAndCreateArtwork(ctx, artwork, bot, storage.GetStorage(), query.Message.GetChat().ID); err != nil {
286+
if err := fetcher.PostAndCreateArtwork(ctx, artwork, bot, query.Message.GetChat().ID, query.Message.GetMessageID()); err != nil {
287287
Logger.Errorf("发布失败: %s", err)
288288
bot.EditMessageCaption(&telego.EditMessageCaptionParams{
289289
ChatID: telegoutil.ID(query.Message.GetChat().ID),
@@ -364,7 +364,7 @@ func postArtworkCmd(ctx context.Context, bot *telego.Bot, message telego.Message
364364
} else {
365365
artwork = cachedArtwork.Artwork
366366
}
367-
if err := fetcher.PostAndCreateArtwork(ctx, artwork, bot, storage.GetStorage(), message.Chat.ID); err != nil {
367+
if err := fetcher.PostAndCreateArtwork(ctx, artwork, bot, message.Chat.ID, message.MessageID); err != nil {
368368
telegram.ReplyMessage(bot, message, "发布失败: "+err.Error())
369369
return
370370
}
@@ -600,7 +600,7 @@ func batchPostArtwork(ctx context.Context, bot *telego.Bot, message telego.Messa
600600
continue
601601
}
602602
}
603-
if err := fetcher.PostAndCreateArtwork(ctx, artwork, bot, storage.GetStorage(), message.Chat.ID); err != nil {
603+
if err := fetcher.PostAndCreateArtwork(ctx, artwork, bot, message.Chat.ID, message.MessageID); err != nil {
604604
Logger.Errorf("发布失败: %s", err)
605605
failed++
606606
telegram.ReplyMessage(bot, message, fmt.Sprintf("发布 %s 失败: %s", sourceURL, err))

cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
const (
10-
Version string = "0.9.3"
10+
Version string = "0.10.0"
1111
)
1212

1313
var VersionCmd = &cobra.Command{

fetcher/fetcher.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"ManyACG/errors"
66
. "ManyACG/logger"
77
"ManyACG/sources"
8-
"ManyACG/storage"
98
"ManyACG/telegram"
109
"ManyACG/types"
1110
"context"
@@ -33,7 +32,7 @@ func StartScheduler(ctx context.Context) {
3332
}(source, config.Cfg.Fetcher.Limit, artworkCh, source.Config().Intervel)
3433
}
3534
for artwork := range artworkCh {
36-
err := PostAndCreateArtwork(ctx, artwork, telegram.Bot, storage.GetStorage(), config.Cfg.Telegram.Admins[0])
35+
err := PostAndCreateArtwork(ctx, artwork, telegram.Bot, config.Cfg.Telegram.Admins[0], 0)
3736
if err != nil {
3837
if es.Is(err, errors.ErrArtworkAlreadyExist) || es.Is(err, errors.ErrArtworkDeleted) {
3938
continue
@@ -68,7 +67,7 @@ func FetchOnce(ctx context.Context, limit int) {
6867
Logger.Infof("Fetched %d artworks", len(artworks))
6968

7069
for _, artwork := range artworks {
71-
err := PostAndCreateArtwork(ctx, artwork, telegram.Bot, storage.GetStorage(), config.Cfg.Telegram.Admins[0])
70+
err := PostAndCreateArtwork(ctx, artwork, telegram.Bot, config.Cfg.Telegram.Admins[0], 0)
7271
if err != nil {
7372
if es.Is(err, errors.ErrArtworkAlreadyExist) || es.Is(err, errors.ErrArtworkDeleted) {
7473
continue

fetcher/service.go

+46-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"go.mongodb.org/mongo-driver/mongo"
2020
)
2121

22-
func PostAndCreateArtwork(ctx context.Context, artwork *types.Artwork, bot *telego.Bot, storage storage.Storage, fromID int64) error {
22+
func PostAndCreateArtwork(ctx context.Context, artwork *types.Artwork, bot *telego.Bot, fromID int64, messageID int) error {
2323
artworkInDB, err := service.GetArtworkByURL(ctx, artwork.SourceURL)
2424
if err == nil && artworkInDB != nil {
2525
Logger.Debugf("Artwork %s already exists", artwork.Title)
@@ -32,20 +32,60 @@ func PostAndCreateArtwork(ctx context.Context, artwork *types.Artwork, bot *tele
3232
Logger.Debugf("Artwork %s is deleted", artwork.Title)
3333
return errors.ErrArtworkDeleted
3434
}
35+
showProgress := fromID != 0 && messageID != 0 && bot != nil
36+
if showProgress {
37+
defer bot.EditMessageReplyMarkup(&telego.EditMessageReplyMarkupParams{
38+
ChatID: telegoutil.ID(fromID),
39+
MessageID: messageID,
40+
ReplyMarkup: nil,
41+
})
42+
}
3543
for i, picture := range artwork.Pictures {
36-
info, err := storage.SavePicture(artwork, picture)
44+
if showProgress {
45+
go bot.EditMessageReplyMarkup(&telego.EditMessageReplyMarkupParams{
46+
ChatID: telegoutil.ID(fromID),
47+
MessageID: messageID,
48+
ReplyMarkup: telegoutil.InlineKeyboard(
49+
[]telego.InlineKeyboardButton{
50+
telegoutil.InlineKeyboardButton(fmt.Sprintf("正在保存图片 %d/%d", i+1, len(artwork.Pictures))).WithCallbackData("noop"),
51+
},
52+
),
53+
})
54+
}
55+
info, err := storage.GetStorage().SavePicture(artwork, picture)
3756
if err != nil {
3857
Logger.Errorf("saving picture %d of artwork %s: %s", i, artwork.Title, err)
3958
return fmt.Errorf("saving picture %d of artwork %s: %w", i, artwork.Title, err)
4059
}
4160
artwork.Pictures[i].StorageInfo = info
4261
}
43-
messages, err := telegram.PostArtwork(telegram.Bot, artwork, storage)
62+
if showProgress {
63+
go bot.EditMessageReplyMarkup(&telego.EditMessageReplyMarkupParams{
64+
ChatID: telegoutil.ID(fromID),
65+
MessageID: messageID,
66+
ReplyMarkup: telegoutil.InlineKeyboard(
67+
[]telego.InlineKeyboardButton{
68+
telegoutil.InlineKeyboardButton("图片保存完成, 正在发布到频道...").WithCallbackData("noop"),
69+
},
70+
),
71+
})
72+
}
73+
messages, err := telegram.PostArtwork(telegram.Bot, artwork)
4474
if err != nil {
4575
return fmt.Errorf("posting artwork [%s](%s): %w", artwork.Title, artwork.SourceURL, err)
4676
}
4777
Logger.Infof("Posted artwork %s", artwork.Title)
48-
78+
if showProgress {
79+
go bot.EditMessageReplyMarkup(&telego.EditMessageReplyMarkupParams{
80+
ChatID: telegoutil.ID(fromID),
81+
MessageID: messageID,
82+
ReplyMarkup: telegoutil.InlineKeyboard(
83+
[]telego.InlineKeyboardButton{
84+
telegoutil.InlineKeyboardButton("发布完成").WithCallbackData("noop"),
85+
},
86+
),
87+
})
88+
}
4989
for i, picture := range artwork.Pictures {
5090
if picture.TelegramInfo == nil {
5191
picture.TelegramInfo = &types.TelegramInfo{}
@@ -72,12 +112,12 @@ func PostAndCreateArtwork(ctx context.Context, artwork *types.Artwork, bot *tele
72112
}()
73113
return fmt.Errorf("error when creating artwork %s: %w", artwork.SourceURL, err)
74114
}
75-
go afterCreate(context.TODO(), artwork, bot, storage, fromID)
115+
go afterCreate(context.TODO(), artwork, bot, fromID, messageID)
76116

77117
return nil
78118
}
79119

80-
func afterCreate(ctx context.Context, artwork *types.Artwork, bot *telego.Bot, _ storage.Storage, fromID int64) {
120+
func afterCreate(ctx context.Context, artwork *types.Artwork, bot *telego.Bot, fromID int64, _ int) {
81121
for _, picture := range artwork.Pictures {
82122
if err := service.ProcessPictureAndUpdate(ctx, picture); err != nil {
83123
Logger.Warnf("error when processing %d of artwork %s: %s", picture.Index, artwork.Title, err)

telegram/artwork.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/mymmrac/telego/telegoutil"
1717
)
1818

19-
func PostArtwork(bot *telego.Bot, artwork *types.Artwork, storage storage.Storage) ([]telego.Message, error) {
19+
func PostArtwork(bot *telego.Bot, artwork *types.Artwork) ([]telego.Message, error) {
2020
if bot == nil {
2121
Logger.Fatal("Bot is nil")
2222
return nil, errors.ErrNilBot
@@ -27,7 +27,7 @@ func PostArtwork(bot *telego.Bot, artwork *types.Artwork, storage storage.Storag
2727
}
2828

2929
if len(artwork.Pictures) <= 10 {
30-
inputMediaPhotos, err := getInputMediaPhotos(artwork, storage, 0, len(artwork.Pictures))
30+
inputMediaPhotos, err := getInputMediaPhotos(artwork, 0, len(artwork.Pictures))
3131
if err != nil {
3232
return nil, err
3333
}
@@ -45,7 +45,7 @@ func PostArtwork(bot *telego.Bot, artwork *types.Artwork, storage storage.Storag
4545
if end > len(artwork.Pictures) {
4646
end = len(artwork.Pictures)
4747
}
48-
inputMediaPhotos, err := getInputMediaPhotos(artwork, storage, i, end)
48+
inputMediaPhotos, err := getInputMediaPhotos(artwork, i, end)
4949
if err != nil {
5050
return nil, err
5151
}
@@ -67,11 +67,11 @@ func PostArtwork(bot *telego.Bot, artwork *types.Artwork, storage storage.Storag
6767
}
6868

6969
// start from 0
70-
func getInputMediaPhotos(artwork *types.Artwork, storage storage.Storage, start, end int) ([]telego.InputMedia, error) {
70+
func getInputMediaPhotos(artwork *types.Artwork, start, end int) ([]telego.InputMedia, error) {
7171
inputMediaPhotos := make([]telego.InputMedia, end-start)
7272
for i := start; i < end; i++ {
7373
picture := artwork.Pictures[i]
74-
fileBytes, err := storage.GetFile(picture.StorageInfo)
74+
fileBytes, err := storage.GetStorage().GetFile(picture.StorageInfo)
7575
if err != nil {
7676
Logger.Errorf("failed to get file: %s", err)
7777
return nil, err

0 commit comments

Comments
 (0)