2024-10-24 12:33:37 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-25 11:42:30 +02:00
|
|
|
"fmt"
|
2024-10-24 12:54:47 +02:00
|
|
|
"log"
|
2024-10-24 12:33:37 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/apognu/gocal"
|
|
|
|
"github.com/gorilla/feeds"
|
|
|
|
)
|
|
|
|
|
|
|
|
func convertICStoRSS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Lese die ICS-URL aus dem Query-Parameter
|
|
|
|
icsURL := r.URL.Query().Get("ics")
|
|
|
|
if icsURL == "" {
|
2024-10-24 12:54:47 +02:00
|
|
|
log.Println("[ERROR] Missing 'ics' query parameter")
|
2024-10-24 12:33:37 +02:00
|
|
|
http.Error(w, "Missing 'ics' query parameter", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-25 11:42:30 +02:00
|
|
|
// Lese den Zeitraum aus dem Query-Parameter
|
|
|
|
timeRange := r.URL.Query().Get("range")
|
|
|
|
if timeRange == "" {
|
|
|
|
timeRange = "all" // Standardwert, wenn kein Zeitraum angegeben ist
|
|
|
|
}
|
|
|
|
|
2024-10-24 12:54:47 +02:00
|
|
|
log.Printf("[INFO] Fetching ICS file from URL: %s\n", icsURL)
|
|
|
|
|
2024-10-24 12:33:37 +02:00
|
|
|
// ICS-Datei herunterladen
|
|
|
|
resp, err := http.Get(icsURL)
|
|
|
|
if err != nil {
|
2024-10-24 12:54:47 +02:00
|
|
|
log.Printf("[ERROR] Unable to fetch ICS file: %v\n", err)
|
2024-10-24 12:33:37 +02:00
|
|
|
http.Error(w, "Unable to fetch ICS file", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2024-10-24 12:54:47 +02:00
|
|
|
log.Println("[INFO] Parsing ICS file")
|
|
|
|
|
2024-10-24 12:33:37 +02:00
|
|
|
// ICS-Datei parsen
|
|
|
|
parser := gocal.NewParser(resp.Body)
|
|
|
|
parser.Parse()
|
|
|
|
|
|
|
|
// RSS-Feed erstellen
|
|
|
|
feed := &feeds.Feed{
|
|
|
|
Title: "Converted Calendar Feed",
|
|
|
|
Link: &feeds.Link{Href: icsURL},
|
2024-10-25 11:42:30 +02:00
|
|
|
Description: fmt.Sprintf("This is a converted calendar feed for %s", timeRange),
|
2024-10-24 12:33:37 +02:00
|
|
|
Created: time.Now(),
|
|
|
|
}
|
|
|
|
|
2024-10-25 11:42:30 +02:00
|
|
|
now := time.Now()
|
2024-10-24 12:33:37 +02:00
|
|
|
for _, event := range parser.Events {
|
2024-10-25 11:42:30 +02:00
|
|
|
if shouldIncludeEvent(event, timeRange, now) {
|
|
|
|
item := &feeds.Item{
|
|
|
|
Title: event.Summary,
|
|
|
|
Description: event.Description,
|
|
|
|
Link: &feeds.Link{Href: icsURL},
|
|
|
|
Created: *event.Start,
|
|
|
|
}
|
|
|
|
feed.Items = append(feed.Items, item)
|
2024-10-24 12:33:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-24 12:54:47 +02:00
|
|
|
log.Println("[INFO] Generating RSS feed")
|
|
|
|
|
2024-10-25 14:41:42 +02:00
|
|
|
// RSS als XML generieren
|
2024-10-24 12:33:37 +02:00
|
|
|
rssData, err := feed.ToRss()
|
|
|
|
if err != nil {
|
2024-10-24 12:54:47 +02:00
|
|
|
log.Printf("[ERROR] Unable to generate RSS feed: %v\n", err)
|
2024-10-24 12:33:37 +02:00
|
|
|
http.Error(w, "Unable to generate RSS feed", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-25 14:41:42 +02:00
|
|
|
// Header setzen für XML-Anzeige im Browser
|
|
|
|
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
2024-10-24 12:54:47 +02:00
|
|
|
|
2024-10-25 14:41:42 +02:00
|
|
|
// XML-Daten an den Browser senden
|
|
|
|
fmt.Fprint(w, rssData)
|
|
|
|
|
|
|
|
log.Println("[INFO] RSS feed successfully generated and sent as XML")
|
2024-10-24 12:33:37 +02:00
|
|
|
}
|
|
|
|
|
2024-10-25 11:42:30 +02:00
|
|
|
func shouldIncludeEvent(event gocal.Event, timeRange string, now time.Time) bool {
|
|
|
|
switch timeRange {
|
|
|
|
case "today":
|
|
|
|
return event.Start.Year() == now.Year() && event.Start.YearDay() == now.YearDay()
|
|
|
|
case "week":
|
|
|
|
_, thisWeek := now.ISOWeek()
|
|
|
|
_, eventWeek := event.Start.ISOWeek()
|
|
|
|
return event.Start.Year() == now.Year() && eventWeek == thisWeek
|
|
|
|
case "month":
|
|
|
|
return event.Start.Year() == now.Year() && event.Start.Month() == now.Month()
|
|
|
|
default:
|
|
|
|
return true // "all" oder jeder andere Wert zeigt alle Ereignisse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-24 12:33:37 +02:00
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/rss", convertICStoRSS)
|
2024-10-24 12:54:47 +02:00
|
|
|
log.Println("[INFO] Server is running at :8080")
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
|
|
log.Fatalf("[FATAL] Server failed to start: %v\n", err)
|
|
|
|
}
|
2024-10-24 12:33:37 +02:00
|
|
|
}
|