-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
146 lines (131 loc) · 3.24 KB
/
main.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
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
package main
import (
"flag"
"github.com/greycodee/seo-tools/core"
"github.com/greycodee/seo-tools/core/baidu"
"github.com/greycodee/seo-tools/core/bing"
"github.com/greycodee/seo-tools/core/google"
"github.com/greycodee/seo-tools/tool/parse"
"log"
"regexp"
)
var (
pushUrl string
pushType string
filePath string
webMaster string
pushCount int
)
func init() {
flag.StringVar(&pushUrl,"u","","singe url publish\n(Used when the -t parameter is single)")
flag.StringVar(&pushType,"t","single","push urls Type:\n urlsFile\n sitemap\n")
flag.StringVar(&filePath,"f","","local file path or remote file path\n(Used when the -t parameter is urlsFile or sitemap)")
flag.StringVar(&webMaster,"w","all","chose webmaster service:\n google\n baidu\n bing\n")
flag.IntVar(&pushCount,"n",0,"push count.If not configured, all are pushed by default. Only valid for sitemap")
flag.Parse()
}
func main() {
start()
}
func start() {
switch pushType {
case "single":
singlePush()
break
case "urlsFile":
urlsFilePush()
break
case "sitemap":
sitemapPush()
break
default:
panic("请选择正确的推送类型【urlsFile、sitemap】")
}
}
func singlePush() {
// 单条 url
if pushUrl == "" {
panic("输入要推送的 URL 网址")
}
componentPushUrl(pushUrl)
}
func urlsFilePush() {
p := parse.NewSiteMap(pushCount)
parseFileAndPush(p)
}
func sitemapPush() {
p := parse.NewSiteMap(pushCount)
//var p = &parse.SiteMap{2}
parseFileAndPush(p)
}
func parseFileAndPush(p parse.Parse) {
if filePath == "" {
panic("请输入本地文件或远程文件路径")
}
isLocalFile := false
// 判断文件是远程还是本地
reg := regexp.MustCompile("[a-zA-z]+://[^\\s]*")
if reg == nil { //解释失败,返回nil
panic("正则表达式异常")
}
result:=reg.FindAllString(filePath,-1)
if len(result) == 0 {
// 说明不是 URL,默认认为它是本地文件路径
isLocalFile = true
}
var urls []string
var err error
if isLocalFile {
urls,err = p.ParseLocalFile(filePath)
if err != nil {
panic("解析本地文件失败")
}
}else {
urls,err = p.ParseRemoteFile(filePath)
if err != nil {
panic("解析远程文件失败")
}
}
componentPushUrlList(urls)
}
func chosePushWebMaster() (pushServices []core.PushUrl) {
switch webMaster {
case "google":
pushServices = append(pushServices,new(google.IndexService))
break
case "baidu":
pushServices = append(pushServices,new(baidu.IndexService))
break
case "bing":
pushServices = append(pushServices,new(bing.IndexService))
break
case "all":
pushServices = append(pushServices,new(google.IndexService))
pushServices = append(pushServices,new(baidu.IndexService))
pushServices = append(pushServices,new(bing.IndexService))
break
default:
panic("WebMaster选择错误")
}
return
}
func componentPushUrl(url string) {
pushServices := chosePushWebMaster()
for _,v := range pushServices {
resp, err := v.PushUrl(url)
if err != nil {
log.Fatalln(err)
}
log.Printf("推送结果:%s",resp)
}
}
func componentPushUrlList(urls []string) {
pushServices := chosePushWebMaster()
for _,v := range pushServices {
resp, err := v.PushListUrl(urls)
if err != nil {
log.Fatalln(err)
}
log.Printf("推送结果:%s",resp)
}
}