Skip to content

Commit

Permalink
Licensing changes
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Sachs <898627+asachs01@users.noreply.github.com>
  • Loading branch information
asachs01 committed Sep 7, 2024
1 parent dbe8c44 commit 2824d43
Showing 1 changed file with 119 additions and 100 deletions.
219 changes: 119 additions & 100 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
LINQ Connect Menu Notifier
Copyright (C) 2024 Aaron Sachs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package main

import (
Expand All @@ -10,7 +28,8 @@ import (
"os"
"strings"
"time"
"github.com/arran4/golang-ical"

ics "github.com/arran4/golang-ical"
)

const apiURL = "https://api.linqconnect.com/api/FamilyMenu"
Expand All @@ -23,13 +42,13 @@ type MenuResponse struct {
Days []struct {
Date string `json:"Date"`
MenuMeals []struct {
MenuMealName string `json:"MenuMealName"`
RecipeCategories []struct {
CategoryName string `json:"CategoryName"`
Recipes []struct {
RecipeName string `json:"RecipeName"`
} `json:"Recipes"`
} `json:"RecipeCategories"`
MenuMealName string `json:"MenuMealName"`
RecipeCategories []struct {
CategoryName string `json:"CategoryName"`
Recipes []struct {
RecipeName string `json:"RecipeName"`
} `json:"Recipes"`
} `json:"RecipeCategories"`
} `json:"MenuMeals"`
} `json:"Days"`
} `json:"MenuPlans"`
Expand Down Expand Up @@ -143,7 +162,7 @@ func run(buildingID, districtID, recipients, sender, password, smtpServer, subje
}

func constructURL(buildingID, districtID, startDate, endDate string) string {
return fmt.Sprintf("%s?buildingId=%s&districtId=%s&startDate=%s&endDate=%s",
return fmt.Sprintf("%s?buildingId=%s&districtId=%s&startDate=%s&endDate=%s",
apiURL, buildingID, districtID, startDate, endDate)
}

Expand Down Expand Up @@ -221,98 +240,98 @@ func sendEmail(smtpServer, from, password string, to []string, subject, body str
}

func createICSFile(buildingID, districtID, weekStart, outputPath string, debug bool) error {
// Parse the week start date
start, err := time.Parse("01-02-2006", weekStart)
if err != nil {
return fmt.Errorf("invalid week start date: %w", err)
}

// Create a new calendar
cal := ics.NewCalendar()
cal.SetMethod(ics.MethodPublish)

// Iterate through the menu for 5 days (Monday to Friday)
for i := 0; i < 5; i++ {
date := start.AddDate(0, 0, i)
dateStr := date.Format("01-02-2006")
// Fetch menu for this specific date
url := constructURL(buildingID, districtID, dateStr, dateStr)
menu, err := getMenu(url)
if err != nil {
if debug {
fmt.Printf("Error fetching menu for date %s: %v\n", dateStr, err)
}
continue
}

lunchMenu := getLunchMenuForDate(menu, date.Format("1/2/2006"), debug)

if lunchMenu != "" {
event := cal.AddEvent(fmt.Sprintf("lunch-%s", date.Format("2006-01-02")))
event.SetCreatedTime(time.Now())
event.SetDtStampTime(time.Now())
event.SetModifiedAt(time.Now())
// Set as an all-day event
event.SetAllDayStartAt(date)
event.SetAllDayEndAt(date.AddDate(0, 0, 1)) // End date is exclusive, so we add one day

event.SetSummary(fmt.Sprintf("Lunch Menu - %s", date.Format("01/02/2006")))
event.SetDescription(lunchMenu)
} else if debug {
fmt.Printf("No lunch menu found for date: %s\n", date.Format("01/02/2006"))
}
}

// Create the output file
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()

// Write the ICS file
return cal.SerializeTo(file)
// Parse the week start date
start, err := time.Parse("01-02-2006", weekStart)
if err != nil {
return fmt.Errorf("invalid week start date: %w", err)
}

// Create a new calendar
cal := ics.NewCalendar()
cal.SetMethod(ics.MethodPublish)

// Iterate through the menu for 5 days (Monday to Friday)
for i := 0; i < 5; i++ {
date := start.AddDate(0, 0, i)
dateStr := date.Format("01-02-2006")

// Fetch menu for this specific date
url := constructURL(buildingID, districtID, dateStr, dateStr)
menu, err := getMenu(url)
if err != nil {
if debug {
fmt.Printf("Error fetching menu for date %s: %v\n", dateStr, err)
}
continue
}

lunchMenu := getLunchMenuForDate(menu, date.Format("1/2/2006"), debug)

if lunchMenu != "" {
event := cal.AddEvent(fmt.Sprintf("lunch-%s", date.Format("2006-01-02")))
event.SetCreatedTime(time.Now())
event.SetDtStampTime(time.Now())
event.SetModifiedAt(time.Now())

// Set as an all-day event
event.SetAllDayStartAt(date)
event.SetAllDayEndAt(date.AddDate(0, 0, 1)) // End date is exclusive, so we add one day

event.SetSummary(fmt.Sprintf("Lunch Menu - %s", date.Format("01/02/2006")))
event.SetDescription(lunchMenu)
} else if debug {
fmt.Printf("No lunch menu found for date: %s\n", date.Format("01/02/2006"))
}
}

// Create the output file
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()

// Write the ICS file
return cal.SerializeTo(file)
}

func getLunchMenuForDate(menu *MenuResponse, date string, debug bool) string {
var lunchMenu strings.Builder

if debug {
fmt.Printf("Searching for lunch menu on date: %s\n", date)
}

for _, session := range menu.FamilyMenuSessions {
if session.ServingSession == "Lunch" {
for _, plan := range session.MenuPlans {
for _, day := range plan.Days {
if debug {
fmt.Printf("Checking day: %s\n", day.Date)
}

if day.Date == date {
// Build lunch menu
fmt.Fprintf(&lunchMenu, "Lunch Menu for %s:\n\n", day.Date)
for _, meal := range day.MenuMeals {
fmt.Fprintf(&lunchMenu, "%s:\n", meal.MenuMealName)
for _, category := range meal.RecipeCategories {
fmt.Fprintf(&lunchMenu, " %s:\n", category.CategoryName)
for _, recipe := range category.Recipes {
fmt.Fprintf(&lunchMenu, " - %s\n", recipe.RecipeName)
}
}
lunchMenu.WriteString("\n")
}
return lunchMenu.String()
}
}
}
}
}

if debug {
fmt.Printf("No lunch menu found for date: %s\n", date)
}
return ""
var lunchMenu strings.Builder

if debug {
fmt.Printf("Searching for lunch menu on date: %s\n", date)
}

for _, session := range menu.FamilyMenuSessions {
if session.ServingSession == "Lunch" {
for _, plan := range session.MenuPlans {
for _, day := range plan.Days {
if debug {
fmt.Printf("Checking day: %s\n", day.Date)
}

if day.Date == date {
// Build lunch menu
fmt.Fprintf(&lunchMenu, "Lunch Menu for %s:\n\n", day.Date)
for _, meal := range day.MenuMeals {
fmt.Fprintf(&lunchMenu, "%s:\n", meal.MenuMealName)
for _, category := range meal.RecipeCategories {
fmt.Fprintf(&lunchMenu, " %s:\n", category.CategoryName)
for _, recipe := range category.Recipes {
fmt.Fprintf(&lunchMenu, " - %s\n", recipe.RecipeName)
}
}
lunchMenu.WriteString("\n")
}
return lunchMenu.String()
}
}
}
}
}

if debug {
fmt.Printf("No lunch menu found for date: %s\n", date)
}
return ""
}

0 comments on commit 2824d43

Please sign in to comment.