Compare commits

...

3 Commits

Author SHA1 Message Date
a7591c9868 Configure restaurant ids and add Immigrant, Selepka and Tao
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2023-08-25 17:43:36 +02:00
07b6b6722d Add id to restaurant 2023-08-25 17:43:15 +02:00
82bf2a1ea4 Added Tao parser 2023-08-25 17:42:51 +02:00
5 changed files with 122 additions and 26 deletions

View File

@ -54,14 +54,17 @@ func refresh(c *gin.Context) {
} }
func main() { func main() {
restaurants = append(restaurants, r.NewFreshRestaurant("http://www.fresh-menu.cz/", "Fresh")) restaurants = append(restaurants, r.NewFreshRestaurant("http://www.fresh-menu.cz/", "Fresh", 1))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/2752-u-drevaka-beergrill.html", "U Dřeváka")) restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/2752-u-drevaka-beergrill.html", "U Dřeváka", 2))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/4116-padagali.html", "Padagali")) restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/4116-padagali.html", "Padagali", 3))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/5448-light-of-india.html", "Light of India")) restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/5448-light-of-india.html", "Light of India", 4))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/2609-pizzeria-al-capone.html", "Al Capone")) restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/2609-pizzeria-al-capone.html", "Al Capone", 5))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/3830-suzies-steak-pub.html", "Suzie's")) restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/3830-suzies-steak-pub.html", "Suzie's", 6))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/6468-diva-bara.html", "Divá Bára")) restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/6468-diva-bara.html", "Divá Bára", 7))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/6695-u-karla.html", "U Karla")) restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/6695-u-karla.html", "U Karla", 8))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/7470-the-immigrant-.html", "The Immigrant", 9))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/2721-selepka.html", "Šelepka", 10))
restaurants = append(restaurants, r.NewTaoRestaurant("https://www.taorestaurant.cz/tydenni_menu/nabidka/", "Tao", 11))
refreshInternal() refreshInternal()
fmt.Println("Initial parsing finished") fmt.Println("Initial parsing finished")

View File

@ -15,17 +15,10 @@ type FreshRestaurant struct {
Restaurant Restaurant
} }
func MakeFreshRestaurant(url string, name string) FreshRestaurant { func NewFreshRestaurant(url string, name string, id int) *FreshRestaurant {
restaurant := FreshRestaurant{}
restaurant.SetDefaultValues()
restaurant.url = url
restaurant.name = name
return restaurant
}
func NewFreshRestaurant(url string, name string) *FreshRestaurant {
restaurant := new(FreshRestaurant) restaurant := new(FreshRestaurant)
restaurant.SetDefaultValues() restaurant.SetDefaultValues()
restaurant.id = id
restaurant.url = url restaurant.url = url
restaurant.name = name restaurant.name = name
return restaurant return restaurant

View File

@ -14,17 +14,10 @@ type MenickaRestaurant struct {
Restaurant Restaurant
} }
func MakeMenickaRestaurant(url string, name string) MenickaRestaurant { func NewMenickaRestaurant(url string, name string, id int) *MenickaRestaurant {
restaurant := MenickaRestaurant{}
restaurant.SetDefaultValues()
restaurant.url = url
restaurant.name = name
return restaurant
}
func NewMenickaRestaurant(url string, name string) *MenickaRestaurant {
restaurant := new(MenickaRestaurant) restaurant := new(MenickaRestaurant)
restaurant.SetDefaultValues() restaurant.SetDefaultValues()
restaurant.id = id
restaurant.url = url restaurant.url = url
restaurant.name = name restaurant.name = name
return restaurant return restaurant

View File

@ -16,6 +16,7 @@ type RestaurantInterface interface {
type Restaurant struct { type Restaurant struct {
RestaurantInterface RestaurantInterface
id int
url string url string
name string name string
menus [7]Menu menus [7]Menu
@ -23,6 +24,7 @@ type Restaurant struct {
} }
type RestaurantJSON struct { type RestaurantJSON struct {
Id int `json:"id"`
Restaurant string `json:"restaurant"` Restaurant string `json:"restaurant"`
DailyMenus []Menu `json:"dailymenus"` DailyMenus []Menu `json:"dailymenus"`
PermanentMeals []Meal `json:"permanentmeals"` PermanentMeals []Meal `json:"permanentmeals"`
@ -55,6 +57,7 @@ func (restaurant *Restaurant) clearMenus() {
func (restaurant *Restaurant) MarshalJSON() ([]byte, error) { func (restaurant *Restaurant) MarshalJSON() ([]byte, error) {
return json.Marshal(&RestaurantJSON{ return json.Marshal(&RestaurantJSON{
Id: restaurant.id,
Restaurant: restaurant.name, Restaurant: restaurant.name,
DailyMenus: restaurant.menus[:], DailyMenus: restaurant.menus[:],
PermanentMeals: restaurant.permanent, PermanentMeals: restaurant.permanent,

104
pkg/restaurants/tao.go Normal file
View File

@ -0,0 +1,104 @@
package restaurants
import (
"fmt"
"net/http"
"strconv"
"strings"
"golang.org/x/net/html"
)
type TaoRestaurant struct {
Restaurant
}
func NewTaoRestaurant(url string, name string, id int) *TaoRestaurant {
restaurant := new(TaoRestaurant)
restaurant.SetDefaultValues()
restaurant.id = id
restaurant.url = url
restaurant.name = name
return restaurant
}
func (restaurant *TaoRestaurant) 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
}
// first occurence of tydenni-menu-div
daily, err := findNodeByClass(doc, "ct-div-block tydenni-menu-div")
if err != nil {
fmt.Printf("Couldn't find content for restaurant \"%s\"\n", restaurant.name)
return
}
for menu := daily; menu != nil; menu = menu.NextSibling {
nameNode, err := findNodeByClass(menu, "ct-span")
if err != nil {
continue
}
nameText, err := getText(nameNode)
if err != nil {
continue
}
textElements := strings.Split(nameText, "..")
meal := textElements[0]
priceText := textElements[len(textElements)-1]
priceNum := -1
soup := false
if len(priceText) < 2 {
soup = true
} else {
priceNum, err = strconv.Atoi(strings.TrimLeft(strings.TrimSpace(strings.Split(priceText, "k")[0]), "."))
if err != nil {
priceNum = -1
}
}
restaurant.AddPermanent(soup, strings.TrimSpace(meal), "", priceNum)
}
special := daily.Parent.NextSibling.NextSibling
for i := 0; i < 5; i++ {
nameNode, err := findNodeByClass(special, "ct-span")
if err != nil {
continue
}
nameText, err := getText(nameNode)
if err != nil {
continue
}
textElements := strings.Split(nameText, "..")
meal := textElements[0]
priceText := textElements[len(textElements)-1]
priceNum := -1
soup := false
if len(priceText) == 0 {
soup = true
} else {
priceNum, err = strconv.Atoi(strings.TrimLeft(strings.TrimSpace(strings.Split(priceText, "k")[0]), "."))
if err != nil {
priceNum = -1
}
}
restaurant.menus[i].Add(soup, strings.TrimSpace(meal), "", priceNum)
special = special.NextSibling
}
restaurant.menus[0].SetDay("Monday")
restaurant.menus[1].SetDay("Tuesday")
restaurant.menus[2].SetDay("Wednesday")
restaurant.menus[3].SetDay("Thursday")
restaurant.menus[4].SetDay("Friday")
restaurant.menus[5].SetDay("Saturday")
restaurant.menus[6].SetDay("Sunday")
}