-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
213 lines (163 loc) · 4.45 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"embed"
"errors"
"fmt"
"io/fs"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/eduncan911/podcast"
"github.com/gorilla/feeds"
"github.com/maetthu/republik-feeder/lib/client"
"github.com/patrickmn/go-cache"
)
const baseURL = "https://www.republik.ch"
const articleLimitDefault = 20
//go:embed all:assets
var assets embed.FS
var sizeCache *cache.Cache
func getSize(URL string) (int64, error) {
if size, found := sizeCache.Get(URL); found {
return size.(int64), nil
}
r, err := http.Head(URL)
if err != nil {
return 0, err
}
sizeCache.Set(URL, r.ContentLength, cache.DefaultExpiration)
return r.ContentLength, nil
}
func buildURL(r *http.Request, path string) (*url.URL, error) {
if env := os.Getenv("REPUBLIK_FEEDER_URL"); env != "" {
u, err := url.Parse(env)
if err != nil {
return nil, err
}
u.Path = u.Path + path
return u, nil
}
if path == "" {
path = "/"
}
u := url.URL{
Path: path,
Host: r.Host,
Scheme: "http",
}
return &u, nil
}
func articlesHandler(w http.ResponseWriter, r *http.Request) {
feed := &feeds.Feed{
Title: "Republik - RSS Feed",
Link: &feeds.Link{Href: baseURL},
Description: "Republik - RSS Feed",
Author: &feeds.Author{Name: "Republik", Email: "[email protected]"},
Created: time.Now(),
}
articleLimit := articleLimitDefault
if i, err := strconv.Atoi(os.Getenv("REPUBLIK_FEEDER_ARTICLE_LIMIT")); err == nil && i > 0 {
articleLimit = i
}
c := client.NewClient(os.Getenv("REPUBLIK_FEEDER_COOKIE"))
docs, err := c.Fetch(client.Filter{Feed: true}, articleLimit)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, d := range docs {
title := d.Meta.Title
if d.Meta.Format.Meta.Title != "" {
title = fmt.Sprintf("[%s] %s", d.Meta.Format.Meta.Title, d.Meta.Title)
}
feed.Add(&feeds.Item{
Id: d.ID,
Title: title,
Link: &feeds.Link{Href: baseURL + d.Meta.Path},
Description: d.Meta.Description,
Created: d.PubDate(),
})
}
rss, err := feed.ToRss()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, _ = w.Write([]byte(rss))
}
func podcastHandler(w http.ResponseWriter, r *http.Request) {
date := time.Now()
feed := podcast.New(
"Republik - Podcast Feed",
baseURL,
"Republik - Podcast Feed",
&date,
&date,
)
feed.Language = "de-CH"
img, _ := buildURL(r, "/assets/cover.png")
feed.AddImage(img.String())
articleLimit := articleLimitDefault
if i, err := strconv.Atoi(os.Getenv("REPUBLIK_FEEDER_ARTICLE_LIMIT")); err == nil && i > 0 {
articleLimit = i
}
c := client.NewClient(os.Getenv("REPUBLIK_FEEDER_COOKIE"))
docs, err := c.Fetch(client.Filter{Feed: true, HasAudio: true, AudioSourceKind: "readAloud"}, articleLimit)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, d := range docs {
title := d.Meta.Title
if d.Meta.Format.Meta.Title != "" {
title = fmt.Sprintf("[%s] %s", d.Meta.Format.Meta.Title, d.Meta.Title)
}
pubdate := d.PubDate()
item := podcast.Item{
Title: title,
Link: baseURL + d.Meta.Path,
Description: d.Meta.Description + "\n \n \n" + baseURL + d.Meta.Path,
PubDate: &pubdate,
IDuration: strconv.Itoa(int(d.Meta.AudioSource.DurationMs / 1000)),
}
// fetch file size
if size, err := getSize(d.Meta.AudioSource.MP3); err == nil {
item.AddEnclosure(d.Meta.AudioSource.MP3, podcast.MP3, size)
_, _ = feed.AddItem(item)
}
}
if err := feed.Encode(w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func assetHandler(path string) http.Handler {
fsys, err := fs.Sub(assets, "assets")
if err != nil {
log.Fatal(err)
}
filesystem := http.FS(fsys)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, path)
_, err := filesystem.Open(path)
if errors.Is(err, os.ErrNotExist) {
path = fmt.Sprintf("%s.html", path)
}
r.URL.Path = path
http.FileServer(filesystem).ServeHTTP(w, r)
})
}
func main() {
if len(os.Args) != 2 {
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s <listen-address>\n", os.Args[0])
os.Exit(1)
}
sizeCache = cache.New(24*time.Hour, 24*time.Hour)
http.HandleFunc("/articles", articlesHandler)
http.HandleFunc("/podcast", podcastHandler)
http.Handle("/assets/", assetHandler("/assets"))
log.Fatal(http.ListenAndServe(os.Args[1], nil))
}