forked from StudioSol/sitemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.go
163 lines (136 loc) · 3.88 KB
/
sitemap.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//Generates sitemaps and index files based on the sitemaps.org protocol.
//facilitates the creation of sitemaps for large amounts of urls.
// For a full guide visit https://github.com/StudioSol/Sitemap
package sitemap
import (
"compress/gzip"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync"
"time"
)
var savedSitemaps []string
//clean array of already generated sitemaps (not delete files)
func ClearSavedSitemaps() {
savedSitemaps = []string{}
}
//returns the url of already generated sitemaps
func GetSavedSitemaps() []string {
return savedSitemaps
}
//Creates a new group of sitemaps that used a common name.
//If the sitemap exceed the limit of 50k urls, new sitemaps will have a numeric suffix to the name. Example:
//- blog_1.xml.gz
//- blog_2.xml.gz
func NewSitemapGroup(folder string, name string) *SitemapGroup {
s := new(SitemapGroup)
s.Configure(name, folder)
go s.Initialize()
return s
}
//Creates a new group of sitemaps indice that used a common name.
//If the sitemap exceed the limit of 50k urls, new sitemaps will have a numeric suffix to the name. Example:
//- blog_1.xml.gz
//- blog_2.xml.gz
func NewIndexGroup(folder string, name string) *IndexGroup {
s := new(IndexGroup)
s.Configure(name, folder)
go s.Initialize()
return s
}
//Save and gzip xml
func saveXml(xmlFile []byte, path string) (err error) {
fo, err := os.Create(path)
if err != nil {
return err
}
defer fo.Close()
zip := gzip.NewWriter(fo)
defer zip.Close()
_, err = zip.Write(xmlFile)
if err != nil {
return err
}
return err
}
//Search all the xml.gz sitemaps_dir directory, uses the modified date of the file as lastModified
//path_index is included for the function does not include the url of the index in your own content, if it is present in the same directory.
func CreateIndexByScanDir(targetDir string, indexFileName string, public_url string) (index Index) {
index = Index{Sitemaps: []Sitemap{}}
fs, err := ioutil.ReadDir(targetDir)
if err != nil {
return
}
for _, f := range fs {
if strings.HasSuffix(f.Name(), ".xml.gz") && !strings.HasSuffix(indexFileName, f.Name()) {
lastModified := f.ModTime()
index.Sitemaps = append(index.Sitemaps, Sitemap{Loc: public_url + f.Name(), LastMod: &lastModified})
}
}
return
}
//Returns an index sitemap starting from a slice of urls
func CreateIndexBySlice(urls []string, public_url string) (index Index) {
index = Index{Sitemaps: []Sitemap{}}
if len(urls) > 0 {
for _, fileName := range urls {
lastModified := time.Now()
index.Sitemaps = append(index.Sitemaps, Sitemap{Loc: public_url + fileName, LastMod: &lastModified})
}
}
return
}
//Creates and gzip the xml index
func CreateSitemapIndex(indexFilePath string, index Index) (err error) {
//create xml
indexXml, err := createSitemapIndexXml(index)
if err != nil {
return err
}
err = saveXml(indexXml, indexFilePath)
log.Println("SITEMAP - Sitemap Index created on", indexFilePath)
return err
}
//Sends a ping to search engines indicating that the index has been updated.
//Currently supports Google and Bing.
func PingSearchEngines(indexFile string) {
var urls = []string{
"http://www.google.com/webmasters/tools/ping?sitemap=" + indexFile,
"http://www.bing.com/ping?sitemap=" + indexFile,
}
results := asyncHttpGets(urls)
for result := range results {
log.Printf("SITEMAP - %s status: %s\n", result.url, result.response.Status)
}
}
type HttpResponse struct {
url string
response *http.Response
err error
}
func asyncHttpGets(urls []string) chan HttpResponse {
ch := make(chan HttpResponse)
go func() {
var wg sync.WaitGroup
for _, url := range urls {
wg.Add(1)
go func(url string) {
resp, err := http.Get(url)
if err != nil {
log.Println("SITEMAP - error", resp, err)
wg.Done()
return
}
resp.Body.Close()
ch <- HttpResponse{url, resp, err}
wg.Done()
}(url)
}
wg.Wait()
close(ch)
}()
return ch
}