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