-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
212 lines (179 loc) · 5.12 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
package main
import (
"encoding/xml"
"fmt"
"gopkg.in/yaml.v2"
"html/template"
"log"
"net/http"
"os"
"sort"
"sync"
"io"
"bytes"
"time"
)
type Config struct {
Channels map[string]string `yaml:"channels"`
}
type Feed struct {
Entries []Entry `xml:"entry"`
}
type Entry struct {
Title string `xml:"title"`
Link Link `xml:"link"`
Published string `xml:"published"`
PubDate time.Time `xml:"-"`
Author string `xml:"author>name"`
Thumbnail string `xml:"-"`
Views string `xml:"-"`
MediaGroup MediaGroup `xml:"group"`
}
type Link struct {
Href string `xml:"href,attr"`
}
type MediaGroup struct {
Thumbnail Thumbnail `xml:"thumbnail"`
MediaCommunity MediaCommunity `xml:"community"`
}
type Thumbnail struct {
URL string `xml:"url,attr"`
}
type MediaCommunity struct {
Statistics Statistics `xml:"statistics"`
}
type Statistics struct {
Views string `xml:"views,attr"`
}
var (
allEntries = []Entry{}
feedsMutex sync.RWMutex
tmpl *template.Template
)
func main() {
cfg, err := loadConfig("config.yaml")
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
tmpl = template.Must(template.New("feed.html").Funcs(template.FuncMap{
"timeAgo": timeAgo,
"viewCount": viewCount,
}).ParseFiles("templates/feed.html"))
go updateFeeds(cfg, 5*time.Minute)
http.HandleFunc("/", feedHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
log.Println("Server started at http://localhost:8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("Server error: %v", err)
}
}
func loadConfig(path string) (Config, error) {
file, err := os.Open(path)
if err != nil {
return Config{}, err
}
defer file.Close()
var cfg Config
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&cfg); err != nil {
return Config{}, err
}
return cfg, nil
}
func fetchFeed(url string) ([]Entry, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body = io.NopCloser(bytes.NewReader(body))
var feed Feed
if err := xml.NewDecoder(resp.Body).Decode(&feed); err != nil {
return nil, err
}
for i := range feed.Entries {
feed.Entries[i].PubDate, _ = time.Parse(time.RFC3339, feed.Entries[i].Published)
feed.Entries[i].Thumbnail = feed.Entries[i].MediaGroup.Thumbnail.URL
feed.Entries[i].Views = feed.Entries[i].MediaGroup.MediaCommunity.Statistics.Views
}
return feed.Entries, nil
}
func updateFeeds(cfg Config, interval time.Duration) {
for {
var newEntries []Entry
for _, url := range cfg.Channels {
entries, err := fetchFeed("https://www.youtube.com/feeds/videos.xml?channel_id=" + url)
if err != nil {
continue
}
newEntries = append(newEntries, entries...)
}
sort.Slice(newEntries, func(i, j int) bool {
return newEntries[i].PubDate.After(newEntries[j].PubDate)
})
feedsMutex.Lock()
allEntries = newEntries
feedsMutex.Unlock()
time.Sleep(interval)
}
}
func feedHandler(w http.ResponseWriter, r *http.Request) {
feedsMutex.RLock()
defer feedsMutex.RUnlock()
if _, err := r.Cookie("visited"); err != nil {
http.SetCookie(w, &http.Cookie{
Name: "visited",
Value: "true",
Path: "/",
MaxAge: 3600 * 24 * 365,
})
}
if err := tmpl.Execute(w, allEntries); err != nil {
log.Printf("Template execution error: %v", err)
http.Error(w, "Error rendering template", http.StatusInternalServerError)
return
}
}
func timeAgo(t time.Time) string {
diff := time.Since(t)
switch {
case diff < time.Minute:
return "just now"
case diff < time.Hour:
if diff.Minutes() == 1 {
return "1 minute ago"
}
return fmt.Sprintf("%d minutes ago", int(diff.Minutes()))
case diff < 24*time.Hour:
if diff.Hours() == 1 {
return "1 hour ago"
}
return fmt.Sprintf("%d hours ago", int(diff.Hours()))
default:
days := int(diff.Hours() / 24)
if days == 1 {
return "1 day ago"
}
return fmt.Sprintf("%d days ago", days)
}
}
func viewCount(views string) string {
viewsInt := 0
fmt.Sscanf(views, "%d", &viewsInt)
switch {
case viewsInt < 1000:
return fmt.Sprintf("%d views", viewsInt)
case viewsInt < 10000:
return fmt.Sprintf("%.1fK views", float64(viewsInt)/1000)
case viewsInt < 1000000:
return fmt.Sprintf("%.0fK views", float64(viewsInt)/1000)
case viewsInt < 10000000:
return fmt.Sprintf("%.1fM views", float64(viewsInt)/1000000)
default:
return fmt.Sprintf("%.0fM views", float64(viewsInt)/1000000)
}
}