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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
package main
import (
"bytes"
"encoding/json"
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"io/ioutil"
"net/http"
"os"
"reflect"
"regexp"
"strings"
)
const (
googleApi = "http://www.google.com/ping?sitemap={{your_sitemap_url}}"
bingApi = "https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey={{your_api_key}}"
baiduApi = "http://data.zz.baidu.com/urls?site={{your_site_url}}&token={{your token}}"
//其他语言,看自己是否需要
zhFile = "xxx/sitemap.xml"
jaFile = "xxx/sitemap.xml"
enFile = "xxx/sitemap.xml"
//简单保存提交记录,避免重复提交
databases = "url.db"
siteGoogle = 0
siteBing = 1
siteBaidu = 2
)
var db *gorm.DB
type Url struct {
Url string
Site int
}
func main() {
var err error
db, err = gorm.Open(sqlite.Open(databases), &gorm.Config{})
if err != nil {
panic(err)
}
if err = db.AutoMigrate(&Url{}); err != nil {
panic(err)
}
googleCommit()
bingCommit()
baiduCommit()
}
// 使用 ping 的方式请求谷歌抓取站点数据
func googleCommit() {
response, err := http.Get(googleApi)
if err != nil {
fmt.Println("google commit err: ", err)
return
}
if response.StatusCode == http.StatusOK {
fmt.Println("google commit ok")
} else {
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("google commit err: ", string(body), response.StatusCode, response.Status)
}
}
// 每天10000个url,没有月配额
func bingCommit() {
urls, err := getUrlsFromXml("")
if err != nil {
fmt.Println("bing commit err: ", err)
return
}
urls, err = urlDeduplication(urls, siteBing)
if err != nil {
fmt.Println("bing commit err: ", err)
return
}
if urls == nil || len(urls) == 0 {
fmt.Print("bing commit nothing")
return
}
mp := map[string]interface{}{
"siteUrl": "https://arturiamu.github.io",
"urlList": urls,
}
js, err := json.Marshal(mp)
if err != nil {
fmt.Println("bing commit err: ", err)
return
}
request, err := http.NewRequest("post", bingApi, bytes.NewReader([]byte(js)))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("charset", "utf-8")
client := http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Println("bing commit err: ", err)
return
}
bd, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("bing commit err: ", err)
return
}
if response.StatusCode == http.StatusOK {
fmt.Println("bing commit ok", string(bd))
for _, url := range urls {
db.Create(&Url{Site: siteBing, Url: url})
}
fmt.Println("bing save url ok")
} else {
fmt.Println("bing commit err: ", string(bd), response.StatusCode, response.Status)
return
}
}
func baiduCommit() {
urls, err := getUrlsFromXml("blog.mulinbiao.com")
if err != nil {
fmt.Println("baidu commit err: ", err)
return
}
urls, err = urlDeduplication(urls, siteBaidu)
if err != nil {
fmt.Println("baidu commit err: ", err)
return
}
if urls == nil || len(urls) == 0 {
fmt.Print("baidu commit nothing")
return
}
str := strings.Join(urls, "\n")
request, err := http.NewRequest("POST", baiduApi, bytes.NewReader([]byte(str)))
if err != nil {
fmt.Println("baidu commit err: ", err)
return
}
//request.Header.Set("User-Agent", "curl/7.12.1")
//request.Header.Set("Host", "data.zz.baidu.com")
//request.Header.Set("Content-Length", "83")
request.Header.Set("Content-Type", "text/plain")
client := http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Println("baidu commit err: ", err)
return
}
bd, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("baidu commit err: ", err)
return
}
if response.StatusCode == http.StatusOK {
fmt.Println("baidu commit ok", string(bd))
for _, url := range urls {
db.Create(&Url{Site: siteBaidu, Url: url})
}
fmt.Println("baidu save url ok")
} else {
fmt.Println("baidu commit err: ", string(bd), response.StatusCode, response.Status)
return
}
}
// 去重
func urlDeduplication(us []string, site int) (urls []string, err error) {
var lst []Url
err = db.Where("site = ?", site).Find(&lst).Error
if err != nil {
return
}
if lst != nil && len(lst) > 0 {
for _, u := range us {
if indexUrlArr(lst, u) == -1 {
urls = append(urls, u)
}
}
} else {
for _, u := range us {
urls = append(urls, u)
}
}
return urls, nil
}
func indexUrlArr(arr []Url, target string) int {
for i, s := range arr {
if s.Url == target {
return i
}
}
return -1
}
// 由于使用blog.mulinbiao.com指向arturiamu.github.io
// 百度只能提交blog.mulinbiao.com,需要做转换,看自己博客需求
func getUrlsFromXml(domain string) (urls []string, err error) {
fzh, err := os.ReadFile(zhFile)
if err != nil {
fmt.Println("open file err: ", err)
return
}
fja, err := os.ReadFile(jaFile)
if err != nil {
fmt.Println("open file err: ", err)
return
}
fen, err := os.ReadFile(enFile)
if err != nil {
fmt.Println("open file err: ", err)
return
}
var arr []string
var mp = make(map[string]struct{})
//使用正则表达式简单从xml中提取url,可以优化
r := regexp.MustCompile(`\bhttps?://\S+\b`)
arr = append(arr, r.FindAllString(string(fzh), -1)...)
arr = append(arr, r.FindAllString(string(fja), -1)...)
arr = append(arr, r.FindAllString(string(fen), -1)...)
for _, a := range arr {
aa := a
if !strings.Contains(aa, "https://arturiamu.github.io") {
continue
}
if domain != "" {
aa = strings.ReplaceAll(aa, "arturiamu.github.io", domain)
}
aa = strings.ReplaceAll(aa, "</loc", "")
if !strings.HasSuffix(aa, "/") {
aa = aa + "/"
}
if _, ok := mp[aa]; ok {
continue
}
mp[aa] = struct{}{}
urls = append(urls, aa)
}
return urls, nil
}
|