forked from schneider/mensa
Schneider
8 years ago
1 changed files with 100 additions and 0 deletions
-
100server/server.go
@ -0,0 +1,100 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"encoding/json" |
||||
|
"fmt" |
||||
|
"io/ioutil" |
||||
|
"os" |
||||
|
|
||||
|
"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 string `json:"day"` |
||||
|
Mensa string `json:"mensa"` |
||||
|
Meals []Meal `json:"meals"` |
||||
|
} |
||||
|
) |
||||
|
|
||||
|
var ( |
||||
|
mensen = []string{ |
||||
|
"Zentralmensa", |
||||
|
"Nordmensa", |
||||
|
} |
||||
|
days = []string{ |
||||
|
"heute", |
||||
|
"morgen", |
||||
|
"uebermorgen", |
||||
|
} |
||||
|
) |
||||
|
|
||||
|
func main() { |
||||
|
menus := []Menu{} |
||||
|
|
||||
|
for _, mensa := range mensen { |
||||
|
for _, day := range days { |
||||
|
menu, err := operate(mensa, day) |
||||
|
if err != nil { |
||||
|
fmt.Println(err.Error()) |
||||
|
} |
||||
|
menus = append(menus, menu) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
bytes, err := json.Marshal(menus) |
||||
|
if err != nil { |
||||
|
fmt.Println(err.Error()) |
||||
|
} |
||||
|
|
||||
|
if len(os.Args) == 2 { |
||||
|
err = writeToFile(bytes) |
||||
|
if err != nil { |
||||
|
fmt.Println(err.Error()) |
||||
|
} |
||||
|
} else { |
||||
|
fmt.Println(string(bytes)) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func writeToFile(content []byte) error { |
||||
|
err := ioutil.WriteFile(os.Args[1], content, 0644) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
func operate(mensa, day string) (Menu, error) { |
||||
|
meals, err := parseHTML(fmt.Sprintf("https://phpapp2.zvw.uni-goettingen.de/portlet_mensaplan/public/ajaxresponse/getgerichte?mensa=%s&tag=%s&preis=stu&vegetarisch=true&schweinefleisch=true&vegan=true&fisch=true&gefluegel=true&rind=true&wild=true&lamm=true&alkohol=true&knoblauch=true&sonstige_gerichte=true", mensa, day)) |
||||
|
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 tr").Each(func(i int, s *goquery.Selection) { |
||||
|
meal := Meal{ |
||||
|
Category: s.Find(".gericht_kategorie").Text(), |
||||
|
Price: s.Find(".gericht_preis").Text(), |
||||
|
Title: s.Find(".spalte_gericht").Text(), |
||||
|
} |
||||
|
meals = append(meals, meal) |
||||
|
}) |
||||
|
return meals, nil |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue