forked from schneider/mensa
Schneider
7 years ago
1 changed files with 87 additions and 87 deletions
-
174server/server.go
@ -1,119 +1,119 @@ |
|||||
package main |
package main |
||||
|
|
||||
import ( |
import ( |
||||
"encoding/json" |
|
||||
"fmt" |
|
||||
"io/ioutil" |
|
||||
"os" |
|
||||
"regexp" |
|
||||
"strings" |
|
||||
"time" |
|
||||
|
|
||||
"github.com/PuerkitoBio/goquery" |
|
||||
|
"encoding/json" |
||||
|
"fmt" |
||||
|
"io/ioutil" |
||||
|
"os" |
||||
|
"regexp" |
||||
|
"strings" |
||||
|
"time" |
||||
|
|
||||
|
"github.com/PuerkitoBio/goquery" |
||||
) |
) |
||||
|
|
||||
type ( |
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"` |
|
||||
Date string `json:"date"` |
|
||||
} |
|
||||
|
// 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"` |
||||
|
Date string `json:"date"` |
||||
|
} |
||||
) |
) |
||||
|
|
||||
var ( |
var ( |
||||
mensen = map[string]string{ |
|
||||
"Zentralmensa": "zentralmensa", |
|
||||
"Nordmensa": "nordmensa", |
|
||||
"Mensa+am+Turm": "turmmensa", |
|
||||
"Mensa+Italia": "mensaitalia", |
|
||||
"Bistro+HAWK": "bistrohawk", |
|
||||
} |
|
||||
trimRegex = regexp.MustCompile(`\s{2,}`) |
|
||||
|
mensen = map[string]string{ |
||||
|
"Zentralmensa": "zentralmensa", |
||||
|
"Nordmensa": "nordmensa", |
||||
|
"Mensa+am+Turm": "turmmensa", |
||||
|
"Mensa+Italia": "mensaitalia", |
||||
|
"Bistro+HAWK": "bistrohawk", |
||||
|
} |
||||
|
trimRegex = regexp.MustCompile(`\s{2,}`) |
||||
) |
) |
||||
|
|
||||
func main() { |
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, mensen[mensa], day), content, 0644) |
|
||||
if err != nil { |
|
||||
fmt.Println(err.Error()) |
|
||||
return |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
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, mensen[mensa], day), content, 0644) |
||||
|
if err != nil { |
||||
|
fmt.Println(err.Error()) |
||||
|
return |
||||
|
} |
||||
|
} |
||||
|
} |
||||
} |
} |
||||
|
|
||||
func writeToFile(content []byte) error { |
func writeToFile(content []byte) error { |
||||
err := ioutil.WriteFile(os.Args[1], content, 0644) |
|
||||
if err != nil { |
|
||||
return err |
|
||||
} |
|
||||
return nil |
|
||||
|
err := ioutil.WriteFile(os.Args[1], content, 0644) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
return nil |
||||
} |
} |
||||
|
|
||||
func operate(mensa string, day int) (Menu, error) { |
func operate(mensa string, day int) (Menu, error) { |
||||
currentDay := int(time.Now().Weekday()) // week starts on sunday in go, but thats not relevant, because the canteen is closed on sunday
|
|
||||
push := 0 |
|
||||
if currentDay > day { |
|
||||
push = 1 |
|
||||
} |
|
||||
|
currentDay := int(time.Now().Weekday()) // week starts on sunday in go, but thats not relevant, because the canteen is closed on sunday
|
||||
|
push := 0 |
||||
|
if currentDay > day { |
||||
|
push = 1 |
||||
|
} |
||||
|
|
||||
document, err := goquery.NewDocument(fmt.Sprintf("http://www.studentenwerk-goettingen.de/speiseplan.html?no_cache=1&day=%v&push=%v&selectmensa=%s", day, push, mensa)) |
|
||||
|
document, err := goquery.NewDocument(fmt.Sprintf("http://www.studentenwerk-goettingen.de/speiseplan.html?no_cache=1&day=%v&push=%v&selectmensa=%s", day, push, mensa)) |
||||
|
|
||||
if err != nil { |
|
||||
return Menu{}, err |
|
||||
} |
|
||||
|
if err != nil { |
||||
|
return Menu{}, err |
||||
|
} |
||||
|
|
||||
meals, err := parseMeals(document) |
|
||||
if err != nil { |
|
||||
return Menu{}, err |
|
||||
} |
|
||||
|
meals, err := parseMeals(document) |
||||
|
if err != nil { |
||||
|
return Menu{}, err |
||||
|
} |
||||
|
|
||||
date := parseDate(document) |
|
||||
return Menu{Day: day, Mensa: mensa, Meals: meals, Date: date}, nil |
|
||||
|
date := parseDate(document) |
||||
|
return Menu{Day: day, Mensa: mensa, Meals: meals, Date: date}, nil |
||||
} |
} |
||||
|
|
||||
func parseDate(doc *goquery.Document) string { |
func parseDate(doc *goquery.Document) string { |
||||
return strip(doc.Find("#speise-head2_datum").Text()) |
|
||||
|
return strip(doc.Find("#speise-head2_datum").Text()) |
||||
} |
} |
||||
|
|
||||
func strip(old string) string { |
func strip(old string) string { |
||||
return strings.TrimSpace(trimRegex.ReplaceAllString(old, " ")) |
|
||||
|
return strings.TrimSpace(trimRegex.ReplaceAllString(old, " ")) |
||||
} |
} |
||||
|
|
||||
func parseMeals(doc *goquery.Document) ([]Meal, error) { |
func parseMeals(doc *goquery.Document) ([]Meal, error) { |
||||
meals := []Meal{} |
|
||||
doc.Find("table.speise-tblmain tbody tr").Each(func(i int, s *goquery.Selection) { |
|
||||
meal := Meal{ |
|
||||
Category: strip(s.Find(".ext_sits_preis").Text()), // yes, really preis
|
|
||||
Title: strip(s.Find(".ext_sits_essen").Text()), |
|
||||
} |
|
||||
meals = append(meals, meal) |
|
||||
}) |
|
||||
return meals, nil |
|
||||
|
meals := []Meal{} |
||||
|
doc.Find("table.speise-tblmain tbody tr").Each(func(i int, s *goquery.Selection) { |
||||
|
meal := Meal{ |
||||
|
Category: strip(s.Find(".ext_sits_preis").Text()), // yes, really preis
|
||||
|
Title: strip(s.Find(".ext_sits_essen").Text()), |
||||
|
} |
||||
|
meals = append(meals, meal) |
||||
|
}) |
||||
|
return meals, nil |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue