Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package recomapi
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
FBT_Star string = "star"
|
||||
FBT_Like string = "like"
|
||||
FBT_Read string = "read"
|
||||
)
|
||||
|
||||
type RSItemInfo struct {
|
||||
Comment string `json:"Comment"`
|
||||
ItemId string `json:"ItemId"`
|
||||
Labels []string `json:"Labels"`
|
||||
Timestamp time.Time `json:"Timestamp"`
|
||||
}
|
||||
|
||||
type RSItemInfoList []*RSItemInfo
|
||||
|
||||
type RSUserInfo struct {
|
||||
Comment string `json:"Comment"`
|
||||
UserId string `json:"UserId"`
|
||||
Labels []string `json:"Labels"`
|
||||
Subscribe []string `json:"Subscribe"`
|
||||
Timestamp time.Time `json:"Timestamp"`
|
||||
}
|
||||
|
||||
type RSUserInfoList []*RSUserInfo
|
||||
|
||||
type APIResult struct {
|
||||
RowAffected int64 `json:"RowAffected"`
|
||||
}
|
||||
|
||||
type FeedbackInfo struct {
|
||||
Comment string `json:"Comment"`
|
||||
FeedbackType string `json:"FeedbackType"`
|
||||
ItemId string `json:"ItemId"`
|
||||
Timestamp time.Time `json:"Timestamp"`
|
||||
UserId string `json:"UserId"`
|
||||
}
|
||||
|
||||
type FeedbackInfoList []*FeedbackInfo
|
||||
@@ -0,0 +1,45 @@
|
||||
package recomapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/common/utils"
|
||||
)
|
||||
|
||||
// AddOneFeedback 添加一个推荐反馈
|
||||
func AddOneFeedback(url string, key string,
|
||||
userId int64, mediaId int64, fbType string, comment string) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
feedBack := &FeedbackInfo{
|
||||
UserId: strconv.FormatInt(userId, 10),
|
||||
ItemId: strconv.FormatInt(mediaId, 10),
|
||||
FeedbackType: fbType,
|
||||
Comment: comment,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
return AddManyFeedbacks(url, key, FeedbackInfoList{feedBack})
|
||||
}
|
||||
|
||||
func AddManyFeedbacks(url string, key string, feedbacks FeedbackInfoList) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
reqUrl := url + "/api/feedback"
|
||||
resp := &APIResult{}
|
||||
reqBody, _ := json.Marshal(feedbacks)
|
||||
header := map[string]string{"X-API-Key": key}
|
||||
code, err := utils.POSTJsonWithJResp(resp, reqUrl, header, reqBody)
|
||||
if err != nil {
|
||||
log.Error("recomapi:AddManyFeedbacks POSTJsonWithJResp failed",
|
||||
log.Any("reqUrl", reqUrl), log.E(err))
|
||||
return 0, srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
if code != http.StatusOK {
|
||||
log.Error("recomapi:AddManyFeedbacks POSTJsonWithJResp code error.",
|
||||
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
||||
return 0, srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
return resp.RowAffected, nil
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package recomapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/common/utils"
|
||||
)
|
||||
|
||||
func InsertOneItem(url string, key string, itemInfo *RSItemInfo) *srvdef.SrvErr {
|
||||
reqUrl := url + "/api/item"
|
||||
resp := &APIResult{}
|
||||
reqBody, _ := json.Marshal(itemInfo)
|
||||
header := map[string]string{"X-API-Key": key}
|
||||
code, err := utils.POSTJsonWithJResp(resp, reqUrl, header, reqBody)
|
||||
if err != nil {
|
||||
log.Error("recomapi:InsertOneItem POSTJsonWithJResp failed",
|
||||
log.Any("reqUrl", reqUrl), log.E(err))
|
||||
return srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
if code != http.StatusOK {
|
||||
log.Error("recomapi:InsertOneItem POSTJsonWithJResp code error.",
|
||||
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
||||
return srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func InsertManyItems(url string, key string, itemInfos RSItemInfoList) *srvdef.SrvErr {
|
||||
reqUrl := url + "/api/items"
|
||||
reqBody, _ := json.Marshal(itemInfos)
|
||||
header := map[string]string{"X-API-Key": key}
|
||||
code, err := utils.POSTJsonWithJResp(nil, reqUrl, header, reqBody)
|
||||
if err != nil {
|
||||
log.Error("recomapi:InsertManyItems POSTJsonWithJResp failed",
|
||||
log.Any("reqUrl", reqUrl), log.E(err))
|
||||
return srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
if code != http.StatusOK {
|
||||
log.Error("recomapi:InsertManyItems POSTJsonWithJResp code error.",
|
||||
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
||||
return srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetOneItem(url string, key string, itemId string) (*RSItemInfo, *srvdef.SrvErr) {
|
||||
reqUrl := fmt.Sprintf("%s/api/item/%s", url, itemId)
|
||||
resp := &RSItemInfo{}
|
||||
header := map[string]string{"X-API-Key": key}
|
||||
code, err := utils.GetWithJResp(resp, reqUrl, header, nil)
|
||||
if err != nil {
|
||||
log.Error("recomapi:GetOneItem GetWithJResp failed",
|
||||
log.Any("reqUrl", reqUrl), log.E(err))
|
||||
return nil, srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
if code != http.StatusOK {
|
||||
log.Error("recomapi:GetOneItem GetWithJResp code error.",
|
||||
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
||||
return nil, srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
log.Info("Test", log.Any("resp", resp))
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func GetManyItems(url string, key string, cursor string, size int64) (RSItemInfoList, string, *srvdef.SrvErr) {
|
||||
reqUrl := fmt.Sprintf("%s/api/items?n=%d&cursor=%s", url, size, cursor)
|
||||
type Resp struct {
|
||||
Cursor string `json:"Cursor"`
|
||||
Items RSItemInfoList `json:"Items"`
|
||||
}
|
||||
var resp Resp
|
||||
header := map[string]string{"X-API-Key": key}
|
||||
code, err := utils.GetWithJResp(&resp, reqUrl, header, nil)
|
||||
if err != nil {
|
||||
log.Error("recomapi:GetOneItem GetWithJResp failed",
|
||||
log.Any("reqUrl", reqUrl), log.E(err))
|
||||
return nil, "", srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
if code != http.StatusOK {
|
||||
log.Error("recomapi:GetOneItem GetWithJResp code error.",
|
||||
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
||||
return nil, "", srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
return resp.Items, resp.Cursor, nil
|
||||
}
|
||||
|
||||
func DeleteOneItem(url string, key string, itemId string) (int64, *srvdef.SrvErr) {
|
||||
reqUrl := fmt.Sprintf("%s/api/item/%s", url, itemId)
|
||||
resp := &APIResult{}
|
||||
header := map[string]string{"X-API-Key": key}
|
||||
code, err := utils.DeleteWithJResp(resp, reqUrl, header, nil)
|
||||
if err != nil {
|
||||
log.Error("recomapi:DeleteOneItem DeleteWithJResp failed",
|
||||
log.Any("reqUrl", reqUrl), log.E(err))
|
||||
return 0, srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
if code != http.StatusOK {
|
||||
log.Error("recomapi:DeleteOneItem DeleteWithJResp code error.",
|
||||
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
||||
return 0, srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
return resp.RowAffected, nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package recomapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/common/utils"
|
||||
)
|
||||
|
||||
func GetRecomItems(url string, key string, userId int64, writeBack string, size int64) (
|
||||
[]int64, *srvdef.SrvErr) {
|
||||
reqUrl := fmt.Sprintf("%s/api/recommend/%d?n=%d&write-back=%s", url, userId, size, writeBack)
|
||||
var resp []string
|
||||
header := map[string]string{"X-API-Key": key}
|
||||
code, err := utils.GetWithJResp(&resp, reqUrl, header, nil)
|
||||
if err != nil {
|
||||
log.Error("recomapi:GetRecomItems GetWithJResp failed",
|
||||
log.Any("reqUrl", reqUrl), log.E(err))
|
||||
return nil, srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
if code != http.StatusOK {
|
||||
log.Error("recomapi:GetRecomItems GetWithJResp code error.",
|
||||
log.Any("reqUrl", reqUrl), log.Any("code", code))
|
||||
return nil, srvdef.New(srvdef.Err, "", "")
|
||||
}
|
||||
itemIds := make([]int64, 0)
|
||||
for _, v := range resp {
|
||||
id, err := strconv.ParseInt(v, 10, 64)
|
||||
if err == nil {
|
||||
itemIds = append(itemIds, id)
|
||||
}
|
||||
}
|
||||
return itemIds, nil
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package recomapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
"videoweb/pub/common/log"
|
||||
)
|
||||
|
||||
var (
|
||||
testUrl = "http://139.177.187.219:8087"
|
||||
testKey = ""
|
||||
)
|
||||
|
||||
func Test_InsertOneItem(t *testing.T) {
|
||||
itemInfo := &RSItemInfo{
|
||||
Comment: "Comment1",
|
||||
ItemId: "10004",
|
||||
Labels: []string{"lable1", "lable2", "lable3"},
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
serr := InsertOneItem(testUrl, testKey, itemInfo)
|
||||
fmt.Printf("\nTest_pingReq %s %v\n", itemInfo, serr)
|
||||
}
|
||||
|
||||
func Test_InsertManyItems(t *testing.T) {
|
||||
list := make(RSItemInfoList, 0)
|
||||
itemInfo1 := &RSItemInfo{
|
||||
Comment: "Comment1",
|
||||
ItemId: "10005",
|
||||
Labels: []string{"lable1", "lable2", "lable3"},
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
list = append(list, itemInfo1)
|
||||
itemInfo2 := &RSItemInfo{
|
||||
Comment: "Comment1",
|
||||
ItemId: "10006",
|
||||
Labels: []string{"lable1", "lable2", "lable3"},
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
list = append(list, itemInfo2)
|
||||
serr := InsertManyItems(testUrl, testKey, list)
|
||||
fmt.Printf("\nTest_InsertManyItems %v\n", serr)
|
||||
}
|
||||
|
||||
func Test_GetOneItem(t *testing.T) {
|
||||
itemInfo, serr := GetOneItem(testUrl, testKey, "10004")
|
||||
fmt.Printf("\nTest_GetOneItem %v %v\n", itemInfo, serr)
|
||||
}
|
||||
|
||||
func Test_GetManyItem(t *testing.T) {
|
||||
itemInfos, cursor, serr := GetManyItems(testUrl, testKey, "10003", 2)
|
||||
log.Info("Test_GetManyItem", log.Any("itemInfos", itemInfos),
|
||||
log.Any("cursor", cursor), log.E(serr))
|
||||
//fmt.Printf("\nTest_GetOneItem %v %v\n", itemInfos, serr)
|
||||
}
|
||||
|
||||
func Test_DeleteOneItem(t *testing.T) {
|
||||
count, serr := DeleteOneItem(testUrl, testKey, "10004")
|
||||
log.Info("Test_DeleteOneItem", log.Any("count", count), log.E(serr))
|
||||
//fmt.Printf("\nTest_GetOneItem %v %v\n", itemInfos, serr)
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
//feedback
|
||||
|
||||
func Test_AddOneFeedback(t *testing.T) {
|
||||
count, serr := AddOneFeedback(testUrl, testKey, 18531, 16346, FBT_Star, "feedback1")
|
||||
fmt.Printf("\nTest_pingReq %d %v\n", count, serr)
|
||||
}
|
||||
|
||||
// /reccommend
|
||||
func Test_GetManyRecomItems(t *testing.T) {
|
||||
itemIds, serr := GetRecomItems(testUrl, testKey, 18531, "test", 10)
|
||||
log.Info("Test_GetManyRecomItems", log.Any("itemIds", itemIds), log.E(serr))
|
||||
//fmt.Printf("\nTest_GetOneItem %v %v\n", itemInfos, serr)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package recomapi
|
||||
Reference in New Issue
Block a user