-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsitemap.go
93 lines (75 loc) · 1.9 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
package main
import (
"fmt"
"github.com/ikeikeikeike/go-sitemap-generator/stm"
log "github.com/sirupsen/logrus"
"net/http"
"os"
)
func (app *Application) Sitemap(w http.ResponseWriter, _ *http.Request) {
sm, err := app.generateSitemap()
if err != nil {
http.Error(w, "failed to generate sitemap", http.StatusInternalServerError)
return
}
if _, err := w.Write(sm.XMLContent()); err != nil {
http.Error(w, "failed to write sitemap", http.StatusInternalServerError)
log.WithFields(log.Fields{"error": err}).Error("failed to write sitemap")
}
}
func (app *Application) generateSitemap() (*stm.Sitemap, error) {
sm := stm.NewSitemap()
sm.SetDefaultHost(os.Getenv("BASE_URL"))
sm.Create()
sm.Add(stm.URL{
"loc": "/",
"changefreq": "hourly",
"priority": "0.5",
})
sm.Add(stm.URL{
"loc": "/queue",
"changefreq": "hourly",
"priority": "0.5",
})
sm.Add(stm.URL{
"loc": "/history",
"changefreq": "hourly",
"priority": "0.5",
})
if err := app.addVideosToSitemap(sm); err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to add videos to sitemap")
return nil, err
}
return sm, nil
}
func (app *Application) addVideosToSitemap(sm *stm.Sitemap) error {
videos, err := AllVideos(app.db)
if err != nil {
return err
}
for _, video := range videos {
// thumbnail url
sm.Add(stm.URL{
"loc": fmt.Sprintf("/api/download/%s/thumbnail", video.Id),
"changefreq": "yearly",
"priority": "0.2",
})
// video and ffmpeg log is only available after the video is finished
if !video.Finished {
continue
}
// video download url
sm.Add(stm.URL{
"loc": fmt.Sprintf("/archive/%s", video.Id),
"changefreq": "yearly",
"priority": "0.3",
})
// ffmpeg url
sm.Add(stm.URL{
"loc": fmt.Sprintf("/api/download/%s/ffmpeg", video.Id),
"changefreq": "yearly",
"priority": "0.1",
})
}
return nil
}