-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
652 lines (558 loc) · 15.5 KB
/
Copy pathmain.go
File metadata and controls
652 lines (558 loc) · 15.5 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/xml"
"fmt"
"html/template"
"io"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"gopkg.in/yaml.v2"
)
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"`
}
type FeedStatus struct {
TotalChannels int
SuccessCount int
FailedChannels []string
LastUpdate time.Time
Updating bool
}
type PageData struct {
Entries []Entry
Status FeedStatus
ConvertURL string
ConvertResult string
ConvertError string
}
var (
allEntries = []Entry{}
feedsMutex sync.RWMutex
feedStatus FeedStatus
statusMutex sync.RWMutex
channelCaches = make(map[string][]Entry)
channelMutex sync.RWMutex
tmpl *template.Template
urlCache = make(map[string]string)
cacheMutex sync.RWMutex
httpClient = newHTTPClient()
port = envOrDefault("PORT", "8080")
videoURLBase = strings.TrimRight(os.Getenv("VIDEO_URL"), "/")
channelIDPattern = regexp.MustCompile(`youtube\.com/channel/(UC[a-zA-Z0-9_-]+)`)
feedUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
)
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,
"join": strings.Join,
}).ParseFiles("templates/feed.html"))
refreshInterval := envDurationOrDefault("REFRESH_INTERVAL", 15*time.Minute)
refreshFeeds(cfg)
if videoURLBase != "" {
log.Printf("Video links will use %s", videoURLBase)
}
log.Printf("Feed refresh interval: %v", refreshInterval)
go func() {
ticker := time.NewTicker(refreshInterval)
defer ticker.Stop()
for range ticker.C {
refreshFeeds(cfg)
}
}()
http.HandleFunc("/", feedHandler)
http.HandleFunc("/proxy/", proxyHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
log.Printf("Server started at http://localhost:%s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatalf("Server error: %v", err)
}
}
func envOrDefault(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
func newHTTPClient() *http.Client {
timeout := envDurationOrDefault("HTTP_TIMEOUT", 30*time.Second)
return &http.Client{
Timeout: timeout,
Transport: &http.Transport{
MaxIdleConns: 32,
MaxIdleConnsPerHost: 8,
IdleConnTimeout: 90 * time.Second,
},
}
}
func drainBody(resp *http.Response) {
if resp == nil || resp.Body == nil {
return
}
_, _ = io.Copy(io.Discard, resp.Body)
}
func shouldRetry(statusCode int) bool {
switch statusCode {
case http.StatusTooManyRequests,
http.StatusInternalServerError,
http.StatusBadGateway,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout:
return true
default:
return statusCode == 0
}
}
func feedFetchError(statusCode int, err error) error {
if err == nil {
return nil
}
switch statusCode {
case http.StatusNotFound:
return fmt.Errorf("%w (YouTube RSS feed unavailable — known intermittent outage, not a bad channel ID)", err)
case http.StatusTooManyRequests:
return fmt.Errorf("%w (YouTube rate limit — try a longer REFRESH_INTERVAL)", err)
default:
return err
}
}
func retryDelay(statusCode int, attempt int, retryAfter string) time.Duration {
if statusCode == http.StatusTooManyRequests || statusCode == http.StatusServiceUnavailable {
if secs, err := strconv.Atoi(strings.TrimSpace(retryAfter)); err == nil && secs > 0 {
return time.Duration(secs) * time.Second
}
return time.Duration(30+attempt*15) * time.Second
}
base := time.Duration(1<<uint(attempt)) * time.Second
jitter := time.Duration(rand.Intn(500)) * time.Millisecond
return base + jitter
}
func channelFetchDelay(channelCount int) time.Duration {
if value := os.Getenv("CHANNEL_FETCH_DELAY"); value != "" {
d, err := time.ParseDuration(value)
if err != nil {
log.Fatalf("Invalid CHANNEL_FETCH_DELAY: %v", err)
}
return d
}
if channelCount > 10 {
return 2 * time.Second
}
return 500 * time.Millisecond
}
func envDurationOrDefault(key string, fallback time.Duration) time.Duration {
value := os.Getenv(key)
if value == "" {
return fallback
}
d, err := time.ParseDuration(value)
if err != nil {
log.Fatalf("Invalid %s: %v", key, err)
}
return d
}
func rewriteWatchURL(href string) string {
if videoURLBase == "" {
return href
}
videoID := extractVideoID(href)
if videoID == "" {
return href
}
return videoURLBase + "/watch?v=" + videoID
}
func extractVideoID(href string) string {
parsed, err := url.Parse(href)
if err != nil {
return ""
}
if id := parsed.Query().Get("v"); id != "" {
return id
}
parts := strings.Split(strings.Trim(parsed.Path, "/"), "/")
if len(parts) == 2 && parts[0] == "shorts" {
return parts[1]
}
return ""
}
func resolveChannelID(rawURL string) (string, error) {
rawURL = strings.TrimSpace(rawURL)
if rawURL == "" {
return "", fmt.Errorf("enter a channel URL")
}
if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") {
rawURL = "https://" + rawURL
}
parsed, err := url.Parse(rawURL)
if err != nil {
return "", fmt.Errorf("invalid URL")
}
parts := strings.Split(strings.Trim(parsed.Path, "/"), "/")
if len(parts) >= 2 && parts[0] == "channel" && strings.HasPrefix(parts[1], "UC") {
return parts[1], nil
}
req, err := http.NewRequest(http.MethodGet, parsed.String(), nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", "Mozilla/5.0")
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
drainBody(resp)
return "", fmt.Errorf("could not fetch channel page")
}
body, err := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1024))
if err != nil {
return "", err
}
match := channelIDPattern.FindSubmatch(body)
if len(match) < 2 {
return "", fmt.Errorf("could not find channel ID")
}
return string(match[1]), nil
}
func configLabel(channelURL, channelID string) string {
parsed, err := url.Parse(channelURL)
if err != nil {
return "ChannelName"
}
parts := strings.Split(strings.Trim(parsed.Path, "/"), "/")
if len(parts) == 0 {
return "ChannelName"
}
label := parts[len(parts)-1]
label = strings.TrimPrefix(label, "@")
if label == "" || label == channelID {
return "ChannelName"
}
return label
}
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) (entries []Entry, statusCode int, retryAfter string, err error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, 0, "", err
}
req.Header.Set("User-Agent", feedUserAgent)
req.Header.Set("Accept", "application/atom+xml,application/xml;q=0.9,*/*;q=0.8")
resp, err := httpClient.Do(req)
if err != nil {
return nil, 0, "", err
}
defer resp.Body.Close()
statusCode = resp.StatusCode
retryAfter = resp.Header.Get("Retry-After")
if resp.StatusCode != http.StatusOK {
drainBody(resp)
return nil, statusCode, retryAfter, fmt.Errorf("unexpected status %d", resp.StatusCode)
}
body, err := io.ReadAll(io.LimitReader(resp.Body, 4*1024*1024))
if err != nil {
return nil, statusCode, retryAfter, err
}
var feed Feed
if err := xml.NewDecoder(bytes.NewReader(body)).Decode(&feed); err != nil {
return nil, statusCode, retryAfter, err
}
for i := range feed.Entries {
feed.Entries[i].PubDate, _ = time.Parse(time.RFC3339, feed.Entries[i].Published)
feed.Entries[i].Link.Href = rewriteWatchURL(feed.Entries[i].Link.Href)
originalURL := feed.Entries[i].MediaGroup.Thumbnail.URL
feed.Entries[i].Thumbnail = cacheURL(originalURL)
feed.Entries[i].Views = feed.Entries[i].MediaGroup.MediaCommunity.Statistics.Views
}
return feed.Entries, statusCode, retryAfter, nil
}
func fetchFeedWithRetry(url string, attempts int) (entries []Entry, statusCode int, err error) {
var lastErr error
var lastStatus int
for attempt := 0; attempt < attempts; attempt++ {
entries, statusCode, retryAfter, err := fetchFeed(url)
if err == nil {
return entries, statusCode, nil
}
lastErr = err
lastStatus = statusCode
log.Printf("Fetch attempt %d/%d failed for %s: %v", attempt+1, attempts, url, err)
if !shouldRetry(statusCode) {
break
}
if attempt < attempts-1 {
delay := retryDelay(statusCode, attempt, retryAfter)
log.Printf("Retrying in %v", delay)
time.Sleep(delay)
}
}
return nil, lastStatus, feedFetchError(lastStatus, lastErr)
}
func refreshFeeds(cfg Config) {
setUpdating(true)
defer setUpdating(false)
total := len(cfg.Channels)
successCount := 0
notFoundCount := 0
failedChannels := make([]string, 0)
fetchDelay := channelFetchDelay(total)
names := make([]string, 0, total)
for name := range cfg.Channels {
names = append(names, name)
}
sort.Strings(names)
for i, name := range names {
if i > 0 {
time.Sleep(fetchDelay)
}
channelID := cfg.Channels[name]
feedURL := "https://www.youtube.com/feeds/videos.xml?channel_id=" + channelID
if v := strings.ToLower(os.Getenv("HIDE_SHORTS")); v != "false" && v != "0" && v != "no" && v != "off" {
if strings.HasPrefix(channelID, "UC") {
feedURL = "https://www.youtube.com/feeds/videos.xml?playlist_id=UULF" + channelID[2:]
}
}
log.Printf("Fetching channel %s...", name)
entries, status, err := fetchFeedWithRetry(feedURL, 5)
if err != nil {
log.Printf("Error fetching channel %s: %v", name, err)
if status == http.StatusNotFound {
notFoundCount++
}
failedChannels = append(failedChannels, name)
channelMutex.RLock()
_, hasCache := channelCaches[channelID]
channelMutex.RUnlock()
if hasCache {
log.Printf("Keeping cached entries for channel %s", name)
}
continue
}
log.Printf("Fetched %d entries for channel %s", len(entries), name)
successCount++
channelMutex.Lock()
channelCaches[channelID] = entries
channelMutex.Unlock()
}
mergeEntries()
statusMutex.Lock()
feedStatus = FeedStatus{
TotalChannels: total,
SuccessCount: successCount,
FailedChannels: failedChannels,
LastUpdate: time.Now(),
}
statusMutex.Unlock()
log.Printf("Feed update complete: %d/%d channels, %d videos", successCount, total, len(getEntries()))
if notFoundCount > 0 && notFoundCount == len(failedChannels) {
log.Printf("All failures were HTTP 404 — YouTube's public RSS endpoint is likely down right now (intermittent, not your config); cached videos are kept until it recovers")
}
}
func mergeEntries() {
channelMutex.RLock()
var merged []Entry
for _, entries := range channelCaches {
merged = append(merged, entries...)
}
channelMutex.RUnlock()
twoYearsAgo := time.Now().AddDate(-2, 0, 0)
filtered := make([]Entry, 0, len(merged))
for _, entry := range merged {
if entry.PubDate.After(twoYearsAgo) {
filtered = append(filtered, entry)
}
}
sort.Slice(filtered, func(i, j int) bool {
return filtered[i].PubDate.After(filtered[j].PubDate)
})
feedsMutex.Lock()
allEntries = filtered
feedsMutex.Unlock()
}
func setUpdating(updating bool) {
statusMutex.Lock()
feedStatus.Updating = updating
statusMutex.Unlock()
}
func getEntries() []Entry {
feedsMutex.RLock()
defer feedsMutex.RUnlock()
return allEntries
}
func getStatus() FeedStatus {
statusMutex.RLock()
defer statusMutex.RUnlock()
return feedStatus
}
func hashURL(url string) string {
hasher := md5.New()
hasher.Write([]byte(url))
return hex.EncodeToString(hasher.Sum(nil))
}
func cacheURL(originalURL string) string {
hashedURL := hashURL(originalURL)
ext := path.Ext(originalURL)
cacheMutex.Lock()
urlCache[hashedURL] = originalURL
cacheMutex.Unlock()
return "/proxy/" + hashedURL + ext
}
func getOriginalURL(hashedURL string) (string, bool) {
cacheMutex.RLock()
originalURL, exists := urlCache[strings.TrimSuffix(hashedURL, path.Ext(hashedURL))]
cacheMutex.RUnlock()
return originalURL, exists
}
func proxyHandler(w http.ResponseWriter, r *http.Request) {
hashedFilename := path.Base(r.URL.Path)
originalURL, exists := getOriginalURL(hashedFilename)
if !exists {
http.Error(w, "Image not found", http.StatusNotFound)
return
}
resp, err := httpClient.Get(originalURL)
if err != nil {
http.Error(w, "Failed to fetch image", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
drainBody(resp)
http.Error(w, "Failed to fetch image", http.StatusBadGateway)
return
}
w.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
w.Header().Set("Cache-Control", "public, max-age=31536000")
if _, err := io.Copy(w, resp.Body); err != nil {
log.Printf("Error streaming image: %v", err)
}
}
func feedHandler(w http.ResponseWriter, r *http.Request) {
data := PageData{
Entries: getEntries(),
Status: getStatus(),
}
if convertURL := strings.TrimSpace(r.URL.Query().Get("convert")); convertURL != "" {
data.ConvertURL = convertURL
channelID, err := resolveChannelID(convertURL)
if err != nil {
data.ConvertError = err.Error()
} else {
data.ConvertResult = fmt.Sprintf(` %s: "%s"`, configLabel(convertURL, channelID), channelID)
}
}
if err := tmpl.Execute(w, data); err != nil {
log.Printf("Template execution error: %v", err)
http.Error(w, "Error rendering template", http.StatusInternalServerError)
return
}
}
func timeAgo(t time.Time) string {
if t.IsZero() {
return "never"
}
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()))
case diff < 30*24*time.Hour:
days := int(diff.Hours() / 24)
if days == 1 {
return "1 day ago"
}
return fmt.Sprintf("%d days ago", days)
case diff < 365*24*time.Hour:
months := int(diff.Hours() / (24 * 30))
if months == 1 {
return "1 month ago"
}
return fmt.Sprintf("%d months ago", months)
default:
return t.Format("Jan 2, 2006")
}
}
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)
}
}