Initial working version
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package restaurants
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"golang.org/x/text/encoding/charmap"
|
||||
)
|
||||
|
||||
func getAttribute(node *html.Node, key string) (string, error) {
|
||||
for _, attr := range node.Attr {
|
||||
if attr.Key == key {
|
||||
return attr.Val, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.New("couldn't find the provided key")
|
||||
}
|
||||
|
||||
func hasClass(node *html.Node, class string) bool {
|
||||
if node.Type == html.ElementNode {
|
||||
c, err := getAttribute(node, "class")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return c == class
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func findNodeByClass(node *html.Node, class string) (*html.Node, error) {
|
||||
if hasClass(node, class) {
|
||||
return node, nil
|
||||
}
|
||||
for n := node.FirstChild; n != nil; n = n.NextSibling {
|
||||
c, err := findNodeByClass(n, class)
|
||||
if err == nil {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("couldn't find a node with provided class")
|
||||
}
|
||||
|
||||
func getTextInternal(node *html.Node) (string, error) {
|
||||
if node.Type == html.TextNode {
|
||||
return node.Data, nil
|
||||
}
|
||||
return "", errors.New("not a text node")
|
||||
}
|
||||
|
||||
func getText(node *html.Node) (string, error) {
|
||||
if node.Type == html.TextNode {
|
||||
return node.Data, nil
|
||||
}
|
||||
for n := node.FirstChild; n != nil; n = n.NextSibling {
|
||||
text, err := getTextInternal(n)
|
||||
if err == nil {
|
||||
return text, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("couldn't find a text node")
|
||||
}
|
||||
|
||||
func getTextDecodeWindows1250(node *html.Node) (string, error) {
|
||||
text, err := getText(node)
|
||||
if err != nil {
|
||||
return text, err
|
||||
}
|
||||
return decodeWindows1250(text)
|
||||
}
|
||||
|
||||
func decodeWindows1250(text string) (string, error) {
|
||||
dec := charmap.Windows1250.NewDecoder()
|
||||
out, err := dec.String(text)
|
||||
return out, err
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package restaurants
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type Meal struct {
|
||||
isSoup bool
|
||||
name string
|
||||
desc string
|
||||
price int
|
||||
}
|
||||
|
||||
func MakeMeal(isSoup bool, name string, desc string, price int) Meal {
|
||||
return Meal{isSoup, name, desc, price}
|
||||
}
|
||||
|
||||
func (meal Meal) IsSoup() bool {
|
||||
return meal.isSoup
|
||||
}
|
||||
|
||||
func (meal Meal) GetName() string {
|
||||
return meal.name
|
||||
}
|
||||
|
||||
func (meal Meal) GetDescription() string {
|
||||
return meal.desc
|
||||
}
|
||||
|
||||
func (meal Meal) GetPrice() int {
|
||||
return meal.price
|
||||
}
|
||||
|
||||
func (meal *Meal) SetSoup(isSoup bool) {
|
||||
meal.isSoup = isSoup
|
||||
}
|
||||
|
||||
func (meal *Meal) SetName(name string) {
|
||||
meal.name = name
|
||||
}
|
||||
|
||||
func (meal *Meal) SetDescription(desc string) {
|
||||
meal.desc = desc
|
||||
}
|
||||
|
||||
func (meal *Meal) SetPrice(price int) {
|
||||
meal.price = price
|
||||
}
|
||||
|
||||
func (meal *Meal) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(&struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
IsSoup bool `json:"isSoup"`
|
||||
Price int `json:"price"`
|
||||
}{
|
||||
Name: meal.name,
|
||||
Description: meal.desc,
|
||||
IsSoup: meal.isSoup,
|
||||
Price: meal.price,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package restaurants
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
type MenickaRestaurant struct {
|
||||
Restaurant
|
||||
}
|
||||
|
||||
func MakeMenickaRestaurant(url string, name string) MenickaRestaurant {
|
||||
restaurant := MenickaRestaurant{}
|
||||
restaurant.url = url
|
||||
restaurant.name = name
|
||||
return restaurant
|
||||
}
|
||||
|
||||
func NewMenickaRestaurant(url string, name string) *MenickaRestaurant {
|
||||
restaurant := new(MenickaRestaurant)
|
||||
restaurant.url = url
|
||||
restaurant.name = name
|
||||
return restaurant
|
||||
}
|
||||
|
||||
func dayToIndex(day string) (int, error) {
|
||||
if day == "Pondělí" {
|
||||
return 0, nil
|
||||
} else if day == "Úterý" {
|
||||
return 1, nil
|
||||
} else if day == "Středa" {
|
||||
return 2, nil
|
||||
} else if day == "Čtvrtek" {
|
||||
return 3, nil
|
||||
} else if day == "Pátek" {
|
||||
return 4, nil
|
||||
} else if day == "Sobota" {
|
||||
return 5, nil
|
||||
} else if day == "Neděle" {
|
||||
return 6, nil
|
||||
}
|
||||
return -1, errors.New("couldn't parse the day")
|
||||
}
|
||||
|
||||
func (restaurant *MenickaRestaurant) Parse() {
|
||||
restaurant.clearMenus()
|
||||
resp, err := http.Get(restaurant.url)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
doc, err := html.Parse(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
content, err := findNodeByClass(doc, "obsah")
|
||||
if err != nil {
|
||||
fmt.Printf("Couldn't find content for restaurant \"%s\"\n", restaurant.name)
|
||||
return
|
||||
}
|
||||
for menu := content.FirstChild; menu != nil; menu = menu.NextSibling {
|
||||
if hasClass(menu, "menicka") {
|
||||
day, err := findNodeByClass(menu, "nadpis")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
meals, err := findNodeByClass(menu, "popup-gallery")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
dayText, err := getTextDecodeWindows1250(day)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
dayText = strings.Split(dayText, " ")[0]
|
||||
dayIndex, err := dayToIndex(dayText)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for meal := meals.FirstChild; meal != nil; meal = meal.NextSibling {
|
||||
nameNode, err := findNodeByClass(meal, "polozka")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
name, err := getTextDecodeWindows1250(nameNode)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
price := -1
|
||||
priceNode, err := findNodeByClass(meal, "cena")
|
||||
if err == nil {
|
||||
priceStr, err := getText(priceNode)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
price, err = strconv.Atoi(strings.Split(priceStr, " ")[0])
|
||||
if err != nil {
|
||||
price = -1
|
||||
}
|
||||
}
|
||||
if hasClass(meal, "polevka") {
|
||||
restaurant.menus[dayIndex].Add(true, strings.TrimSpace(name), "", price)
|
||||
} else {
|
||||
restaurant.menus[dayIndex].Add(false, strings.TrimSpace(name), "", price)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package restaurants
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type Menu struct {
|
||||
meals []Meal
|
||||
valid bool
|
||||
day string
|
||||
}
|
||||
|
||||
func MakeMenu(meals []Meal, day string) Menu {
|
||||
return Menu{meals, true, day}
|
||||
}
|
||||
|
||||
func (menu *Menu) Add(isSoup bool, name string, desc string, price int) {
|
||||
menu.AddMeal(MakeMeal(isSoup, name, desc, price))
|
||||
}
|
||||
|
||||
func (menu *Menu) AddMeal(meal Meal) {
|
||||
menu.meals = append(menu.meals, meal)
|
||||
}
|
||||
|
||||
func (menu Menu) GetMeals() []Meal {
|
||||
return menu.meals
|
||||
}
|
||||
|
||||
func (menu *Menu) SetInvalidMenu(invalid bool) {
|
||||
menu.valid = !invalid
|
||||
}
|
||||
|
||||
func (menu *Menu) SetValidMenu(valid bool) {
|
||||
menu.valid = valid
|
||||
}
|
||||
|
||||
func (menu Menu) IsValid() bool {
|
||||
return menu.valid
|
||||
}
|
||||
|
||||
func (menu *Menu) SetDay(day string) {
|
||||
menu.day = day
|
||||
}
|
||||
|
||||
func (menu Menu) GetDay() string {
|
||||
return menu.day
|
||||
}
|
||||
|
||||
func (menu *Menu) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(&struct {
|
||||
Meals []Meal `json:"meals"`
|
||||
Day string `json:"day"`
|
||||
}{
|
||||
Meals: menu.meals,
|
||||
Day: menu.day,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package restaurants
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type RestaurantInterface interface {
|
||||
// public
|
||||
GetMenus() [7]Menu
|
||||
Parse()
|
||||
AddPermanentMeal(meal Meal)
|
||||
MarshalJSON() ([]byte, error)
|
||||
GetSpecificDayObject(days []int) RestaurantJSON
|
||||
|
||||
// private
|
||||
clearMenus()
|
||||
}
|
||||
|
||||
type Restaurant struct {
|
||||
RestaurantInterface
|
||||
url string
|
||||
name string
|
||||
menus [7]Menu
|
||||
permanent []Meal
|
||||
}
|
||||
|
||||
type RestaurantJSON struct {
|
||||
Restaurant string `json:"restaurant"`
|
||||
DailyMenus []Menu `json:"dailymenus"`
|
||||
PermanentMeals []Meal `json:"permanentmeals"`
|
||||
}
|
||||
|
||||
func (restaurant *Restaurant) AddPermanent(isSoup bool, name string, desc string, price int) {
|
||||
restaurant.AddPermanentMeal(MakeMeal(isSoup, name, desc, price))
|
||||
}
|
||||
|
||||
func (restaurant *Restaurant) AddPermanentMeal(meal Meal) {
|
||||
restaurant.permanent = append(restaurant.permanent, meal)
|
||||
}
|
||||
|
||||
func (restaurant Restaurant) GetMenus() [7]Menu {
|
||||
return restaurant.menus
|
||||
}
|
||||
|
||||
func (restaurant *Restaurant) clearMenus() {
|
||||
for i := 0; i < 7; i++ {
|
||||
restaurant.menus[i] = Menu{}
|
||||
}
|
||||
}
|
||||
|
||||
func (restaurant *Restaurant) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(&RestaurantJSON{
|
||||
Restaurant: restaurant.name,
|
||||
DailyMenus: restaurant.menus[:],
|
||||
PermanentMeals: restaurant.permanent,
|
||||
})
|
||||
}
|
||||
|
||||
func (restaurant *Restaurant) GetSpecificDayObject(days []int) RestaurantJSON {
|
||||
obj := RestaurantJSON{
|
||||
Restaurant: restaurant.name,
|
||||
PermanentMeals: restaurant.permanent,
|
||||
}
|
||||
for _, index := range days {
|
||||
obj.DailyMenus = append(obj.DailyMenus, restaurant.menus[index])
|
||||
}
|
||||
return obj
|
||||
}
|
||||
Reference in New Issue
Block a user