143 lines
3.4 KiB
Go
143 lines
3.4 KiB
Go
package laosiji
|
|
|
|
import (
|
|
"91porn-server/common/log"
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func getMovieSearchUrl() string {
|
|
return fmt.Sprintf("%s/lsjapi/movie/search", APIUrl)
|
|
}
|
|
|
|
func getMovieDetailUrl() string {
|
|
return fmt.Sprintf("%s/lsjapi/movie/detail", APIUrl)
|
|
}
|
|
|
|
func getMoviedDetailByMidUrl() string {
|
|
return fmt.Sprintf("%s/lsjapi/movie/detailByMid", APIUrl)
|
|
}
|
|
|
|
// StructToMapViaJSON 通过 JSON 转换结构体到 map
|
|
func StructToMapViaJSON(obj interface{}) (map[string]interface{}, error) {
|
|
// 将结构体转换为 JSON
|
|
jsonData, err := json.Marshal(obj)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 将 JSON 解析为 map
|
|
var result map[string]interface{}
|
|
err = json.Unmarshal(jsonData, &result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// MovieSearch 视频查询
|
|
func MovieSearch(ctx context.Context, req MovieSearchReq) (resp MovieSearchResp, err error) {
|
|
endpoint := getMovieSearchUrl()
|
|
req.Need_total_info = "y"
|
|
|
|
var data map[string]interface{}
|
|
data, err = StructToMapViaJSON(req)
|
|
if err != nil {
|
|
log.Error("GenerateUndress post fail", log.E(err))
|
|
return resp, err
|
|
}
|
|
|
|
err = moviePost(ctx, endpoint, data, &resp)
|
|
if err != nil {
|
|
log.Error("GenerateUndress post fail", log.Any("data", data), log.E(err))
|
|
}
|
|
return
|
|
}
|
|
|
|
// MovieDetail 获取视频列表
|
|
func MovieDetail(ctx context.Context, req MovieDetailReq) (resp MovieDetailResp, err error) {
|
|
endpoint := getMovieDetailUrl()
|
|
|
|
var data map[string]interface{}
|
|
data, err = StructToMapViaJSON(req)
|
|
if err != nil {
|
|
log.Error("GenerateUndress post fail", log.E(err))
|
|
return resp, err
|
|
}
|
|
|
|
err = moviePost(ctx, endpoint, data, &resp)
|
|
if err != nil {
|
|
log.Error("GenerateUndress post fail", log.Any("data", data), log.E(err))
|
|
}
|
|
return
|
|
}
|
|
|
|
func moviePost(ctx context.Context, endpoint string, req map[string]interface{}, response interface{}) (err error) {
|
|
if err = ensureConfigured(); err != nil {
|
|
return err
|
|
}
|
|
jsonData, err := json.Marshal(req)
|
|
if err != nil {
|
|
log.Warn("GenerateAiUndress", log.E(err))
|
|
return
|
|
}
|
|
encryptedData, err := encryptBase64(string(jsonData), APIKey)
|
|
if err != nil {
|
|
log.Warn("GenerateAiUndress encryptBase64", log.E(err))
|
|
return
|
|
}
|
|
// 创建 POST 请求
|
|
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer([]byte(encryptedData)))
|
|
if err != nil {
|
|
return
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
httpReq.Header.Set("Content-Length", fmt.Sprintf("%d", len(encryptedData)))
|
|
httpReq.Header.Set("appid", Appid)
|
|
|
|
// 发送请求
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
|
resp, err := client.Do(httpReq)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// 读取响应体
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// 检查 HTTP 状态码
|
|
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
|
return fmt.Errorf("laosiji movie api http status:%d body:%s", resp.StatusCode, truncateToolBody(body, APIKey, Appid))
|
|
}
|
|
// 解析 JSON
|
|
var res Response
|
|
if err = json.Unmarshal(body, &res); err != nil {
|
|
return
|
|
}
|
|
if res.Status != "y" {
|
|
err = fmt.Errorf("errorCode:%v error:%s", res.ErrorCode, truncateToolText(res.Error, APIKey, Appid))
|
|
return
|
|
}
|
|
|
|
// 进行解密
|
|
dataStr, err := decryptBase64(res.Data, APIKey)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = json.Unmarshal([]byte(dataStr), response)
|
|
if err != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|