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.

102 lines
2.1 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 string `json:"day"`
  20. Mensa string `json:"mensa"`
  21. Meals []Meal `json:"meals"`
  22. }
  23. )
  24. var (
  25. mensen = []string{
  26. "Zentralmensa",
  27. "Nordmensa",
  28. }
  29. days = []string{
  30. "heute",
  31. "morgen",
  32. "uebermorgen",
  33. }
  34. )
  35. func main() {
  36. path := "./"
  37. if len(os.Args) == 2 {
  38. path = os.Args[1] + "/"
  39. }
  40. for _, mensa := range mensen {
  41. for _, day := range days {
  42. menu, err := operate(mensa, day)
  43. if err != nil {
  44. fmt.Println(err.Error())
  45. return
  46. }
  47. content, err := json.Marshal(menu)
  48. if err != nil {
  49. fmt.Println(err.Error())
  50. return
  51. }
  52. err = ioutil.WriteFile(fmt.Sprintf("%s/%s.%s.json", path, strings.ToLower(mensa), day), content, 0644)
  53. if err != nil {
  54. fmt.Println(err.Error())
  55. return
  56. }
  57. }
  58. }
  59. }
  60. func writeToFile(content []byte) error {
  61. err := ioutil.WriteFile(os.Args[1], content, 0644)
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. func operate(mensa, day string) (Menu, error) {
  68. 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))
  69. if err != nil {
  70. return Menu{}, err
  71. }
  72. return Menu{Day: day, Mensa: mensa, Meals: meals}, nil
  73. }
  74. func parseHTML(url string) ([]Meal, error) {
  75. doc, err := goquery.NewDocument(url)
  76. if err != nil {
  77. return nil, err
  78. }
  79. meals := []Meal{}
  80. doc.Find("table tr").Each(func(i int, s *goquery.Selection) {
  81. meal := Meal{
  82. Category: s.Find(".gericht_kategorie").Text(),
  83. Price: s.Find(".gericht_preis").Text(),
  84. Title: s.Find(".spalte_gericht").Text(),
  85. }
  86. meals = append(meals, meal)
  87. })
  88. return meals, nil
  89. }