Skip to content

Commit

Permalink
fix some parsing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnani committed Aug 20, 2021
1 parent c1210dc commit f915e7a
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"bytes"
"crypto/sha256"
"fmt"
"io/ioutil"
"log"
"net/http"
"reflect"
"regexp"
"strings"
"time"

Expand All @@ -14,6 +17,8 @@ import (
"google.golang.org/api/calendar/v3"
)

var dateFormatFix = regexp.MustCompile(`(?m)^(DTSTAMP:.*)T4(.*)$`)

func parseICal(url string) []gocal.Event {
resp, err := http.Get(url)
if err != nil {
Expand All @@ -22,7 +27,15 @@ func parseICal(url string) []gocal.Event {

defer resp.Body.Close()

c := gocal.NewParser(resp.Body)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Failed to read calendar body")
}

//This is really remarkably dumb but some sources give back garbage
body := bytes.NewReader(dateFormatFix.ReplaceAll(b, []byte("$1")))

c := gocal.NewParser(body)
if err := c.Parse(); err != nil {
panic(err)
}
Expand Down Expand Up @@ -116,7 +129,15 @@ func diffEvents(cfg Config, up []gocal.Event, gevent []*calendar.Event) ([]*cale
del := make([]string, 0, len(ids))
for _, e := range ids {
if e.Status != "cancelled" {
del = append(del, e.Id)
t, err := time.Parse(e.Start.DateTime, time.RFC3339)
if err != nil {
log.Fatalf("Unable to parse date time %s: %v", e.Start.DateTime, err)
}
if time.Now().Before(t) {
del = append(del, e.Id)
} else {
log.Printf("Not deleting event %s because it already started", e.Summary)
}
}
}
return create, update, del
Expand Down

0 comments on commit f915e7a

Please sign in to comment.