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.

119 lines
2.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "regexp"
  8. "strings"
  9. "time"
  10. "github.com/PuerkitoBio/goquery"
  11. )
  12. type (
  13. // Meal contains the info about a meals
  14. Meal struct {
  15. Category string `json:"category"`
  16. Price string `json:"price"`
  17. Title string `json:"title"`
  18. }
  19. // Menu contains all info to menues in a mensa on one day
  20. Menu struct {
  21. Day int `json:"day"`
  22. Mensa string `json:"mensa"`
  23. Meals []Meal `json:"meals"`
  24. Date string `json:"date"`
  25. }
  26. )
  27. var (
  28. mensen = map[string]string{
  29. "Zentralmensa": "zentralmensa",
  30. "Nordmensa": "nordmensa",
  31. "Mensa+am+Turm": "turmmensa",
  32. "Mensa+Italia": "mensaitalia",
  33. "Bistro+HAWK": "bistrohawk",
  34. }
  35. trimRegex = regexp.MustCompile(`\s{2,}`)
  36. )
  37. func main() {
  38. path := "./"
  39. if len(os.Args) == 2 {
  40. path = os.Args[1] + "/"
  41. }
  42. for mensa := range mensen {
  43. for day := 1; day < 8; day = day + 1 {
  44. menu, err := operate(mensa, day)
  45. if err != nil {
  46. fmt.Println(err.Error())
  47. return
  48. }
  49. content, err := json.Marshal(menu)
  50. if err != nil {
  51. fmt.Println(err.Error())
  52. return
  53. }
  54. err = ioutil.WriteFile(fmt.Sprintf("%s/%s.%v.json", path, mensen[mensa], day), content, 0644)
  55. if err != nil {
  56. fmt.Println(err.Error())
  57. return
  58. }
  59. }
  60. }
  61. }
  62. func writeToFile(content []byte) error {
  63. err := ioutil.WriteFile(os.Args[1], content, 0644)
  64. if err != nil {
  65. return err
  66. }
  67. return nil
  68. }
  69. func operate(mensa string, day int) (Menu, error) {
  70. currentDay := int(time.Now().Weekday()) // week starts on sunday in go, but thats not relevant, because the canteen is closed on sunday
  71. push := 0
  72. if currentDay > day {
  73. push = 1
  74. }
  75. document, err := goquery.NewDocument(fmt.Sprintf("http://www.studentenwerk-goettingen.de/speiseplan.html?no_cache=1&day=%v&push=%v&selectmensa=%s", day, push, mensa))
  76. if err != nil {
  77. return Menu{}, err
  78. }
  79. meals, err := parseMeals(document)
  80. if err != nil {
  81. return Menu{}, err
  82. }
  83. date := parseDate(document)
  84. return Menu{Day: day, Mensa: mensa, Meals: meals, Date: date}, nil
  85. }
  86. func parseDate(doc *goquery.Document) string {
  87. return strip(doc.Find("#speise-head2_datum").Text())
  88. }
  89. func strip(old string) string {
  90. return strings.TrimSpace(trimRegex.ReplaceAllString(old, " "))
  91. }
  92. func parseMeals(doc *goquery.Document) ([]Meal, error) {
  93. meals := []Meal{}
  94. doc.Find("table.speise-tblmain tbody tr").Each(func(i int, s *goquery.Selection) {
  95. meal := Meal{
  96. Category: strip(s.Find(".ext_sits_preis").Text()), // yes, really preis
  97. Title: strip(s.Find(".ext_sits_essen").Text()),
  98. }
  99. meals = append(meals, meal)
  100. })
  101. return meals, nil
  102. }