|
| 1 | +package sitemap |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// Index is a structure of <sitemapindex> |
| 13 | +type Index struct { |
| 14 | + XMLName xml.Name `xml:"sitemapindex"` |
| 15 | + Sitemap []parts `xml:"sitemap"` |
| 16 | +} |
| 17 | + |
| 18 | +// parts is a structure of <sitemap> in <sitemapindex> |
| 19 | +type parts struct { |
| 20 | + Loc string `xml:"loc"` |
| 21 | + LastMod string `xml:"lastmod"` |
| 22 | +} |
| 23 | + |
| 24 | +// Sitemap is a structure of <sitemap> |
| 25 | +type Sitemap struct { |
| 26 | + // Xsi string `xml:"xsi,attr"` |
| 27 | + // Image string `xml:"image,attr"` |
| 28 | + // SchemaLocation string `xml:"schemaLocation,attr"` |
| 29 | + // Xmlns string `xml:"xmlns,attr"` |
| 30 | + XMLName xml.Name `xml:"urlset"` |
| 31 | + URL []URL `xml:"url"` |
| 32 | +} |
| 33 | + |
| 34 | +// URL is a structure of <url> in <sitemap> |
| 35 | +type URL struct { |
| 36 | + Loc string `xml:"loc"` |
| 37 | + LastMod string `xml:"lastmod,omitempty"` |
| 38 | + ChangeFreq string `xml:"changefreq,omitempty"` |
| 39 | + Priority float32 `xml:"priority,omitempty"` |
| 40 | + // Image []Image `xml:"image,omitempty"` |
| 41 | +} |
| 42 | + |
| 43 | +// Image is a structure of <image> in <url> |
| 44 | +type Image struct { |
| 45 | + Loc string `xml:"loc,omitempty"` |
| 46 | + Title string `xml:"title,omitempty"` |
| 47 | + Caption string `xml:"caption,omitempty"` |
| 48 | + GeoLoc string `xml:"geolocation,omitempty"` |
| 49 | + License string `xml:"license,omitempty"` |
| 50 | +} |
| 51 | + |
| 52 | +var ( |
| 53 | + // fetch is page acquisition function |
| 54 | + fetch = func(URL string, options interface{}) ([]byte, error) { |
| 55 | + var body []byte |
| 56 | + |
| 57 | + res, err := http.Get(URL) |
| 58 | + if err != nil { |
| 59 | + return body, err |
| 60 | + } |
| 61 | + defer res.Body.Close() |
| 62 | + |
| 63 | + return io.ReadAll(res.Body) |
| 64 | + } |
| 65 | + |
| 66 | + // Time interval to be used in Index.get |
| 67 | + interval = time.Second |
| 68 | +) |
| 69 | + |
| 70 | +/* |
| 71 | +Get is fetch and parse sitemap.xml/sitemapindex.xml |
| 72 | +
|
| 73 | +If sitemap.xml or sitemapindex.xml has some problems, This function return error. |
| 74 | +
|
| 75 | +・When sitemap.xml/sitemapindex.xml could not retrieved. |
| 76 | +・When sitemap.xml/sitemapindex.xml is empty. |
| 77 | +・When sitemap.xml/sitemapindex.xml has format problems. |
| 78 | +・When sitemapindex.xml contains a sitemap.xml URL that cannot be retrieved. |
| 79 | +・When sitemapindex.xml contains a sitemap.xml that is empty |
| 80 | +・When sitemapindex.xml contains a sitemap.xml that has format problems. |
| 81 | +
|
| 82 | +If you want to ignore these errors, use the ForceGet function. |
| 83 | +*/ |
| 84 | +func Get(URL string, options interface{}) (Sitemap, error) { |
| 85 | + data, err := fetch(URL, options) |
| 86 | + if err != nil { |
| 87 | + return Sitemap{}, err |
| 88 | + } |
| 89 | + |
| 90 | + idx, idxErr := ParseIndex(data) |
| 91 | + smap, smapErr := Parse(data) |
| 92 | + |
| 93 | + if idxErr != nil && smapErr != nil { |
| 94 | + if idxErr != nil { |
| 95 | + err = idxErr |
| 96 | + } else { |
| 97 | + err = smapErr |
| 98 | + } |
| 99 | + return Sitemap{}, fmt.Errorf("URL is not a sitemap or sitemapindex: %v", err) |
| 100 | + } else if idxErr != nil { |
| 101 | + return smap, nil |
| 102 | + } |
| 103 | + |
| 104 | + smap, err = idx.get(options, false) |
| 105 | + if err != nil { |
| 106 | + return Sitemap{}, err |
| 107 | + } |
| 108 | + |
| 109 | + return smap, nil |
| 110 | +} |
| 111 | + |
| 112 | +/* |
| 113 | +ForceGet is fetch and parse sitemap.xml/sitemapindex.xml. |
| 114 | +The difference with the Get function is that it ignores some errors. |
| 115 | +
|
| 116 | +Errors to Ignore: |
| 117 | +
|
| 118 | +・When sitemapindex.xml contains a sitemap.xml URL that cannot be retrieved. |
| 119 | +・When sitemapindex.xml contains a sitemap.xml that is empty |
| 120 | +・When sitemapindex.xml contains a sitemap.xml that has format problems. |
| 121 | +
|
| 122 | +Errors not to Ignore: |
| 123 | +
|
| 124 | +・When sitemap.xml/sitemapindex.xml could not retrieved. |
| 125 | +・When sitemap.xml/sitemapindex.xml is empty. |
| 126 | +・When sitemap.xml/sitemapindex.xml has format problems. |
| 127 | +
|
| 128 | +If you want **not** to ignore some errors, use the Get function. |
| 129 | +*/ |
| 130 | +func ForceGet(URL string, options interface{}) (Sitemap, error) { |
| 131 | + data, err := fetch(URL, options) |
| 132 | + if err != nil { |
| 133 | + return Sitemap{}, err |
| 134 | + } |
| 135 | + |
| 136 | + idx, idxErr := ParseIndex(data) |
| 137 | + smap, smapErr := Parse(data) |
| 138 | + |
| 139 | + if idxErr != nil && smapErr != nil { |
| 140 | + if idxErr != nil { |
| 141 | + err = idxErr |
| 142 | + } else { |
| 143 | + err = smapErr |
| 144 | + } |
| 145 | + return Sitemap{}, fmt.Errorf("URL is not a sitemap or sitemapindex: %v", err) |
| 146 | + } else if idxErr != nil { |
| 147 | + return smap, nil |
| 148 | + } |
| 149 | + |
| 150 | + smap, err = idx.get(options, true) |
| 151 | + if err != nil { |
| 152 | + return Sitemap{}, err |
| 153 | + } |
| 154 | + |
| 155 | + return smap, nil |
| 156 | +} |
| 157 | + |
| 158 | +// Get Sitemap data from sitemapindex file |
| 159 | +func (idx *Index) get(options interface{}, ignoreErr bool) (Sitemap, error) { |
| 160 | + var smap Sitemap |
| 161 | + |
| 162 | + for _, s := range idx.Sitemap { |
| 163 | + time.Sleep(interval) |
| 164 | + data, err := fetch(s.Loc, options) |
| 165 | + if !ignoreErr && err != nil { |
| 166 | + return smap, fmt.Errorf("failed to retrieve %s in sitemapindex.xml: %v", s.Loc, err) |
| 167 | + } |
| 168 | + |
| 169 | + err = xml.Unmarshal(data, &smap) |
| 170 | + if !ignoreErr && err != nil { |
| 171 | + return smap, fmt.Errorf("failed to parse %s in sitemapindex.xml: %v", s.Loc, err) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return smap, nil |
| 176 | +} |
| 177 | + |
| 178 | +// Parse create Sitemap data from text |
| 179 | +func Parse(data []byte) (Sitemap, error) { |
| 180 | + var smap Sitemap |
| 181 | + if len(data) == 0 { |
| 182 | + return smap, fmt.Errorf("sitemap.xml is empty") |
| 183 | + } |
| 184 | + |
| 185 | + err := xml.Unmarshal(data, &smap) |
| 186 | + return smap, err |
| 187 | +} |
| 188 | + |
| 189 | +// ParseIndex create Index data from text |
| 190 | +func ParseIndex(data []byte) (Index, error) { |
| 191 | + var idx Index |
| 192 | + if len(data) == 0 { |
| 193 | + return idx, fmt.Errorf("sitemapindex.xml is empty") |
| 194 | + } |
| 195 | + |
| 196 | + err := xml.Unmarshal(data, &idx) |
| 197 | + return idx, err |
| 198 | +} |
| 199 | + |
| 200 | +// SetInterval change Time interval to be used in Index.get |
| 201 | +func SetInterval(time time.Duration) { |
| 202 | + interval = time |
| 203 | +} |
| 204 | + |
| 205 | +// SetFetch change fetch closure |
| 206 | +func SetFetch(f func(URL string, options interface{}) ([]byte, error)) { |
| 207 | + fetch = f |
| 208 | +} |
| 209 | + |
| 210 | +// Print shows the sitemap from Sitemap struct |
| 211 | +func (smap *Sitemap) Print() ([]byte, error) { |
| 212 | + return xml.MarshalIndent(smap, "", " ") |
| 213 | +} |
| 214 | + |
| 215 | +// Save creates the sitemap from Sitemap struct and save it to file |
| 216 | +func (smap *Sitemap) Save(dir, file string) error { |
| 217 | + data, err := smap.Print() |
| 218 | + if err != nil { |
| 219 | + return err |
| 220 | + } |
| 221 | + |
| 222 | + // Add the xml header |
| 223 | + data = append([]byte(xml.Header), data...) |
| 224 | + |
| 225 | + // Create directory if it does not exist |
| 226 | + if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 227 | + os.Mkdir(dir, 0755) |
| 228 | + } |
| 229 | + |
| 230 | + _, err = os.Create(fmt.Sprintf("%s/%s", dir, file)) |
| 231 | + if err != nil { |
| 232 | + return err |
| 233 | + } |
| 234 | + |
| 235 | + err = os.WriteFile(fmt.Sprintf("%s/%s", dir, file), data, 0644) |
| 236 | + if err != nil { |
| 237 | + return err |
| 238 | + } |
| 239 | + |
| 240 | + return nil |
| 241 | +} |
0 commit comments