Skip to content

Commit

Permalink
feat: Support Baidu Content Audit Platform
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records authored and LinkinStars committed Oct 8, 2024
1 parent ae31793 commit 4f6733f
Show file tree
Hide file tree
Showing 8 changed files with 529 additions and 0 deletions.
12 changes: 12 additions & 0 deletions reviewer-baidu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Baidu Reviewer

> Baidu Content Audit Platform is a service platform for intelligent auditing of multimedia content. It can be used to filter spam.
## Config

- `api_key`: Baidu App's API key
- `secret_key`: Baidu App's Secret Key

## Document

- https://cloud.baidu.com/doc/ANTIPORN/s/Vk3h6xaga
163 changes: 163 additions & 0 deletions reviewer-baidu/basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package basic

import (
"embed"
"encoding/json"
"github.com/apache/incubator-answer-plugins/util"

"github.com/apache/incubator-answer-plugins/reviewer-baidu/i18n"
"github.com/apache/incubator-answer/plugin"
"github.com/lufei/baidu-golang-sdk/aip/censor"
myI18n "github.com/segmentfault/pacman/i18n"
"github.com/segmentfault/pacman/log"
)

//go:embed info.yaml
var Info embed.FS

type Reviewer struct {
Config *ReviewerConfig
}

type ReviewerConfig struct {
APIKey string `json:"api_key"`
SecretKey string `json:"secret_key"`
SpamFiltering string `json:"span_filtering"`
}

func init() {
plugin.Register(&Reviewer{
Config: &ReviewerConfig{},
})
}

func (r *Reviewer) Info() plugin.Info {
info := &util.Info{}
info.GetInfo(Info)

return plugin.Info{
Name: plugin.MakeTranslator(i18n.InfoName),
SlugName: info.SlugName,
Description: plugin.MakeTranslator(i18n.InfoDescription),
Author: info.Author,
Version: info.Version,
Link: info.Link,
}
}

func (r *Reviewer) Review(content *plugin.ReviewContent) (result *plugin.ReviewResult) {
result = &plugin.ReviewResult{Approved: true}
if len(r.Config.APIKey) == 0 {
return result
}
// If the author is admin, no need to review
if content.Author.Role > 1 {
return result
}

client := censor.NewClient(r.Config.APIKey, r.Config.SecretKey)
TextCensorResult, err := client.TextCensor(content.Title+"\n"+content.Content, content.IP)
if err != nil {
log.Errorf("Request baidu to check failed: %v", err)
return handleReviewError(content, plugin.ReviewStatusNeedReview)
}

var jsonMap map[string]interface{}
err = json.Unmarshal([]byte(TextCensorResult), &jsonMap)
if err != nil {
return handleReviewError(content, plugin.ReviewStatusNeedReview)
}

if conclusionType, ok := jsonMap["conclusionType"].(float64); ok {
if conclusionType == 1.0 {
return result
}
}

if r.Config.SpamFiltering == "delete" {
return handleReviewError(content, plugin.ReviewStatusDeleteDirectly)
}

return handleReviewError(content, plugin.ReviewStatusNeedReview)
}

func (r *Reviewer) ConfigFields() []plugin.ConfigField {
return []plugin.ConfigField{
{
Name: "api_key",
Type: plugin.ConfigTypeInput,
Title: plugin.MakeTranslator(i18n.ConfigAPIKeyLabel),
Description: plugin.MakeTranslator(i18n.ConfigAPIKeyDescription),
Required: true,
UIOptions: plugin.ConfigFieldUIOptions{
InputType: plugin.InputTypeText,
Label: plugin.MakeTranslator(i18n.ConfigAPIKeyLabel),
},
Value: r.Config.APIKey,
},
{
Name: "secret_key",
Type: plugin.ConfigTypeInput,
Title: plugin.MakeTranslator(i18n.ConfigSecretKeyTitle),
Description: plugin.MakeTranslator(i18n.ConfigSecretKeyDescription),
Required: true,
UIOptions: plugin.ConfigFieldUIOptions{
InputType: plugin.InputTypeText,
Label: plugin.MakeTranslator(i18n.ConfigSecretKeyLabel),
},
Value: r.Config.SecretKey,
},
{
Name: "span_filtering",
Type: plugin.ConfigTypeSelect,
Title: plugin.MakeTranslator(i18n.ConfigSpanFilteringTitle),
Required: false,
UIOptions: plugin.ConfigFieldUIOptions{},
Value: r.Config.SpamFiltering,
Options: []plugin.ConfigFieldOption{
{
Value: "review",
Label: plugin.MakeTranslator(i18n.ConfigSpanFilteringReview),
},
{
Value: "delete",
Label: plugin.MakeTranslator(i18n.ConfigSpanFilteringDelete),
},
},
},
}
}

func handleReviewError(content *plugin.ReviewContent, ReviewStatus plugin.ReviewStatus) *plugin.ReviewResult {
return &plugin.ReviewResult{
Approved: false,
ReviewStatus: ReviewStatus,
Reason: plugin.TranslateWithData(myI18n.Language(content.Language), i18n.CommentNeedReview, nil),
}
}

func (r *Reviewer) ConfigReceiver(config []byte) error {
c := &ReviewerConfig{}
_ = json.Unmarshal(config, c)
r.Config = c
return nil
}
48 changes: 48 additions & 0 deletions reviewer-baidu/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module github.com/apache/incubator-answer-plugins/reviewer-baidu

go 1.21.3

require (
github.com/apache/incubator-answer v1.4.0
github.com/apache/incubator-answer-plugins/util v1.0.2
github.com/lufei/baidu-golang-sdk v0.0.0-20241007032158-d85deddc0d61
github.com/segmentfault/pacman v1.0.5-0.20230822083413-c0075a2d401f
)

require (
github.com/LinkinStars/go-i18n/v2 v2.2.2 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/wire v0.5.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 4f6733f

Please sign in to comment.