-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuniversal.go
79 lines (65 loc) · 2.18 KB
/
universal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package urlsubmitter
import (
"errors"
"fmt"
)
type UniversalOptions struct {
BaiduAPI string // Baidu搜索资源平台-资源提交-普通收录-API提交-推送接口
BingKey string // Key for the Bing IndexNow API.
BingKeyLocation string // Location of the Bing key file.
BingHost string // Host name of the site.
GoogleCredentialsFile string // 谷歌indexing-api服务账号密钥文件路径
}
// UniversalSubmitter submits URLs to all platforms.
type UniversalSubmitter struct {
BaiduSubmitter *BaiduSubmitter
BingSubmitter *BingSubmitter
GoogleSubmitter *GoogleSubmitter
}
// NewUniversalSubmitter creates a new UniversalSubmitter.
func NewUniversalSubmitter(options *UniversalOptions) *UniversalSubmitter {
var baiduSubmitter *BaiduSubmitter
var bingSubmitter *BingSubmitter
var googleSubmitter *GoogleSubmitter
if options.BaiduAPI != "" {
baiduSubmitter = NewBaiduSubmitter(options.BaiduAPI)
}
if options.BingKey != "" && options.BingKeyLocation != "" && options.BingHost != "" {
bingSubmitter = NewBingSubmitter(options.BingKey, options.BingKeyLocation, options.BingHost)
}
if options.GoogleCredentialsFile != "" {
googleSubmitter = NewGoogleSubmitter(options.GoogleCredentialsFile)
}
if baiduSubmitter == nil && bingSubmitter == nil && googleSubmitter == nil {
return nil
}
return &UniversalSubmitter{
BaiduSubmitter: baiduSubmitter,
BingSubmitter: bingSubmitter,
GoogleSubmitter: googleSubmitter,
}
}
// SubmitURLs submits URLs to all platforms.
func (s *UniversalSubmitter) SubmitURLs(urls []string) (map[string]string, error) {
results := make(map[string]string)
var errs error
baiduResult, err := s.BaiduSubmitter.SubmitURLs(urls)
if err != nil {
errs = errors.Join(errs, fmt.Errorf("BaiduError: %w", err))
} else {
results["baidu"] = baiduResult
}
bingResult, err := s.BingSubmitter.SubmitURLs(urls)
if err != nil {
errs = errors.Join(errs, fmt.Errorf("BingError: %w", err))
} else {
results["bing"] = bingResult
}
googleResult, err := s.GoogleSubmitter.SubmitURLs(urls)
if err != nil {
errs = errors.Join(errs, fmt.Errorf("GoogleError: %w", err))
} else {
results["google"] = googleResult
}
return results, errs
}