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.

100 lines
2.0 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "github.com/PuerkitoBio/goquery"
  8. )
  9. type (
  10. // Meal contains the info about a meals
  11. Meal struct {
  12. Category string `json:"category"`
  13. Price string `json:"price"`
  14. Title string `json:"title"`
  15. }
  16. // Menu contains all info to menues in a mensa on one day
  17. Menu struct {
  18. Day string `json:"day"`
  19. Mensa string `json:"mensa"`
  20. Meals []Meal `json:"meals"`
  21. }
  22. )
  23. var (
  24. mensen = []string{
  25. "Zentralmensa",
  26. "Nordmensa",
  27. }
  28. days = []string{
  29. "heute",
  30. "morgen",
  31. "uebermorgen",
  32. }
  33. )
  34. func main() {
  35. menus := []Menu{}
  36. for _, mensa := range mensen {
  37. for _, day := range days {
  38. menu, err := operate(mensa, day)
  39. if err != nil {
  40. fmt.Println(err.Error())
  41. }
  42. menus = append(menus, menu)
  43. }
  44. }
  45. bytes, err := json.Marshal(menus)
  46. if err != nil {
  47. fmt.Println(err.Error())
  48. }
  49. if len(os.Args) == 2 {
  50. err = writeToFile(bytes)
  51. if err != nil {
  52. fmt.Println(err.Error())
  53. }
  54. } else {
  55. fmt.Println(string(bytes))
  56. }
  57. }
  58. func writeToFile(content []byte) error {
  59. err := ioutil.WriteFile(os.Args[1], content, 0644)
  60. if err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. func operate(mensa, day string) (Menu, error) {
  66. 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))
  67. if err != nil {
  68. return Menu{}, err
  69. }
  70. return Menu{Day: day, Mensa: mensa, Meals: meals}, nil
  71. }
  72. func parseHTML(url string) ([]Meal, error) {
  73. doc, err := goquery.NewDocument(url)
  74. if err != nil {
  75. return nil, err
  76. }
  77. meals := []Meal{}
  78. doc.Find("table tr").Each(func(i int, s *goquery.Selection) {
  79. meal := Meal{
  80. Category: s.Find(".gericht_kategorie").Text(),
  81. Price: s.Find(".gericht_preis").Text(),
  82. Title: s.Find(".spalte_gericht").Text(),
  83. }
  84. meals = append(meals, meal)
  85. })
  86. return meals, nil
  87. }