Fix: vendor movie proxy

pull/24/head
zijiren233 2 years ago
parent 18836ff1bf
commit b213ede66d

@ -44,7 +44,7 @@ func newStatus() Status {
func (c *current) Current() Current { func (c *current) Current() Current {
c.lock.RLock() c.lock.RLock()
defer c.lock.RUnlock() defer c.lock.RUnlock()
c.current.updateSeek() c.current.UpdateSeek()
return c.current return c.current
} }
@ -67,7 +67,7 @@ func (c *current) SetMovie(movie model.Movie) {
func (c *current) Status() Status { func (c *current) Status() Status {
c.lock.RLock() c.lock.RLock()
defer c.lock.RUnlock() defer c.lock.RUnlock()
c.current.updateSeek() c.current.UpdateSeek()
return c.current.Status return c.current.Status
} }
@ -109,7 +109,7 @@ func (c *Current) Proto() *pb.Current {
} }
} }
func (c *Current) updateSeek() { func (c *Current) UpdateSeek() {
if c.Movie.Base.Live { if c.Movie.Base.Live {
c.Status.lastUpdate = time.Now() c.Status.lastUpdate = time.Now()
return return

@ -114,7 +114,7 @@ func (m *movie) init() (err error) {
return errors.New("movie proxy is not enabled") return errors.New("movie proxy is not enabled")
} }
if m.Base.VendorInfo.Vendor != "" { if m.Base.VendorInfo.Vendor != "" {
return errors.New("vendor movie info is not supported in movie proxy mode") return nil
} }
u, err := url.Parse(m.Base.Url) u, err := url.Parse(m.Base.Url)
if err != nil { if err != nil {

@ -74,6 +74,8 @@ func MovieList(ctx *gin.Context) {
return return
} }
current.UpdateSeek()
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{ ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
"current": genCurrentResp(current), "current": genCurrentResp(current),
"total": room.GetMoviesCount(), "total": room.GetMoviesCount(),
@ -110,6 +112,8 @@ func CurrentMovie(ctx *gin.Context) {
return return
} }
current.UpdateSeek()
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{ ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
"current": genCurrentResp(current), "current": genCurrentResp(current),
})) }))
@ -339,11 +343,20 @@ func ChangeCurrentMovie(ctx *gin.Context) {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return return
} }
current, err := genCurrent(room)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
return
}
current.UpdateSeek()
if err := room.Broadcast(&op.ElementMessage{ if err := room.Broadcast(&op.ElementMessage{
ElementMessage: &pb.ElementMessage{ ElementMessage: &pb.ElementMessage{
Type: pb.ElementMessageType_CHANGE_CURRENT, Type: pb.ElementMessageType_CHANGE_CURRENT,
Sender: user.Username, Sender: user.Username,
Current: room.Current().Proto(), Current: current.Proto(),
}, },
}); err != nil { }); err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
@ -378,13 +391,13 @@ func ProxyMovie(ctx *gin.Context) {
return return
} }
if m.Base.VendorInfo.Vendor != "" { if !m.Base.Proxy || m.Base.Live || m.Base.RtmpSource {
ProxyVendorMovie(ctx, m.Movie) ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support movie proxy"))
return return
} }
if !m.Base.Proxy || m.Base.Live || m.Base.RtmpSource { if m.Base.VendorInfo.Vendor != "" {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support proxy")) ProxyVendorMovie(ctx, m.Movie)
return return
} }
@ -544,35 +557,24 @@ func ProxyVendorMovie(ctx *gin.Context, m *dbModel.Movie) {
} }
cli := bilibili.NewClient(vendor.Cookies) cli := bilibili.NewClient(vendor.Cookies)
var mu *bilibili.VideoURL
if bvid != "" { if bvid != "" {
mu, err := cli.GetVideoURL(0, bvid, uint(cid)) mu, err = cli.GetVideoURL(0, bvid, uint(cid))
if err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
return
}
// s, err := cli.GetSubtitles(0, bvid, uint(cid))
// if err != nil {
// ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
// return
// }
ctx.Redirect(http.StatusFound, mu.URL)
return
} else { } else {
mu, err := cli.GetPGCURL(uint(epId), uint(cid)) mu, err = cli.GetPGCURL(uint(epId), uint(cid))
if err != nil { }
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) if err != nil {
return ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
}
hrs := proxy.NewBufferedHttpReadSeeker(128*1024, mu.URL,
proxy.WithContext(ctx),
proxy.WithAppendHeaders(map[string]string{
"Referer": "https://www.bilibili.com/",
"User-Agent": utils.UA,
}),
)
http.ServeContent(ctx.Writer, ctx.Request, mu.URL, time.Now(), hrs)
return return
} }
hrs := proxy.NewBufferedHttpReadSeeker(128*1024, mu.URL,
proxy.WithContext(ctx),
proxy.WithAppendHeaders(map[string]string{
"Referer": "https://www.bilibili.com/",
"User-Agent": utils.UA,
}),
)
http.ServeContent(ctx.Writer, ctx.Request, mu.URL, time.Now(), hrs)
default: default:
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("vendor not support")) ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("vendor not support"))
@ -581,6 +583,10 @@ func ProxyVendorMovie(ctx *gin.Context, m *dbModel.Movie) {
} }
func parse2VendorMovie(movie *dbModel.Movie) error { func parse2VendorMovie(movie *dbModel.Movie) error {
if movie.Base.Proxy {
return nil
}
switch movie.Base.VendorInfo.Vendor { switch movie.Base.VendorInfo.Vendor {
case dbModel.StreamingVendorBilibili: case dbModel.StreamingVendorBilibili:
bvidI := movie.Base.VendorInfo.Info["bvid"] bvidI := movie.Base.VendorInfo.Info["bvid"]
@ -618,13 +624,12 @@ func parse2VendorMovie(movie *dbModel.Movie) error {
return fmt.Errorf("cid is not number") return fmt.Errorf("cid is not number")
} }
vendor, err := db.AssignFirstOrCreateVendorByUserIDAndVendor(movie.CreatorID, dbModel.StreamingVendorBilibili)
if err != nil {
return err
}
cli := bilibili.NewClient(vendor.Cookies)
if bvid != "" { if bvid != "" {
vendor, err := db.AssignFirstOrCreateVendorByUserIDAndVendor(movie.CreatorID, dbModel.StreamingVendorBilibili)
if err != nil {
return err
}
cli := bilibili.NewClient(vendor.Cookies)
mu, err := cli.GetVideoURL(0, bvid, uint(cid)) mu, err := cli.GetVideoURL(0, bvid, uint(cid))
if err != nil { if err != nil {
return err return err
@ -632,10 +637,6 @@ func parse2VendorMovie(movie *dbModel.Movie) error {
movie.Base.Url = mu.URL movie.Base.Url = mu.URL
return nil return nil
} else { } else {
// mu, err := cli.GetPGCURL(uint(epId), uint(cid))
// if err != nil {
// return err
// }
return nil return nil
} }

Loading…
Cancel
Save