Skip to content

Commit

Permalink
- Updated EpitechEvent fields name
Browse files Browse the repository at this point in the history
- Fixed a sync issue resulting in duplicate Google calendar events
  • Loading branch information
shellbear committed Sep 17, 2019
1 parent d504074 commit 100b5d4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 36 deletions.
83 changes: 55 additions & 28 deletions pkg/epical/epical.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const (
CalendarName = "EpiCal"
Version = "0.1.4"
Version = "0.1.5"
)

func ListEvents(epitechToken string) {
Expand Down Expand Up @@ -59,32 +59,35 @@ func ClearEvents(credentialsPath string, deleteFrom time.Time, deleteCalendar bo
log.Fatalln("Failed to get Google calendar ", err)
}

if cal != nil {
events, err := svc.Events.List(cal.Id).Do()
if cal == nil {
return
}

events, err := GetGoogleCalendarEvents(cal.Id, svc)
if err != nil {
log.Fatalln("Failed to list calendar events", err)
}

for _, evt := range events {
t, err := time.Parse(time.RFC3339, evt.Start.DateTime)
if err != nil {
log.Fatalln("Failed to list calendar events", err)
log.Fatalln("Failed to parse calendar event datetime,", err)
}

for _, evt := range events.Items {
t, err := time.Parse(time.RFC3339, evt.Start.DateTime)
if t.After(deleteFrom) {
err = svc.Events.Delete(cal.Id, evt.Id).Do()
if err != nil {
log.Fatalln("Failed to parse calendar event datetime,", err)
}

if t.After(deleteFrom) {
err = svc.Events.Delete(cal.Id, evt.Id).Do()
if err != nil {
log.Fatalln("Failed to delete calendar event,", err)
}
log.Fatalln("Failed to delete calendar event,", err)
}
}
}

if deleteCalendar {
if err = svc.Calendars.Delete(cal.Id).Do(); err != nil {
log.Fatalln("Failed to delete Google calendar,", err)
}
if deleteCalendar {
if err = svc.Calendars.Delete(cal.Id).Do(); err != nil {
log.Fatalln("Failed to delete Google calendar,", err)
}
}

}

func SyncCalendar(credentialsPath, token string) {
Expand All @@ -99,25 +102,43 @@ func SyncCalendar(credentialsPath, token string) {
log.Fatalln("Failed to get Google calendar service,", err)
}

t := time.Now().Truncate(time.Hour * 24)

ClearEvents(credentialsPath, t, false)
fmt.Println("Cleared old calendar events.")

cal, err := GetOrCreateGoogleCalendar(svc, CalendarName)
if err != nil {
log.Fatalln("Failed to get Google calendar,", err)
}

googleEvents, err := GetGoogleCalendarEvents(cal.Id, svc)
if err != nil {
log.Fatalln("Failed to get Google calendar events,", err)
}

if len(data) == 0 {
fmt.Println("There is no upcoming Epitech event.")
} else {
for _, c := range data {
newEvt, err := NewGoogleCalendarEvent(&c)
if err != nil {
log.Fatalln("Failed to create Google calendar event,", err)
return
}

i := 0

for _, c := range data {
found := false
newEvt, err := NewGoogleCalendarEvent(&c)
if err != nil {
log.Fatalln("Failed to create Google calendar event,", err)
}

for _, oldEvt := range googleEvents {
description := strings.Split(oldEvt.Description, "\n")

if len(description) != 0 && c.CodeEvent == description[0] && newEvt.Summary == oldEvt.Summary &&
newEvt.Start.Date == oldEvt.Start.Date && newEvt.End.Date == oldEvt.End.Date {
googleEvents[i] = oldEvt
i++
found = true
break
}
}

if !found {
evt, err := svc.Events.Insert(cal.Id, newEvt).Do()
if err != nil {
log.Fatalln("Failed to create Google calendar event", err)
Expand All @@ -126,4 +147,10 @@ func SyncCalendar(credentialsPath, token string) {
log.Println("Created event", evt.Summary)
}
}

googleEvents = googleEvents[:i]

for _, event := range googleEvents {
svc.Events.Delete(cal.Id, event.Id)
}
}
12 changes: 6 additions & 6 deletions pkg/epical/epitech.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
)

type EpitechEvent struct {
Scolaryear string `json:"scolaryear"`
Codemodule string `json:"codemodule"`
Codeinstance string `json:"codeinstance"`
Codeacti string `json:"codeacti"`
Codeevent string `json:"codeevent"`
ScholarYear string `json:"scolaryear"`
CodeModule string `json:"codemodule"`
CodeInstance string `json:"codeinstance"`
CodeActi string `json:"codeacti"`
CodeEvent string `json:"codeevent"`
Semester int `json:"semester"`
InstanceLocation string `json:"instance_location"`
Titlemodule string `json:"titlemodule"`
ModuleTitle string `json:"titlemodule"`
ProfInst []struct {
Type string `json:"type"`
Login string `json:"login"`
Expand Down
32 changes: 30 additions & 2 deletions pkg/epical/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ func NewGoogleCalendarEvent(event *EpitechEvent) (*calendar.Event, error) {
}

newEvent := &calendar.Event{
Etag: event.CodeEvent,
Summary: event.ActiTitle,
Description: fmt.Sprintf("%s/module/%s/%s/%s/%s", EpitechBaseUrl,
event.Scolaryear, event.Codemodule, event.Codeinstance, event.Codeacti),
Description: fmt.Sprintf("%s\n%s/module/%s/%s/%s/%s", event.CodeEvent, EpitechBaseUrl,
event.ScholarYear, event.CodeModule, event.CodeInstance, event.CodeActi),
Start: &calendar.EventDateTime{
DateTime: start.Format(GoogleCalendarApiTimeFormat),
TimeZone: "Europe/Paris",
Expand Down Expand Up @@ -196,3 +197,30 @@ func GetGoogleCalendarService(credentialsPath string) (*calendar.Service, error)

return calendarService, err
}

func GetGoogleCalendarEvents(calID string, service *calendar.Service) ([]*calendar.Event, error) {
var pageToken string
var eventsCall *calendar.Events
var events []*calendar.Event
var err error

for {
if pageToken == "" {
eventsCall, err = service.Events.List(calID).Do()
} else {
eventsCall, err = service.Events.List(calID).PageToken(pageToken).Do()
}

if err != nil {
return nil, err
}

events = append(events, eventsCall.Items...)
pageToken = eventsCall.NextPageToken
if pageToken == "" {
break
}
}

return events, nil
}

0 comments on commit 100b5d4

Please sign in to comment.