You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.5 KiB

4 years ago
package baiduapi
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const (
//url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined"
aPIKey = "OH7VGIOa7mUQXNKRpDwvywuk"
secretKey = "tSbkq2ntrFQzSotG1Swv92wvBPmjvKLw"
)
type BdAudit struct {
}
/*{ https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2& 目前只需要用到Access_token
"refresh_token": ,
"expires_in": 2592000,
"scope": "public wise_adapt",
"session_key": "9mzdDAYpaA7hnkLhBZHyFS8w8++LPHDsgoOlz9v3K8Gbtxn6fMCVojbFkwNc9JrQD9Y0htiZvSQEZHP6\/DjVjUmAZN3QO",
"scope"
"access_token": "24.737badb6a34ceec6c9f2cea76dc39c37.2592000.1627527054.282335-24455959",
"session_secret": "dfac94a3489fe9fca7c3221cbf7525ff"
}*/
type AccRespAll struct {
RefreshToken string `json:"refresh_token"`
ExpiresIn int64 `json:"expires_in"`
SessionKey string `json:"session_key"`
AccessToken string `json:"access_token"`
Scope string `json:"scope"`
SessionSecret string `json:"session_secret"`
}
//获取access_token
func (r *BdAudit) GetAccessToken() AccRespAll {
u, _ := url.Parse("https://aip.baidubce.com/oauth/2.0/token")
q := u.Query()
q.Set("grant_type", "client_credentials")
q.Set("client_id", aPIKey)
q.Set("client_secret", secretKey)
u.RawQuery = q.Encode()
//u := "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials" + "&client_id=" + aPIKey + "&client_secret=" + secretKey
resp, err := http.Post(u.String(),
"application/x-www-form-urlencoded",
strings.NewReader(""))
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(body))
var al AccRespAll
err = json.Unmarshal(body, &al)
if err != nil {
fmt.Println(err)
}
return al
}
type ImageResp struct {
LogId int64 `json:"log_id"`
ErrorCode int64 `json:"error_code"`
ErrorMsg string `json:"error_msg"`
Conclusion string `json:"conclusion"`
ConclusionType uint `json:"conclusionType"`
}
//内容审核--图像审核
func (r *BdAudit) ImageAudit(img string) ImageResp {
//图像审核,传入图像的路由地址
method := "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined"
u, _ := url.Parse(method)
q := u.Query()
q.Set("access_token", r.GetAccessToken().AccessToken)
q.Set("imgUrl", img)
u.RawQuery = q.Encode()
resp, err := http.Post(u.String(),
"application/x-www-form-urlencoded",
strings.NewReader(""))
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(body))
var rest ImageResp
json.Unmarshal(body, &rest)
return rest
}
//内容审核--文本审核
func (r *BdAudit) TextAudit(text string) ImageResp {
method := "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined"
u, _ := url.Parse(method)
q := u.Query()
q.Set("access_token", r.GetAccessToken().AccessToken)
q.Set("text", text)
u.RawQuery = q.Encode()
resp, err := http.Post(u.String(), "application/x-www-form-urlencoded", strings.NewReader(""))
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
}
var rest ImageResp
json.Unmarshal(body, &rest)
return rest
}
type VideoResp struct {
LogId int64 `json:"log_id"`
ErrorCode uint `json:"error_code"`
ErrorMsg string `json:"error_msg"`
Conclusion string `json:"conclusion"`
ConclusionType int `json:"conclusion_type"`
IsHitMd5 bool `json:"isHitMd5"`
Msg string `json:"msg"`
}
//短视频审核
func (r *BdAudit) VideoAudit(name, videoUrl, id string) VideoResp {
method := "https://aip.baidubce.com/rest/2.0/solution/v1/video_censor/v2/user_defined"
u, _ := url.Parse(method)
q := u.Query()
q.Set("access_token", r.GetAccessToken().AccessToken)
q.Set("name", name)
q.Set("videoUrl", videoUrl)
q.Set("extId", id)
u.RawQuery = q.Encode()
resp, e := http.Post(u.String(), "application/x-www-form-urlencoded", strings.NewReader(""))
if e != nil {
fmt.Println(e.Error(), "12321321")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error(), "123213")
}
//fmt.Println(string(body))
var rest VideoResp
json.Unmarshal(body, &rest)
fmt.Println("err.Error()")
return rest
}