diff --git a/go.mod b/go.mod index 5371f027..4c41e5a8 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,7 @@ require ( github.com/synctv-org/vendors v0.1.1-0.20231209122754-ebad9251fa7a github.com/ulule/limiter/v3 v3.11.2 github.com/zencoder/go-dash/v3 v3.0.3 - github.com/zijiren233/gencontainer v0.0.0-20231209055719-473cab2b7931 + github.com/zijiren233/gencontainer v0.0.0-20231209155516-a52fcb19fee5 github.com/zijiren233/go-colorable v0.0.0-20230930131441-997304c961cb github.com/zijiren233/ksync v0.2.0 github.com/zijiren233/livelib v0.2.3-0.20231103145812-58de2ae7f423 diff --git a/go.sum b/go.sum index ce08cc02..9f4c563b 100644 --- a/go.sum +++ b/go.sum @@ -368,6 +368,8 @@ github.com/zijiren233/gencontainer v0.0.0-20231108115721-8d4bb333a5bc h1:uKhMt52 github.com/zijiren233/gencontainer v0.0.0-20231108115721-8d4bb333a5bc/go.mod h1:V5oL7PrZxgisuLCblFWd89Jg99O8vM1n58llcxZ2hDY= github.com/zijiren233/gencontainer v0.0.0-20231209055719-473cab2b7931 h1:13Z/zjQzntVOt0DWSzgqznayZRqx4l1SnD+3Bo8DR38= github.com/zijiren233/gencontainer v0.0.0-20231209055719-473cab2b7931/go.mod h1:V5oL7PrZxgisuLCblFWd89Jg99O8vM1n58llcxZ2hDY= +github.com/zijiren233/gencontainer v0.0.0-20231209155516-a52fcb19fee5 h1:OsNDmOre1xXJpRaQUeqet3yYZbkfy8bfEdsXs8PrXSE= +github.com/zijiren233/gencontainer v0.0.0-20231209155516-a52fcb19fee5/go.mod h1:V5oL7PrZxgisuLCblFWd89Jg99O8vM1n58llcxZ2hDY= github.com/zijiren233/go-colorable v0.0.0-20230930131441-997304c961cb h1:0DyOxf/TbbGodHhOVHNoPk+7v/YBJACs22gKpKlatWw= github.com/zijiren233/go-colorable v0.0.0-20230930131441-997304c961cb/go.mod h1:6TCzjDiQ8+5gWZiwsC3pnA5M0vUy2jV2Y7ciHJh729g= github.com/zijiren233/ksync v0.2.0 h1:OyXVXbVQYFEVfWM13NApt4LMHbLQ3HTs4oYcLmqL6NE= diff --git a/server/handlers/movie.go b/server/handlers/movie.go index 4d2edb44..5c31b9c0 100644 --- a/server/handlers/movie.go +++ b/server/handlers/movie.go @@ -33,6 +33,7 @@ import ( "github.com/synctv-org/vendors/api/alist" "github.com/synctv-org/vendors/api/bilibili" "github.com/zencoder/go-dash/v3/mpd" + "github.com/zijiren233/gencontainer/refreshcache" "github.com/zijiren233/livelib/protocol/hls" "github.com/zijiren233/livelib/protocol/httpflv" "golang.org/x/exp/maps" @@ -807,7 +808,10 @@ func initBilibiliCache(ctx context.Context, movie dbModel.Movie, cookieUserID st } } -type bilibiliSubtitleCache map[string]func(context.Context) ([]byte, error) +type bilibiliSubtitleCache map[string]*struct { + url string + srt *refreshcache.RefreshData[[]byte] +} type bilibiliSubtitleResp struct { FontSize float64 `json:"font_size"` @@ -854,9 +858,12 @@ func initBilibiliSubtitleCache(ctx context.Context, movie dbModel.Movie) func() } subtitleCache := make(bilibiliSubtitleCache, len(resp.Subtitles)) for k, v := range resp.Subtitles { - v := v - subtitleCache[k] = func(ctx context.Context) ([]byte, error) { - return translateBilibiliSubtitleToSrt(ctx, v) + subtitleCache[k] = &struct { + url string + srt *refreshcache.RefreshData[[]byte] + }{ + url: v, + srt: refreshcache.NewRefreshData[[]byte](0), } } @@ -1016,7 +1023,9 @@ func proxyVendorMovie(ctx *gin.Context, movie *op.Movie) { return } if s, ok := srtFunc[id]; ok { - srtData, err := s(ctx) + srtData, err := s.srt.Get(func() ([]byte, error) { + return translateBilibiliSubtitleToSrt(ctx, s.url) + }) if err != nil { ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) return