Speiseplan der Mensen der Georg-August-Universität Göttingen
				https://mensa.schneider-hosting.de
			
			
				
			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
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							102 lines
						
					
					
						
							2.1 KiB
						
					
					
				| package main | |
| 
 | |
| import ( | |
| 	"encoding/json" | |
| 	"fmt" | |
| 	"io/ioutil" | |
| 	"os" | |
| 	"strings" | |
| 
 | |
| 	"github.com/PuerkitoBio/goquery" | |
| ) | |
| 
 | |
| type ( | |
| 	// Meal contains the info about a meals | |
| 	Meal struct { | |
| 		Category string `json:"category"` | |
| 		Price    string `json:"price"` | |
| 		Title    string `json:"title"` | |
| 	} | |
| 	// Menu contains all info to menues in a mensa on one day | |
| 	Menu struct { | |
| 		Day   string `json:"day"` | |
| 		Mensa string `json:"mensa"` | |
| 		Meals []Meal `json:"meals"` | |
| 	} | |
| ) | |
| 
 | |
| var ( | |
| 	mensen = []string{ | |
| 		"Zentralmensa", | |
| 		"Nordmensa", | |
| 	} | |
| 	days = []string{ | |
| 		"heute", | |
| 		"morgen", | |
| 		"uebermorgen", | |
| 	} | |
| ) | |
| 
 | |
| func main() { | |
| 	path := "./" | |
| 	if len(os.Args) == 2 { | |
| 		path = os.Args[1] + "/" | |
| 	} | |
| 
 | |
| 	for _, mensa := range mensen { | |
| 		for _, day := range days { | |
| 			menu, err := operate(mensa, day) | |
| 			if err != nil { | |
| 				fmt.Println(err.Error()) | |
| 				return | |
| 			} | |
| 
 | |
| 			content, err := json.Marshal(menu) | |
| 			if err != nil { | |
| 				fmt.Println(err.Error()) | |
| 				return | |
| 			} | |
| 
 | |
| 			err = ioutil.WriteFile(fmt.Sprintf("%s/%s.%s.json", path, strings.ToLower(mensa), day), content, 0644) | |
| 			if err != nil { | |
| 				fmt.Println(err.Error()) | |
| 				return | |
| 			} | |
| 		} | |
| 	} | |
| } | |
| 
 | |
| func writeToFile(content []byte) error { | |
| 	err := ioutil.WriteFile(os.Args[1], content, 0644) | |
| 	if err != nil { | |
| 		return err | |
| 	} | |
| 	return nil | |
| } | |
| 
 | |
| func operate(mensa, day string) (Menu, error) { | |
| 	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)) | |
| 	if err != nil { | |
| 		return Menu{}, err | |
| 	} | |
| 
 | |
| 	return Menu{Day: day, Mensa: mensa, Meals: meals}, nil | |
| } | |
| 
 | |
| func parseHTML(url string) ([]Meal, error) { | |
| 	doc, err := goquery.NewDocument(url) | |
| 	if err != nil { | |
| 		return nil, err | |
| 	} | |
| 
 | |
| 	meals := []Meal{} | |
| 	doc.Find("table tr").Each(func(i int, s *goquery.Selection) { | |
| 		meal := Meal{ | |
| 			Category: s.Find(".gericht_kategorie").Text(), | |
| 			Price:    s.Find(".gericht_preis").Text(), | |
| 			Title:    s.Find(".spalte_gericht").Text(), | |
| 		} | |
| 		meals = append(meals, meal) | |
| 	}) | |
| 	return meals, nil | |
| }
 |