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.

101 lines
2.1 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. path := "./"
  36. if len(os.Args) == 2 {
  37. path = os.Args[1] + "/"
  38. }
  39. for _, mensa := range mensen {
  40. for _, day := range days {
  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.%s.json", path, 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, day string) (Menu, error) {
  67. 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))
  68. if err != nil {
  69. return Menu{}, err
  70. }
  71. return Menu{Day: day, Mensa: mensa, Meals: meals}, nil
  72. }
  73. func parseHTML(url string) ([]Meal, error) {
  74. doc, err := goquery.NewDocument(url)
  75. if err != nil {
  76. return nil, err
  77. }
  78. meals := []Meal{}
  79. doc.Find("table tr").Each(func(i int, s *goquery.Selection) {
  80. meal := Meal{
  81. Category: s.Find(".gericht_kategorie").Text(),
  82. Price: s.Find(".gericht_preis").Text(),
  83. Title: s.Find(".spalte_gericht").Text(),
  84. }
  85. meals = append(meals, meal)
  86. })
  87. return meals, nil
  88. }