Skip to content

Commit

Permalink
Handle duplicate event ids in calendar urls
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnani committed Oct 26, 2021
1 parent 6cb87d0 commit 53ccdd0
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"io/ioutil"
"log"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/akarnani/webcal_sync/gcal"
"github.com/apognu/gocal"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/googleapi"
)

var dateFormatFix = regexp.MustCompile(`(?m)^(DTSTAMP:.*)T(.*)$`)
Expand Down Expand Up @@ -63,12 +65,22 @@ func diffEvents(cfg Config, up []gocal.Event, gevent []*calendar.Event) ([]*cale
ids[e.ICalUID] = e
}

seenIds := make(map[string]interface{})

for _, e := range up {
if (*e.Start).Before(time.Now()) {
continue
}

i := getIDForEvent(cfg, e)

if _, ok := seenIds[i]; ok {
log.Printf("ID %s is a duplicate, not processing", i)
continue
}

seenIds[i] = nil

g, ok := ids[i]
delete(ids, i)
if !ok {
Expand Down Expand Up @@ -170,18 +182,27 @@ func getIDForEvent(cfg Config, e gocal.Event) string {
case "":
return e.Uid
default:
panic(fmt.Sprintf("unknown id format %s", cfg.IDFormat))
log.Panicf("unknown id format %s", cfg.IDFormat)
}

//can't be reached due to default's Panicf
return ""
}

func main() {
client := gcal.NewClient()
for _, cfg := range getConfig() {
log.Printf("Starting on calendar %s", cfg.URL)
c, u, d := diffEvents(cfg, parseICal(cfg.URL), client.GetEventsForAttribute(map[string]string{"url": fmt.Sprintf("%x", sha256.Sum256([]byte(cfg.URL)))}))
fmt.Println(cfg.URL, len(c), len(u), len(d))
log.Println(cfg.URL, len(c), len(u), len(d))

for _, e := range c {
if err := client.CreateEvent(e); err != nil {
var gErr *googleapi.Error
if errors.As(err, &gErr) && gErr.Code == http.StatusConflict {
log.Printf("Event already existed: %v, %v", e, gErr)
continue
}
log.Fatalf("failed to create event: %v", err)
}
}
Expand All @@ -197,5 +218,7 @@ func main() {
}
}

log.Printf("finished with calendar")

}
}

0 comments on commit 53ccdd0

Please sign in to comment.