149 lines
4.1 KiB
Go
149 lines
4.1 KiB
Go
|
package logic
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"github.com/eigeen/furryboard/spider-scheduler/pkg/dao"
|
||
|
"github.com/eigeen/furryboard/spider-scheduler/pkg/dao/model"
|
||
|
"github.com/eigeen/furryboard/spider-scheduler/pkg/exception"
|
||
|
"github.com/eigeen/furryboard/spider-scheduler/rpc"
|
||
|
"github.com/eigeen/furryboard/spider-scheduler/rpc/pb"
|
||
|
"gorm.io/gorm"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// SearchNewestVideos b站搜索 最新发布排序 仅搜索视频
|
||
|
func SearchNewestVideos(keyword, cookie string, page uint) ([]*pb.SearchVideoResult, error) {
|
||
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||
|
defer cancel()
|
||
|
info, err := rpc.SpiderCore().SearchVideo(ctx, &pb.SearchVideoReq{
|
||
|
Keyword: keyword,
|
||
|
Order: "pubdate",
|
||
|
Page: uint32(page),
|
||
|
Cookie: cookie,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return nil, exception.InternalError("获取最新视频搜索信息失败:" + err.Error())
|
||
|
}
|
||
|
|
||
|
if info.Code != 200 { // -400 -411请求被拦截
|
||
|
if info.Msg == "" {
|
||
|
info.Msg = "Unknown"
|
||
|
}
|
||
|
return nil, exception.ErrSearchVideo(
|
||
|
fmt.Sprintf("最新视频搜索信息[keyword=%s]获取失败:%s(Code: %d)", keyword, info.Msg, info.Code))
|
||
|
}
|
||
|
return info.Data.Result, nil
|
||
|
|
||
|
}
|
||
|
|
||
|
func UpdateVideoCheckpoint(value string) error {
|
||
|
return AddOrUpdateMetadata("SearchVideoCheckpoint", value)
|
||
|
}
|
||
|
|
||
|
// SearchLiveRooms b站搜索 默认排序 仅搜索直播间
|
||
|
func SearchLiveRooms(keyword, cookie string, page uint) ([]*pb.SearchLiveRoomResult, error) {
|
||
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||
|
defer cancel()
|
||
|
info, err := rpc.SpiderCore().SearchLiveRoom(ctx, &pb.SearchLiveRoomReq{
|
||
|
Keyword: keyword,
|
||
|
Order: "online",
|
||
|
Page: uint32(page),
|
||
|
Cookie: cookie,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return nil, exception.InternalError("获取直播间搜索信息失败:" + err.Error())
|
||
|
}
|
||
|
|
||
|
if info.Code != 200 {
|
||
|
if info.Msg == "" {
|
||
|
info.Msg = "Unknown"
|
||
|
}
|
||
|
return nil, exception.ErrSearchLiveRoom(
|
||
|
fmt.Sprintf("直播间搜索信息[keyword=%s]获取失败:%s(Code: %d)", keyword, info.Msg, info.Code))
|
||
|
}
|
||
|
return info.Data.Result, nil
|
||
|
}
|
||
|
|
||
|
func GetLiveRoomPageNum(keyword, cookie string) (int, error) {
|
||
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||
|
defer cancel()
|
||
|
info, err := rpc.SpiderCore().SearchLiveRoom(ctx, &pb.SearchLiveRoomReq{
|
||
|
Keyword: keyword,
|
||
|
Order: "online",
|
||
|
Page: 1,
|
||
|
Cookie: cookie,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return 0, exception.InternalError("获取直播间搜索信息失败:" + err.Error())
|
||
|
}
|
||
|
|
||
|
if info.Code != 200 {
|
||
|
if info.Msg == "" {
|
||
|
info.Msg = "Unknown"
|
||
|
}
|
||
|
return 0, exception.ErrSearchLiveRoom(
|
||
|
fmt.Sprintf("直播间搜索信息[keyword=%s]获取失败:%s(Code: %d)", keyword, info.Msg, info.Code))
|
||
|
}
|
||
|
|
||
|
return int(info.Data.NumPages), nil
|
||
|
}
|
||
|
|
||
|
// GetLiveRoomByRoomID 通过room_id从数据库查询直播间信息 返回值不为nil
|
||
|
func GetLiveRoomByRoomID(roomID uint64) (*model.LiveRoom, error) {
|
||
|
var room model.LiveRoom
|
||
|
tx := dao.DB().Model(&model.LiveRoom{}).Where("room_id = ?", roomID).Find(&room)
|
||
|
if tx.Error != nil {
|
||
|
return &room, tx.Error
|
||
|
}
|
||
|
return &room, nil
|
||
|
}
|
||
|
|
||
|
func UpdateLiveRoomInfo(rooms []*pb.SearchLiveRoomResult) (int, error) {
|
||
|
err := dao.DB().Transaction(func(tx *gorm.DB) error {
|
||
|
for _, room := range rooms {
|
||
|
roomInfo, err := GetLiveRoomByRoomID(room.Roomid)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
roomInfo.RoomID = uint(room.Roomid)
|
||
|
roomInfo.Title = room.Title
|
||
|
roomInfo.Tags = room.Tags
|
||
|
if err = tx.Save(roomInfo).Error; err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
err = dao.DB().Transaction(func(tx *gorm.DB) error {
|
||
|
for _, room := range rooms {
|
||
|
exists, err := IsUserExistsByUID(uint(room.Uid))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if !exists {
|
||
|
tx.Create(&model.Furry{
|
||
|
UID: uint(room.Uid),
|
||
|
Name: room.Uname,
|
||
|
Status: model.StatusPendingLive,
|
||
|
LiveRoomID: uint(room.Roomid),
|
||
|
})
|
||
|
continue
|
||
|
}
|
||
|
if err = tx.Model(&model.Furry{}).Where("uid = ?", room.Uid).Update("live_room_id", room.Roomid).Error; err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
return len(rooms), nil
|
||
|
}
|