Speiseplan der Mensen der Georg-August-Universität Göttingen https://mensa.schneider-hosting.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.3 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "regexp"
  8. "strings"
  9. "github.com/PuerkitoBio/goquery"
  10. )
  11. type (
  12. // Meal contains the info about a meals
  13. Meal struct {
  14. Category string `json:"category"`
  15. Price string `json:"price"`
  16. Title string `json:"title"`
  17. }
  18. // Menu contains all info to menues in a mensa on one day
  19. Menu struct {
  20. Day int `json:"day"`
  21. Mensa string `json:"mensa"`
  22. Meals []Meal `json:"meals"`
  23. Date string `json:"date"`
  24. }
  25. )
  26. var (
  27. mensen = map[string]string{
  28. "Zentralmensa": "zentralmensa",
  29. "Nordmensa": "nordmensa",
  30. "Mensa+am+Turm": "turmmensa",
  31. "Mensa+Italia": "mensaitalia",
  32. "Bistro+HAWK": "bistrohawk",
  33. }
  34. trimRegex = regexp.MustCompile(`\s{2,}`)
  35. )
  36. func main() {
  37. path := "./"
  38. if len(os.Args) == 2 {
  39. path = os.Args[1] + "/"
  40. }
  41. for mensa := range mensen {
  42. for day := 1; day < 8; day = day + 1 {
  43. menu, err := operate(mensa, day)
  44. if err != nil {
  45. fmt.Println(err.Error())
  46. return
  47. }
  48. content, err := json.Marshal(menu)
  49. if err != nil {
  50. fmt.Println(err.Error())
  51. return
  52. }
  53. err = ioutil.WriteFile(fmt.Sprintf("%s/%s.%v.json", path, mensen[mensa], day), content, 0644)
  54. if err != nil {
  55. fmt.Println(err.Error())
  56. return
  57. }
  58. }
  59. }
  60. }
  61. func writeToFile(content []byte) error {
  62. err := ioutil.WriteFile(os.Args[1], content, 0644)
  63. if err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. func operate(mensa string, day int) (Menu, error) {
  69. document, err := goquery.NewDocument(fmt.Sprintf("http://www.studentenwerk-goettingen.de/speiseplan.html?no_cache=1&day=%vpush=0&selectmensa=%s", day, mensa))
  70. if err != nil {
  71. return Menu{}, err
  72. }
  73. meals, err := parseMeals(document)
  74. if err != nil {
  75. return Menu{}, err
  76. }
  77. date := parseDate(document)
  78. return Menu{Day: day, Mensa: mensa, Meals: meals, Date: date}, nil
  79. }
  80. func parseDate(doc *goquery.Document) string {
  81. return strip(doc.Find("#speise-head2_datum").Text())
  82. }
  83. func strip(old string) string {
  84. return strings.TrimSpace(trimRegex.ReplaceAllString(old, " "))
  85. }
  86. func parseMeals(doc *goquery.Document) ([]Meal, error) {
  87. meals := []Meal{}
  88. doc.Find("table.speise-tblmain tbody tr").Each(func(i int, s *goquery.Selection) {
  89. meal := Meal{
  90. Category: strip(s.Find(".ext_sits_preis").Text()), // yes, really preis
  91. Title: strip(s.Find(".ext_sits_essen").Text()),
  92. }
  93. meals = append(meals, meal)
  94. })
  95. return meals, nil
  96. }