Browse Source

Scrape all mensen

vuex
Schneider 8 years ago
parent
commit
a1fdb1db13
  1. 23
      server/server.go

23
server/server.go

@ -26,9 +26,12 @@ type (
)
var (
mensen = []string{
"Zentralmensa",
"Nordmensa",
mensen = map[string]string{
"Zentralmensa": "zentralmensa",
"Nordmensa": "nordmensa",
"Mensa+am+Turm": "turmmensa",
"Mensa+Italia": "mensaitalia",
"Bistro+HAWK": "bistrohawk",
}
)
@ -38,7 +41,7 @@ func main() {
path = os.Args[1] + "/"
}
for _, mensa := range mensen {
for mensa := range mensen {
for day := 1; day < 8; day = day + 1 {
menu, err := operate(mensa, day)
if err != nil {
@ -52,7 +55,7 @@ func main() {
return
}
err = ioutil.WriteFile(fmt.Sprintf("%s/%s.%v.json", path, strings.ToLower(mensa), day), content, 0644)
err = ioutil.WriteFile(fmt.Sprintf("%s/%s.%v.json", path, mensen[mensa], day), content, 0644)
if err != nil {
fmt.Println(err.Error())
return
@ -78,6 +81,12 @@ func operate(mensa string, day int) (Menu, error) {
return Menu{Day: day, Mensa: mensa, Meals: meals}, nil
}
func strip(old string) string {
new := strings.Replace(old, "\t", "", -1)
new = strings.Replace(new, "\n", "", -1)
return new
}
func parseHTML(url string) ([]Meal, error) {
doc, err := goquery.NewDocument(url)
if err != nil {
@ -87,8 +96,8 @@ func parseHTML(url string) ([]Meal, error) {
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(),
Category: strip(s.Find(".ext_sits_preis").Text()), // yes, really preis
Title: strip(s.Find(".ext_sits_essen").Text()),
}
meals = append(meals, meal)
})

Loading…
Cancel
Save