-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecwidmap.go
87 lines (75 loc) · 2.38 KB
/
ecwidmap.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
package main
import (
"context"
"flag"
"fmt"
"html/template"
"os"
"strconv"
"time"
"github.com/sevkin/go-ecwid"
)
const sitemap = `{{"<?"|html}}xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>{{root|urlquote}}</loc></url>
{{$daily := .Daily}}{{range .Products}}<url><loc>{{.URL|urlquote}}</loc><lastmod>{{.UpdateTimestamp|lastmod}}</lastmod>{{if $daily}}<changefreq>daily</changefreq>{{end}}</url>
{{end}}{{range .Categories}}<url><loc>{{.URL|urlquote}}</loc>{{if $daily}}<changefreq>daily</changefreq>{{end}}</url>
{{end}}</urlset>`
func main() {
const (
storeIDEnv = "ECWID_STOREID"
tokenEnv = "ECWID_TOKEN"
)
var (
storeID uint64
token string
daily bool
)
// сначала попробовать получить из переменных окружения
storeID, _ = strconv.ParseUint(os.Getenv(storeIDEnv), 10, 64)
token = os.Getenv(tokenEnv)
// затем из командной строки
flag.Uint64Var(&storeID, "storeid", storeID, fmt.Sprintf("store ID (can get from %s env)", storeIDEnv))
flag.StringVar(&token, "token", token, fmt.Sprintf("token (can get from %s env)", tokenEnv))
flag.BoolVar(&daily, "daily", daily, "force <changefreq>daily</changefreq>")
flag.Parse()
// обязательные значения storeID, token, filename
if storeID == 0 || token == "" {
fmt.Println("pass storeid and token via env or commandline")
os.Exit(2)
}
store := ecwid.New(ecwid.ID(storeID), token)
//store.SetDebug(true)
template.Must(template.New("sitemap").
Funcs(template.FuncMap{
"html": func(s string) template.HTML { return template.HTML(s) },
"urlquote": func(s string) string {
return s
},
"lastmod": func(u uint64) string {
return time.Unix(int64(u), 0).Format("2006-01-02")
},
"root": func() (string, error) {
profile, err := store.StoreProfileGet()
if err != nil {
return "", err
}
return profile.GeneralInfo.StoreURL, nil
},
}).
Parse(sitemap)).
Execute(os.Stdout, struct {
Daily bool
Products <-chan (*ecwid.Product)
Categories <-chan (*ecwid.Category)
}{
Daily: daily,
Products: store.Products(context.Background(), map[string]string{
"enabled": "true",
"cleanUrls": "true",
}),
Categories: store.Categories(context.Background(), map[string]string{
"cleanUrls": "true",
}),
})
}