mirror of https://github.com/synctv-org/synctv
Feat: bilibili me api
parent
f78a06befe
commit
d8fcd70857
@ -0,0 +1,40 @@
|
||||
package Vbilibili
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
dbModel "github.com/synctv-org/synctv/internal/model"
|
||||
"github.com/synctv-org/synctv/internal/op"
|
||||
"github.com/synctv-org/synctv/server/model"
|
||||
"github.com/synctv-org/synctv/vendors/bilibili"
|
||||
)
|
||||
|
||||
type MeResp struct {
|
||||
IsLogin bool `json:"isLogin"`
|
||||
Username string `json:"username"`
|
||||
Face string `json:"face"`
|
||||
IsVip bool `json:"isVip"`
|
||||
}
|
||||
|
||||
func Me(ctx *gin.Context) {
|
||||
user := ctx.MustGet("user").(*op.User)
|
||||
vendor, err := db.FirstOrCreateVendorByUserIDAndVendor(user.ID, dbModel.StreamingVendorBilibili)
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
|
||||
return
|
||||
}
|
||||
cli := bilibili.NewClient(vendor.Cookies)
|
||||
nav, err := cli.UserInfo()
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, model.NewApiDataResp(MeResp{
|
||||
IsLogin: nav.Data.IsLogin,
|
||||
Username: nav.Data.Uname,
|
||||
Face: nav.Data.Face,
|
||||
IsVip: nav.Data.VipStatus == 1,
|
||||
}))
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package bilibili
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
json "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func (c *Client) UserInfo() (*Nav, error) {
|
||||
req, err := c.NewRequest(http.MethodGet, "https://api.bilibili.com/x/web-interface/nav", nil, WithoutWbi())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var nav Nav
|
||||
err = json.NewDecoder(resp.Body).Decode(&nav)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &nav, nil
|
||||
}
|
||||
Loading…
Reference in New Issue