infoGetter/api/detail.go

132 lines
4.7 KiB
Go
Raw Permalink Normal View History

2021-12-02 13:19:43 +08:00
package api
import (
"database/sql/driver"
"encoding/json"
"fmt"
ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap"
"time"
)
type (
DetailReq struct {
ScriptId string `json:"scriptId"`
CityCode string `json:"cityCode"`
}
DetailInfo struct {
Resp DetailResp `json:"resp" gorm:"column:resp"`
ID int `json:"id" gorm:"column:id"`
}
DetailResp struct {
Head struct {
Msg string `json:"msg"`
Code int `json:"code"`
} `json:"head" gorm:"column:head"`
Data struct {
ScriptId string `json:"scriptId"`
ScriptName string `json:"scriptName"`
ScriptCoverUrl string `json:"scriptCoverUrl"`
ScriptIntro string `json:"scriptIntro"`
ScriptCategory int `json:"scriptCategory"`
ScriptPlayerLimit int `json:"scriptPlayerLimit"`
ScriptMalePlayerLimit int `json:"scriptMalePlayerLimit"`
ScriptFemalePlayerLimit int `json:"scriptFemalePlayerLimit"`
ScriptTag string `json:"scriptTag"`
ScriptScore float64 `json:"scriptScore"`
ScriptScoreCount int `json:"scriptScoreCount"`
ScriptInferenceScore float64 `json:"scriptInferenceScore"`
ScriptPlotScore float64 `json:"scriptPlotScore"`
ScriptComplexScore float64 `json:"scriptComplexScore"`
ScriptImageContent string `json:"scriptImageContent"`
PlayerRoleReversalStatus int `json:"playerRoleReversalStatus"`
ScriptTextContent string `json:"scriptTextContent"`
ScriptSourceId interface{} `json:"scriptSourceId"`
ScriptGroupCount int `json:"scriptGroupCount"`
ShopHaveScriptCount int `json:"shopHaveScriptCount"`
ScriptWantPlayerCount int `json:"scriptWantPlayerCount"`
ScriptPlayedCount int `json:"scriptPlayedCount"`
WantPlayStatus int `json:"wantPlayStatus"`
UserPlayScriptStatus int `json:"userPlayScriptStatus"`
EvaluateStatus int `json:"evaluateStatus"`
UserPlayScriptTime interface{} `json:"userPlayScriptTime"`
UserScriptScore interface{} `json:"userScriptScore"`
UserPlayScriptId interface{} `json:"userPlayScriptId"`
ShopScriptReserveCount int `json:"shopScriptReserveCount"`
GroupDuration int `json:"groupDuration"`
RecommendUrl string `json:"recommendUrl"`
RecommendNum int `json:"recommendNum"`
ScriptDifficultyDegreeName string `json:"scriptDifficultyDegreeName"`
} `json:"data"`
}
)
const DetailUrl = "https://api.h5.helloaba.cn/script/platformScriptInfo"
func (m *DetailInfo) TableName() string {
return "detail_info"
}
// Value 实现gorm针对json数据结构的interface
func (p DetailResp) Value() (driver.Value, error) {
return json.Marshal(p)
}
func (p *DetailResp) Scan(input interface{}) error {
return json.Unmarshal(input.([]byte), &p)
}
// DetailMarshal 将http访问之后的[]byte 解析为DetailResp
func DetailMarshal(req, body []byte) (resp interface{}, err error) {
r := new(DetailResp)
err = json.Unmarshal(body, r)
if err != nil {
ctxLogger.Error(nil, "json UnmarshalError", zap.String("resp body", string(body)))
}
if r.Head.Code != 200 || r.Head.Msg != "成功" {
ctxLogger.Error(nil, "net wrok error", zap.String("resq body", string(req)), zap.String("resp body", string(body)))
}
return r, err
}
// DetailKvStr todo抽象
// DetailKvStr 按照顺序对查询的key排序
func DetailKvStr(body DetailReq) string {
//strings := []string{"cityCode", "scriptId"}
//sort.Strings(strings)
//cityCode scriptId
var s = ""
s += fmt.Sprintf("%v=%v&", "cityCode", body.CityCode)
s += fmt.Sprintf("%v=%v", "scriptId", body.ScriptId)
return s
}
//单点访问
func detail(body DetailReq) {
dto, err := Attach(DetailUrl, GetCheckSum(DetailKvStr(body)), body, M, DetailMarshal)
if err != nil {
ctxLogger.Error(nil, "net error record")
}
if s, ok := dto.(*DetailResp); !ok {
ctxLogger.Error(nil, "assertion error")
} else {
info := DetailInfo{Resp: *s}
DB.Create(&info)
ctxLogger.Info(nil, "detailOk", zap.String("sid", s.Data.ScriptId))
time.Sleep(time.Duration(1) * time.Second)
}
}
//todo:页数抽离出来
//从db中拿出 list的item 中的id进行轮训访问
func multiReq() {
var infos []BasicInfo
DB.Where("page > 100 and page <= 573").Find(&infos)
for _, v := range infos {
for _, v := range v.Resp.Data.Items {
req := DetailReq{ScriptId: v.ScriptID, CityCode: "310000"}
detail(req)
}
}
}