generated from midzer/go-cli-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
83 lines (68 loc) · 1.56 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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/bored-engineer/sitemap"
"github.com/gorilla/feeds"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func writeFile(fname string, c func() (string, error)) error {
f, err := os.Create(fname)
if err != nil {
return fmt.Errorf("can't create %s: %w", fname, err)
}
defer f.Close()
s, err := c()
if err != nil {
return fmt.Errorf("can't convert feed: %w", err)
}
_, err = f.WriteString(s)
if err != nil {
return fmt.Errorf("can't write to %s: %w", fname, err)
}
return nil
}
func run() error {
urls, err := sitemap.Fetch(context.Background(), "https://sitemaps.org/sitemap.xml")
if err != nil {
return fmt.Errorf("can't fetch sitemap: %w", err)
}
feed := &feeds.Feed{
Title: "Sitemaps",
Link: &feeds.Link{Href: "https://sitemaps.org/"},
}
for _, url := range urls {
log.Println(url.LastModification, url.Location)
updated, err := time.Parse("2006-01-02", url.LastModification.String())
if err != nil {
return fmt.Errorf("can't parse last modification date: %w", err)
}
item := &feeds.Item{
Id: url.Location,
Link: &feeds.Link{Href: url.Location},
Updated: updated,
}
feed.Add(item)
}
// write atom.xml
if err := writeFile("atom.xml", feed.ToAtom); err != nil {
return err
}
// write rss.xml
if err := writeFile("rss.xml", feed.ToRss); err != nil {
return err
}
// write feed.json
if err := writeFile("feed.json", feed.ToJSON); err != nil {
return err
}
return nil
}