211 lines
4.4 KiB
Go
211 lines
4.4 KiB
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/furryboard/spider-core/biliapi"
|
|
"github.com/furryboard/spider-core/rpc/pb"
|
|
"github.com/mitchellh/mapstructure"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/reflection"
|
|
)
|
|
|
|
type Service struct {
|
|
pb.UnimplementedBiliAPIServer
|
|
}
|
|
|
|
// GetUserStat 用户统计信息
|
|
func (s *Service) GetUserStat(ctx context.Context, req *pb.StatRequest) (*pb.StatReply, error) {
|
|
body, err := biliapi.UserStat(uint(req.Uid))
|
|
if err != nil {
|
|
return &pb.StatReply{
|
|
Code: 400,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
|
|
bodyObj := biliapi.Root{}
|
|
err = json.Unmarshal(body, &bodyObj)
|
|
if err != nil {
|
|
return &pb.StatReply{
|
|
Code: 400,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
|
|
if bodyObj.Code != 0 {
|
|
fmt.Println("bad request: " + bodyObj.Message)
|
|
return &pb.StatReply{
|
|
Code: int32(bodyObj.Code),
|
|
Msg: bodyObj.Message,
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
|
|
data := &biliapi.StatData{}
|
|
err = mapstructure.Decode(bodyObj.Data, data)
|
|
if err != nil {
|
|
return &pb.StatReply{
|
|
Code: 500,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
statData := pb.StatReply_Data{
|
|
Mid: data.Mid,
|
|
Following: data.Following,
|
|
Whisper: data.Whisper,
|
|
Black: data.Black,
|
|
Follower: data.Follower,
|
|
}
|
|
return &pb.StatReply{
|
|
Code: 200,
|
|
Msg: "",
|
|
Data: &statData,
|
|
}, nil
|
|
}
|
|
|
|
// GetUserBasicInfo 用户基础信息
|
|
func (s *Service) GetUserBasicInfo(ctx context.Context, req *pb.InfoRequest) (*pb.InfoReply, error) {
|
|
body, err := biliapi.BasicInfo(uint(req.Uid))
|
|
bodyObj := biliapi.Root{}
|
|
err = json.Unmarshal(body, &bodyObj)
|
|
if err != nil {
|
|
fmt.Println("bad request: " + err.Error())
|
|
return &pb.InfoReply{
|
|
Code: 400,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
if bodyObj.Code != 0 {
|
|
fmt.Println("bad request: " + bodyObj.Message)
|
|
return &pb.InfoReply{
|
|
Code: int32(bodyObj.Code),
|
|
Msg: bodyObj.Message,
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
|
|
// interface映射到结构体
|
|
data := &pb.InfoReply_Data{}
|
|
dataBytes, err := json.Marshal(bodyObj.Data)
|
|
if err != nil {
|
|
return &pb.InfoReply{
|
|
Code: 500,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
err = json.Unmarshal(dataBytes, data)
|
|
if err != nil {
|
|
return &pb.InfoReply{
|
|
Code: 500,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
return &pb.InfoReply{
|
|
Code: 200,
|
|
Msg: "",
|
|
Data: data,
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (s *Service) SearchVideo(ctx context.Context, req *pb.SearchVideoReq) (*pb.SearchVideoReply, error) {
|
|
body, err := biliapi.SearchVideo(&biliapi.SearchParams{
|
|
Keyword: req.Keyword,
|
|
Order: req.Order,
|
|
Page: uint(req.Page),
|
|
Cookie: req.Cookie,
|
|
})
|
|
bodyObj := biliapi.Root{}
|
|
err = json.Unmarshal(body, &bodyObj)
|
|
if err != nil {
|
|
fmt.Println("bad request: " + err.Error())
|
|
return &pb.SearchVideoReply{
|
|
Code: 400,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
if bodyObj.Code != 0 {
|
|
fmt.Println("bad request: " + bodyObj.Message)
|
|
return &pb.SearchVideoReply{
|
|
Code: int32(bodyObj.Code),
|
|
Msg: bodyObj.Message,
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
|
|
// interface映射到结构体
|
|
data := &pb.SearchVideoReply_Data{}
|
|
err = mapstructure.Decode(bodyObj.Data, data)
|
|
if err != nil {
|
|
return &pb.SearchVideoReply{
|
|
Code: 500,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
return &pb.SearchVideoReply{
|
|
Code: 200,
|
|
Msg: "",
|
|
Data: data,
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (s *Service) SearchLiveRoom(ctx context.Context, req *pb.SearchLiveRoomReq) (*pb.SearchLiveRoomReply, error) {
|
|
body, err := biliapi.SearchLiveRoom(&biliapi.SearchParams{
|
|
Keyword: req.Keyword,
|
|
Order: req.Order,
|
|
Page: uint(req.Page),
|
|
Cookie: req.Cookie,
|
|
})
|
|
bodyObj := biliapi.Root{}
|
|
err = json.Unmarshal(body, &bodyObj)
|
|
if err != nil {
|
|
fmt.Println("bad request: " + err.Error())
|
|
return &pb.SearchLiveRoomReply{
|
|
Code: 400,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
if bodyObj.Code != 0 {
|
|
fmt.Println("bad request: " + bodyObj.Message)
|
|
return &pb.SearchLiveRoomReply{
|
|
Code: int32(bodyObj.Code),
|
|
Msg: bodyObj.Message,
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
|
|
// interface映射到结构体
|
|
data := &pb.SearchLiveRoomReply_Data{}
|
|
err = mapstructure.Decode(bodyObj.Data, data)
|
|
if err != nil {
|
|
return &pb.SearchLiveRoomReply{
|
|
Code: 500,
|
|
Msg: err.Error(),
|
|
Data: nil,
|
|
}, err
|
|
}
|
|
return &pb.SearchLiveRoomReply{
|
|
Code: 200,
|
|
Msg: "",
|
|
Data: data,
|
|
}, nil
|
|
|
|
}
|
|
|
|
func RegisterServices(s *grpc.Server) {
|
|
pb.RegisterBiliAPIServer(s, &Service{})
|
|
reflection.Register(s)
|
|
}
|