@@ -3,12 +3,15 @@ package handlers
33import (
44 "context"
55 "errors"
6+ "fmt"
67 "strconv"
78 "strings"
89
10+ "github.com/duke-git/lancet/v2/slice"
911 "github.com/krau/ManyACG/common"
1012 "github.com/krau/ManyACG/service"
1113 "github.com/krau/ManyACG/sources"
14+ "github.com/krau/ManyACG/storage"
1215 "github.com/krau/ManyACG/telegram/utils"
1316 "github.com/krau/ManyACG/types"
1417
@@ -324,3 +327,119 @@ func ReCaptionArtwork(ctx context.Context, bot *telego.Bot, message telego.Messa
324327 })
325328 utils .ReplyMessage (bot , message , "已重新生成作品描述" )
326329}
330+
331+ func AutoTaggingArtwork (ctx context.Context , bot * telego.Bot , message telego.Message ) {
332+ if ! CheckPermissionInGroup (ctx , message , types .PermissionEditArtwork ) {
333+ utils .ReplyMessage (bot , message , "你没有编辑作品的权限" )
334+ return
335+ }
336+ if common .TaggerClient == nil {
337+ utils .ReplyMessage (bot , message , "Tagger is not available" )
338+ }
339+ var sourceURL string
340+ var findUrlInArgs bool
341+ if message .ReplyToMessage != nil {
342+ sourceURL = utils .FindSourceURLForMessage (message .ReplyToMessage )
343+ } else {
344+ sourceURL = sources .FindSourceURL (message .Text )
345+ findUrlInArgs = true
346+ }
347+ if sourceURL == "" {
348+ utils .ReplyMessage (bot , message , "请回复一条消息, 或者指定作品链接" )
349+ return
350+ }
351+
352+ artwork , err := service .GetArtworkByURL (ctx , sourceURL )
353+ if err != nil {
354+ utils .ReplyMessage (bot , message , "获取作品信息失败: " + err .Error ())
355+ return
356+ }
357+ selectAllPictures := true
358+ pictureIndex := 1
359+ _ , _ , args := telegoutil .ParseCommand (message .Text )
360+ if len (args ) > func () int {
361+ if findUrlInArgs {
362+ return 1
363+ }
364+ return 0
365+ }() {
366+ selectAllPictures = false
367+ pictureIndex , err := strconv .Atoi (args [len (args )- 1 ])
368+ if err != nil {
369+ utils .ReplyMessage (bot , message , "图片序号错误" )
370+ return
371+ }
372+ if pictureIndex < 1 || pictureIndex > len (artwork .Pictures ) {
373+ utils .ReplyMessage (bot , message , "图片序号超出范围" )
374+ return
375+ }
376+ }
377+ pictures := make ([]* types.Picture , 0 )
378+ if selectAllPictures {
379+ pictures = artwork .Pictures
380+ } else {
381+ picture := artwork .Pictures [pictureIndex - 1 ]
382+ pictures [0 ] = picture
383+ }
384+ msg , err := utils .ReplyMessage (bot , message , "正在请求..." )
385+ if err != nil {
386+ common .Logger .Errorf ("Reply message failed: %s" , err )
387+ return
388+ }
389+ for i , picture := range pictures {
390+ var file []byte
391+ file , err = storage .GetFile (ctx , func () * types.StorageDetail {
392+ if picture .StorageInfo .Regular != nil {
393+ return picture .StorageInfo .Regular
394+ } else {
395+ return picture .StorageInfo .Original
396+ }
397+ }())
398+ if err != nil {
399+ file , err = common .DownloadWithCache (ctx , picture .Original , nil )
400+ }
401+ if err != nil {
402+ common .Logger .Errorf ("Download picture %s failed: %s" , picture .Original , err )
403+ continue
404+ }
405+ common .Logger .Debugf ("Predicting tags for %s" , picture .Original )
406+ predict , err := common .TaggerClient .Predict (ctx , file )
407+ if err != nil {
408+ common .Logger .Errorf ("Predict tags failed: %s" , err )
409+ utils .ReplyMessage (bot , message , "Predict tags failed" )
410+ return
411+ }
412+ if len (predict .PredictedTags ) == 0 {
413+ utils .ReplyMessage (bot , message , "No tags predicted" )
414+ return
415+ }
416+ newTags := slice .Union (artwork .Tags , predict .PredictedTags )
417+ if err := service .UpdateArtworkTagsByURL (ctx , artwork .SourceURL , newTags ); err != nil {
418+ utils .ReplyMessage (bot , message , "更新作品标签失败: " + err .Error ())
419+ return
420+ }
421+ artwork , err = service .GetArtworkByURL (ctx , artwork .SourceURL )
422+ if err != nil {
423+ utils .ReplyMessage (bot , message , "获取更新后的作品信息失败: " + err .Error ())
424+ return
425+ }
426+ if artwork .Pictures [0 ].TelegramInfo .MessageID != 0 {
427+ bot .EditMessageCaption (& telego.EditMessageCaptionParams {
428+ ChatID : ChannelChatID ,
429+ MessageID : artwork .Pictures [0 ].TelegramInfo .MessageID ,
430+ Caption : utils .GetArtworkHTMLCaption (artwork ),
431+ ParseMode : telego .ModeHTML ,
432+ })
433+ }
434+ bot .EditMessageText (& telego.EditMessageTextParams {
435+ ChatID : msg .Chat .ChatID (),
436+ MessageID : msg .MessageID ,
437+ Text : fmt .Sprintf ("选择的第 %d 张图片预测标签成功" , i + 1 ),
438+ })
439+ }
440+ bot .EditMessageText (& telego.EditMessageTextParams {
441+ ChatID : msg .Chat .ChatID (),
442+ MessageID : msg .MessageID ,
443+ Text : "更新作品标签成功" ,
444+ })
445+ }
0 commit comments