sandsmad_parser

package module
v1.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

README

Sandsmad Parser

Release Quality Test Go Coverage License GoDoc

This is a golang package for accessing this weeks dishes at sandsmad.dk in a structured format.

Use Cases
  • Slackbot alerting users about today's dish
  • Rating system based on dynamically updated content e.g. eat-score.hrgn.dk
  • Freedom
Why

Adding a programmatic access to semi-structured data helps 3rd parties develop integrations not possible before. This library exists for the sole purpose of it being able to exist.

Example usage

examples/cli-print/main.go

package main

import (
	"fmt"
	"github.com/frederikhs/sandsmad_parser"
	"log"
)

func main() {
	dishes, err := sandsmad_parser.FetchThisWeeksDishes()
	if err != nil {
		log.Fatal(err)
	}

	for i, dish := range dishes {
		fmt.Println(dish.NameOfDay)
		for _, line := range dish.Lines {
			fmt.Println(line)
		}

		if i < len(dishes)-1 {
			fmt.Println("---")
		}
	}
}

Results in the following ouput

$ go run examples/cli-print/main.go
Mandag
Inderfilet med flødeost og tomater samt basilikums olie (3 stk. pr. person indeholder laktose )
Pasta salat
Vegetar Svampebøf med halumi og rødbeder
---
Tirdsag
Bouf Stroganof med cocktail pølser og rodfrugter svampe samt perleløg indeholder laktose og svinekød
Ris
Vegetar Gryderet med kidney bønner rodfrugter perleløg og svampe
---
Onsdag
Kalkun deller med Bacon og frisk basilikum løg hvidløg (3 stk pr person) indeholder gluten
Kartoffelsalat med karry og forårsløg samt drænet yoghurt
Tærte med porre og kartoffel peber mælk og æg
---
Torsdag
Cremet kokos tomat kyllingegryde med pære og citron græs samt løg og urter mild karry
Ris med Basilikum olie
Vegetar Cremet kokos tomat gryde med limabønne pære og citron græs samt løg og urter mild karry
---
Fredag
Mexicansk spinat tortilla
Chili con carne med tomat og mager hakket oksekød chili bønner løg hvidløg
Vegetar vegansk chili sin carne med tomat og chili bønner løg hvidløg
Ingefærshot med presset æble og appelsin saft
Så i kan gå sunde på weekend

See also examples/json-api-webserver/main.go

Documentation

Index

Constants

View Source
const URL = "https://sandsmad.dk/"

Variables

View Source
var DayRegexes = []DayMatcher{
	{
		NameOfDay: "Mandag",
		Regex:     regexp.MustCompile(`(?i)Mandag\s([\S\s]*)\sTirsdag`),
	},
	{
		NameOfDay: "Tirsdag",
		Regex:     regexp.MustCompile(`(?i)Tirsdag\s([\S\s]*)\sOnsdag`),
	},
	{
		NameOfDay: "Onsdag",
		Regex:     regexp.MustCompile(`(?i)Onsdag\s([\S\s]*)\sTorsdag`),
	},
	{
		NameOfDay: "Torsdag",
		Regex:     regexp.MustCompile(`(?i)Torsdag\s([\S\s]*)\sFredag`),
	},
	{
		NameOfDay: "Fredag",
		Regex:     regexp.MustCompile(`(?i)Fredag\s([\S\s]*)\sMonday`),
	},
}

DayRegexes is a slice of DayMatcher for each weekday

View Source
var ErrWeekdayNotDishDay = errors.New("weekday is not a dish day")

Functions

This section is empty.

Types

type DayMatcher

type DayMatcher struct {
	NameOfDay DishDay
	Regex     *regexp.Regexp
}

DayMatcher is a struct containing a regular expression for matching the lines that describes a dish on a particular day

func (DayMatcher) Match

func (dm DayMatcher) Match(page *SandsPage) (*DishOfTheDay, error)

Match takes a menu and returns a DishOfTheDay parsing the menu using the DayMatcher

type DishDay added in v1.0.4

type DishDay string
const (
	Monday    DishDay = "Mandag"
	Tuesday   DishDay = "Tirsdag"
	Wednesday DishDay = "Onsdag"
	Thursday  DishDay = "Torsdag"
	Friday    DishDay = "Fredag"
)

type DishOfTheDay

type DishOfTheDay struct {
	NameOfDay        DishDay  `json:"name_of_day"`
	ShortDescription string   `json:"short_description"`
	Lines            []string `json:"lines"`
}

DishOfTheDay is a struct containing a dish of the day. A slice of lines describes the dish and the short description provides a summary of the main dish

func FetchThisWeeksDishes

func FetchThisWeeksDishes() ([]DishOfTheDay, error)

FetchThisWeeksDishes fetches this weeks dishes using the default fetcher

func FetchThisWeeksDishesWithClient

func FetchThisWeeksDishesWithClient(client *http.Client) ([]DishOfTheDay, error)

FetchThisWeeksDishesWithClient fetches this weeks dishes using the provided client in the fetcher

func FetchThisWeeksDishesWithFetcher

func FetchThisWeeksDishesWithFetcher(fetcher func() ([]byte, error)) ([]DishOfTheDay, error)

FetchThisWeeksDishesWithFetcher fetching, parses and maps this weeks dishes using the provided fetcher

func (DishOfTheDay) IsDishOnTheMenuAt added in v1.0.4

func (dish DishOfTheDay) IsDishOnTheMenuAt(t time.Time) (error, bool)

func (DishOfTheDay) IsDishOnTheMenuToday added in v1.0.4

func (dish DishOfTheDay) IsDishOnTheMenuToday() (error, bool)

type SandsPage

type SandsPage struct {
	Menu string `goquery:"#main > div > section > div > div > div.this-week > div > div"`
}

SandsPage is a struct containing the menu goquery for extracting this weeks menu

func Parse

func Parse(content []byte) (*SandsPage, error)

Parse takes some content and parses it into a SandsPage extracting the menu using the SandsPage goquery

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL