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.

96 lines
1.9 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. }
  23. )
  24. var (
  25. mensen = []string{
  26. "Zentralmensa",
  27. "Nordmensa",
  28. }
  29. )
  30. func main() {
  31. path := "./"
  32. if len(os.Args) == 2 {
  33. path = os.Args[1] + "/"
  34. }
  35. for _, mensa := range mensen {
  36. for day := 1; day < 8; day = day + 1 {
  37. menu, err := operate(mensa, day)
  38. if err != nil {
  39. fmt.Println(err.Error())
  40. return
  41. }
  42. content, err := json.Marshal(menu)
  43. if err != nil {
  44. fmt.Println(err.Error())
  45. return
  46. }
  47. err = ioutil.WriteFile(fmt.Sprintf("%s/%s.%v.json", path, strings.ToLower(mensa), day), content, 0644)
  48. if err != nil {
  49. fmt.Println(err.Error())
  50. return
  51. }
  52. }
  53. }
  54. }
  55. func writeToFile(content []byte) error {
  56. err := ioutil.WriteFile(os.Args[1], content, 0644)
  57. if err != nil {
  58. return err
  59. }
  60. return nil
  61. }
  62. func operate(mensa string, day int) (Menu, error) {
  63. meals, err := parseHTML(fmt.Sprintf("http://www.studentenwerk-goettingen.de/speiseplan.html?no_cache=1&day=%vpush=0&selectmensa=%s", day, mensa))
  64. if err != nil {
  65. return Menu{}, err
  66. }
  67. return Menu{Day: day, Mensa: mensa, Meals: meals}, nil
  68. }
  69. func parseHTML(url string) ([]Meal, error) {
  70. doc, err := goquery.NewDocument(url)
  71. if err != nil {
  72. return nil, err
  73. }
  74. meals := []Meal{}
  75. doc.Find("table.speise-tblmain tbody tr").Each(func(i int, s *goquery.Selection) {
  76. meal := Meal{
  77. Category: s.Find(".ext_sits_preis").Text(), // yes, really preis
  78. Title: s.Find(".ext_sits_essen").Text(),
  79. }
  80. meals = append(meals, meal)
  81. })
  82. return meals, nil
  83. }