Skip to content

Commit

Permalink
feat: 添加参数配置
Browse files Browse the repository at this point in the history
- 支持自定义 Sitemap 自定义推送条数,在推送时配置 -n 参数,比如推送前 10 条,可以配置 -n 10
  • Loading branch information
greycodee committed Apr 20, 2022
1 parent eb522e9 commit 4f875df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
pushType string
filePath string
webMaster string
pushCount int
)

func init() {
Expand All @@ -26,6 +27,7 @@ func init() {
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() {
Expand Down Expand Up @@ -57,12 +59,13 @@ func singlePush() {
}

func urlsFilePush() {
p := new(parse.UrlsFile)
p := parse.NewSiteMap(pushCount)
parseFileAndPush(p)
}

func sitemapPush() {
p := new(parse.SiteMap)
p := parse.NewSiteMap(pushCount)
//var p = &parse.SiteMap{2}
parseFileAndPush(p)
}

Expand Down
13 changes: 10 additions & 3 deletions tool/parse/parse_sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import (
</urlset>
*/
type SiteMap struct {
urlCount int
}

func NewSiteMap(pushCount int) *SiteMap {
return &SiteMap{pushCount}
}

type urlSet struct {
Expand Down Expand Up @@ -46,7 +50,7 @@ func (s SiteMap) ParseRemoteFile(url string) (urls []string,err error) {
if err != nil {
return
}
return parseXml(content)
return s.parseXml(content)
}

func (s SiteMap) ParseLocalFile(filePath string) (urls []string,err error) {
Expand All @@ -64,17 +68,20 @@ func (s SiteMap) ParseLocalFile(filePath string) (urls []string,err error) {
if err != nil {
return
}
return parseXml(content)
return s.parseXml(content)
}


func parseXml(content []byte) (urls []string,err error) {
func (s SiteMap) parseXml(content []byte) (urls []string,err error) {
log.Println("开始解析XML")
sm := urlSet{}
err = xml.Unmarshal(content, &sm)
if err != nil {
return
}
if s.urlCount>0 {
sm.Url = sm.Url[:s.urlCount]
}
for _,v := range sm.Url{
log.Printf("【SITE-MAP】解析到url:%s",v.Loc)
urls = append(urls,v.Loc)
Expand Down

0 comments on commit 4f875df

Please sign in to comment.