More whereing and whening
This commit is contained in:
parent
460a0200d4
commit
355e07cf53
@ -36,6 +36,10 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
dateFmt = "2006-01-02"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
@ -72,27 +76,44 @@ func main() {
|
||||
log.Fatalf("Unable to retrieve next %d events. %v", maxResults, err)
|
||||
}
|
||||
|
||||
// days := generateDays(startT, endT)
|
||||
found := []*miniEvent{}
|
||||
log.Printf("found %d events", len(events.Items))
|
||||
log.Printf("generating days between %v and %v", startT, endT)
|
||||
|
||||
days := generateDays(startT, endT)
|
||||
foundEvents := []*lilEvent{}
|
||||
defaultLocation := *defaultLocationFlag
|
||||
|
||||
log.Printf("generated %d days between %v and %v", len(days), startT, endT)
|
||||
|
||||
if len(events.Items) > 0 {
|
||||
for _, i := range events.Items {
|
||||
var (
|
||||
startTime string
|
||||
endTime string
|
||||
startDate string
|
||||
endDate string
|
||||
)
|
||||
|
||||
if i.Start.DateTime != "" {
|
||||
startTime = i.Start.DateTime
|
||||
endTime = i.End.DateTime
|
||||
startT, err := time.Parse(time.RFC3339, i.Start.DateTime)
|
||||
if err != nil {
|
||||
log.Printf("error parsing start date: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
endT, err := time.Parse(time.RFC3339, i.End.DateTime)
|
||||
if err != nil {
|
||||
log.Printf("error parsing end date: %v", err)
|
||||
continue
|
||||
}
|
||||
startDate = startT.Format(dateFmt)
|
||||
endDate = endT.Format(dateFmt)
|
||||
} else {
|
||||
startTime = i.Start.Date
|
||||
endTime = i.End.Date
|
||||
startDate = i.Start.Date
|
||||
endDate = i.End.Date
|
||||
}
|
||||
|
||||
evt := &miniEvent{
|
||||
Start: startTime,
|
||||
End: endTime,
|
||||
evt := &lilEvent{
|
||||
Start: startDate,
|
||||
End: endDate,
|
||||
Location: defaultLocation,
|
||||
Summary: strings.ToLower(strings.TrimSpace(i.Summary)),
|
||||
}
|
||||
@ -115,21 +136,33 @@ func main() {
|
||||
}
|
||||
evt.Location = loc
|
||||
|
||||
found = append(found, evt)
|
||||
if evt.Start == evt.End {
|
||||
log.Printf("skipping same-day event on %s", evt.Start)
|
||||
continue
|
||||
}
|
||||
foundEvents = append(foundEvents, evt)
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "No events found.\n")
|
||||
log.Println("No events found")
|
||||
}
|
||||
|
||||
// TODO: sort found events
|
||||
for _, day := range days {
|
||||
log.Printf("working on date %v", day)
|
||||
for _, evt := range foundEvents {
|
||||
log.Printf("checking if %s is in range %s %s", day.Date, evt.Start, evt.End)
|
||||
if evt.HasDate(day.Date) {
|
||||
day.Location = evt.Location
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, evt := range found {
|
||||
asJson, err := json.Marshal(evt)
|
||||
for _, day := range days {
|
||||
asJson, err := json.Marshal(day)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||
log.Printf("ERROR: %v", err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("%s\n", string(asJson))
|
||||
fmt.Println(string(asJson))
|
||||
}
|
||||
}
|
||||
|
||||
@ -195,15 +228,69 @@ func saveToken(file string, token *oauth2.Token) {
|
||||
json.NewEncoder(f).Encode(token)
|
||||
}
|
||||
|
||||
type miniEvent struct {
|
||||
Start string `json:"start"`
|
||||
StartDate string `json:"start_date"`
|
||||
End string `json:"end"`
|
||||
EndDate string `json:"end_date"`
|
||||
Location string `json:"loc"`
|
||||
Summary string `json:"summ"`
|
||||
type lilEvent struct {
|
||||
Start string `json:"start"`
|
||||
End string `json:"end"`
|
||||
Location string `json:"loc"`
|
||||
Summary string `json:"summ"`
|
||||
}
|
||||
|
||||
func generateDays(startT, endT time.Time) []string {
|
||||
return []string{}
|
||||
func (e *lilEvent) HasDate(d string) bool {
|
||||
if e.Start == d || e.End == d {
|
||||
log.Printf("match for %s in %s or %s", d, e.Start, e.End)
|
||||
return true
|
||||
}
|
||||
|
||||
dT, err := time.Parse(dateFmt, d)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
dr := e.dateRange()
|
||||
return dr[0].Before(dT) && dr[1].After(dT)
|
||||
}
|
||||
|
||||
func (e *lilEvent) dateRange() []time.Time {
|
||||
ret := []time.Time{}
|
||||
for _, t := range []string{e.Start, e.End} {
|
||||
parsedDate, err := time.Parse(dateFmt, t)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: %v", err)
|
||||
ret = append(ret, time.Time{})
|
||||
continue
|
||||
}
|
||||
ret = append(ret, parsedDate)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
type lilDay struct {
|
||||
Date string `json:"date"`
|
||||
Location string `json:"loc"`
|
||||
}
|
||||
|
||||
func generateDays(startT, endT time.Time) []*lilDay {
|
||||
defaultLocation := *defaultLocationFlag
|
||||
days := []*lilDay{
|
||||
{
|
||||
Date: startT.Format(dateFmt),
|
||||
Location: defaultLocation,
|
||||
},
|
||||
}
|
||||
|
||||
curT := startT
|
||||
for {
|
||||
if curT.After(endT) {
|
||||
return days
|
||||
}
|
||||
|
||||
days = append(days, &lilDay{
|
||||
Date: curT.Format(dateFmt),
|
||||
Location: defaultLocation,
|
||||
})
|
||||
|
||||
curT = curT.Add(24 * time.Hour)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user