-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbb60de
commit 9e0c1fa
Showing
6 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ai | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/apognu/gocal" | ||
) | ||
|
||
func ProvideCalendar(url string, start, end time.Time) (string, error) { | ||
resp, err := http.DefaultClient.Get(url) | ||
if err != nil { | ||
return "", fmt.Errorf("could not get calendar: %w", err) | ||
} | ||
if resp.Body == nil { | ||
defer resp.Body.Close() | ||
} | ||
|
||
calendar := gocal.NewParser(resp.Body) | ||
calendar.Start, calendar.End = &start, &end | ||
|
||
err = calendar.Parse() | ||
if err != nil { | ||
return "", fmt.Errorf("could not parse calendar: %w", err) | ||
} | ||
|
||
type Event struct { | ||
Summary string `json:"summary"` | ||
Start string `json:"start"` | ||
End string `json:"end"` | ||
Description string `json:"description"` | ||
Location string `json:"location"` | ||
} | ||
|
||
events := make([]Event, len(calendar.Events)) | ||
for i, e := range calendar.Events { | ||
events[i] = Event{ | ||
Summary: e.Summary, | ||
Start: formatCalendarDate(e.Start), | ||
End: formatCalendarDate(e.End), | ||
Description: e.Description, | ||
Location: e.Location, | ||
} | ||
} | ||
|
||
jsonData, err := json.Marshal(events) | ||
if err != nil { | ||
return "", fmt.Errorf("could not marshal calendar events: %w", err) | ||
} | ||
|
||
return fmt.Sprintf("Calendar contains following events:\n<json>\n%s\n</json>", string(jsonData)), nil | ||
} | ||
|
||
func formatCalendarDate(date *time.Time) string { | ||
if date == nil { | ||
return "unknown" | ||
} | ||
return date.Format("2006-01-02") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters