|
|
package main
import ( "encoding/json" "fmt" "io/ioutil" "os" "strings"
"github.com/PuerkitoBio/goquery" )
type ( // Meal contains the info about a meals
Meal struct { Category string `json:"category"` Price string `json:"price"` Title string `json:"title"` } // Menu contains all info to menues in a mensa on one day
Menu struct { Day int `json:"day"` Mensa string `json:"mensa"` Meals []Meal `json:"meals"` } )
var ( mensen = []string{ "Zentralmensa", "Nordmensa", } )
func main() { path := "./" if len(os.Args) == 2 { path = os.Args[1] + "/" }
for _, mensa := range mensen { for day := 1; day < 8; day = day + 1 { menu, err := operate(mensa, day) if err != nil { fmt.Println(err.Error()) return }
content, err := json.Marshal(menu) if err != nil { fmt.Println(err.Error()) return }
err = ioutil.WriteFile(fmt.Sprintf("%s/%s.%v.json", path, strings.ToLower(mensa), day), content, 0644) if err != nil { fmt.Println(err.Error()) return } } } }
func writeToFile(content []byte) error { err := ioutil.WriteFile(os.Args[1], content, 0644) if err != nil { return err } return nil }
func operate(mensa string, day int) (Menu, error) { meals, err := parseHTML(fmt.Sprintf("http://www.studentenwerk-goettingen.de/speiseplan.html?no_cache=1&day=%vpush=0&selectmensa=%s", day, mensa)) if err != nil { return Menu{}, err }
return Menu{Day: day, Mensa: mensa, Meals: meals}, nil }
func parseHTML(url string) ([]Meal, error) { doc, err := goquery.NewDocument(url) if err != nil { return nil, err }
meals := []Meal{} doc.Find("table.speise-tblmain tbody tr").Each(func(i int, s *goquery.Selection) { meal := Meal{ Category: s.Find(".ext_sits_preis").Text(), // yes, really preis
Title: s.Find(".ext_sits_essen").Text(), } meals = append(meals, meal) }) return meals, nil }
|