Skip to content

Commit

Permalink
transition to clean API
Browse files Browse the repository at this point in the history
  • Loading branch information
loan-mgt committed Jan 23, 2025
1 parent fd98ef2 commit 55eeec4
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 295 deletions.
35 changes: 7 additions & 28 deletions handlers/your-cal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"cpe/calendar/decrypt"
"cpe/calendar/ical"
"cpe/calendar/request"
"fmt"
"log"
"net/http"
"os"
"strings"
)

func Health(w http.ResponseWriter, r *http.Request){
func Health(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

Expand Down Expand Up @@ -52,20 +53,14 @@ func GenerateICSHandler(w http.ResponseWriter, r *http.Request) {
pass := parts[1]

// Fetch data from the source
data, err := request.FetchData(start, end, username, pass)
events, err := request.FetchData(start, end, username, pass)
if err != nil {
log.Printf("Failed to fetch data: %v", err)
http.Error(w, "Failed to fetch data", http.StatusInternalServerError)
return
}

// Parse the fetched data
events, err := ical.ParseEvents(data)
if err != nil {
log.Printf("Failed to parse events: %v", err)
http.Error(w, "Failed to parse events", http.StatusInternalServerError)
return
}
fmt.Println("Event found", len(events))

// Generate the iCal file with the calendar name
icsContent := ical.GenerateICS(events, calendarName)
Expand All @@ -79,10 +74,6 @@ func GenerateICSHandler(w http.ResponseWriter, r *http.Request) {
}

func ValidateHandler(w http.ResponseWriter, r *http.Request) {
// Get start and end times from environment variables
start := os.Getenv("START_TIMESTAMP")
end := os.Getenv("END_TIMESTAMP")

// Get separator from environment variable
separator := os.Getenv("SEPARATOR")

Expand Down Expand Up @@ -115,24 +106,12 @@ func ValidateHandler(w http.ResponseWriter, r *http.Request) {
pass := parts[1]

// Fetch data from the source
data, err := request.FetchData(start, end, username, pass)
_, err = request.Login(username, pass)
if err != nil {
log.Printf("Failed to fetch data: %v", err)
http.Error(w, "Failed to fetch data", http.StatusInternalServerError)
return
}

// Parse the fetched data
events, err := ical.ParseEvents(data)
if err != nil {
log.Printf("Failed to parse events: %v", err)
http.Error(w, "Failed to parse events", http.StatusInternalServerError)
w.WriteHeader(http.StatusUnauthorized)
return
}

if len(events) > 0 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusUnauthorized)
}
w.WriteHeader(http.StatusOK)
}
33 changes: 10 additions & 23 deletions ical/gen.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package ical

import (
"cpe/calendar/types"
"fmt"
"regexp"
"strings"
"time"
)

// Regular expression for splitting title into components
const regexPattern = `(?P<location>.*?) (?P<promo>[1-9][A-Z]{3,}(?: GR[A-Z0-9])?) (?P<summary>.*?)(?P<description>(( |n)[A-Z-]{3,} .*)|$)`

// GenerateICS generates an ICS string from a list of events
func GenerateICS(events []Event, calendarName string) string {
func GenerateICS(events []types.Event, calendarName string) string {
ics := "BEGIN:VCALENDAR\n"
ics += "VERSION:2.0\n"
ics += "PRODID:-//github.com/qypol342 //CPE Calendar//EN\n"
Expand All @@ -22,36 +18,27 @@ func GenerateICS(events []Event, calendarName string) string {
ics += "REFRESH-INTERVAL;VALUE=DURATION:PT1H\n"

// Define the layout for parsing the datetime with a timezone offset
const layout = "2006-01-02T15:04:05-0700"

// Compile the regular expression
re := regexp.MustCompile(regexPattern)
const layout = "2006-01-02T15:04:05.000"

for _, event := range events {
// Remove newline characters from title
cleanedTitle := strings.ReplaceAll(event.Title, "\n", " ")

// Apply regex to split title
matches := re.FindStringSubmatch(cleanedTitle)
if matches == nil {
// Handle case where regex does not match
fmt.Println("Error parsing title:", event.Title)
if event.Favori == nil {
continue
}

// Extract components from regex matches
location := matches[1]
summary := matches[3]
description := matches[4]
location := event.Favori.F2
summary := event.Favori.F5 + event.Favori.F3
description := event.Favori.F4

// Parse the start and end times in the given time zone
start, err := time.Parse(layout, event.Start)
start, err := time.Parse(layout, event.DateDebut)
if err != nil {
// Handle parsing error
fmt.Println("Error parsing start time:", err)
continue
}
end, err := time.Parse(layout, event.End)
end, err := time.Parse(layout, event.DateFin)
if err != nil {
// Handle parsing error
fmt.Println("Error parsing end time:", err)
Expand All @@ -64,7 +51,7 @@ func GenerateICS(events []Event, calendarName string) string {

// Format times for ICS
ics += "BEGIN:VEVENT\n"
ics += fmt.Sprintf("UID:%s\n", event.ID)
ics += fmt.Sprintf("UID:%d\n", event.ID)
ics += fmt.Sprintf("DTSTART:%s\n", start.Format("20060102T150405Z"))
ics += fmt.Sprintf("DTEND:%s\n", end.Format("20060102T150405Z"))
ics += fmt.Sprintf("LOCATION:%s\n", location)
Expand Down
77 changes: 0 additions & 77 deletions ical/parse.go

This file was deleted.

Loading

0 comments on commit 55eeec4

Please sign in to comment.