Added Fresh restaurant
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
package restaurants
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.sajari.com/docconv"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
type FreshRestaurant struct {
|
||||
Restaurant
|
||||
}
|
||||
|
||||
func MakeFreshRestaurant(url string, name string) FreshRestaurant {
|
||||
restaurant := FreshRestaurant{}
|
||||
restaurant.url = url
|
||||
restaurant.name = name
|
||||
return restaurant
|
||||
}
|
||||
|
||||
func NewFreshRestaurant(url string, name string) *FreshRestaurant {
|
||||
restaurant := new(FreshRestaurant)
|
||||
restaurant.url = url
|
||||
restaurant.name = name
|
||||
return restaurant
|
||||
}
|
||||
|
||||
func (restaurant *FreshRestaurant) 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
|
||||
}
|
||||
|
||||
link, err := findNodeById(doc, "menu-item-519")
|
||||
if err != nil {
|
||||
fmt.Printf("Couldn't find menu node for restaurant \"%s\"\n", restaurant.name)
|
||||
return
|
||||
}
|
||||
linkAddress, err := getAttribute(link.FirstChild, "href")
|
||||
if err != nil {
|
||||
fmt.Printf("Couldn't get PDF link for restaurant \"%s\"\n", restaurant.name)
|
||||
return
|
||||
}
|
||||
pdf, err := http.Get(linkAddress)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer pdf.Body.Close()
|
||||
pdftxt, err := docconv.Convert(pdf.Body, "application/pdf", true)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
meals := [5][]string{}
|
||||
prices := [5][]int{}
|
||||
scanner := bufio.NewScanner(strings.NewReader(pdftxt.Body))
|
||||
curIndex := -1
|
||||
pricesIndex := -1
|
||||
pricesSection := false
|
||||
emptyLine := false
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if len(line) == 0 {
|
||||
emptyLine = true
|
||||
pricesSection = false
|
||||
continue
|
||||
}
|
||||
if strings.Contains(line, "Kč") && emptyLine {
|
||||
pricesIndex++
|
||||
pricesSection = true
|
||||
}
|
||||
emptyLine = false
|
||||
// 10 because soup name starts after 10 and vacations have no soup provided
|
||||
if !pricesSection && len(line) > 10 && line[:3] == "Pol" {
|
||||
curIndex++
|
||||
meals[curIndex] = append(meals[curIndex], line[10:])
|
||||
prices[curIndex] = append(prices[curIndex], -1)
|
||||
}
|
||||
if !pricesSection && line[1] == '.' && len(line) > 2 {
|
||||
meals[curIndex] = append(meals[curIndex], line[3:])
|
||||
}
|
||||
if pricesSection && len(line) != 0 {
|
||||
priceInt, err := strconv.Atoi(strings.Split(line, " ")[0])
|
||||
if err != nil {
|
||||
priceInt = -1
|
||||
}
|
||||
prices[pricesIndex] = append(prices[pricesIndex], priceInt)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
if len(meals[i]) == 0 || len(prices[i]) == 0 {
|
||||
continue
|
||||
}
|
||||
for ind, meal := range meals[i] {
|
||||
if ind >= len(prices) {
|
||||
break
|
||||
}
|
||||
restaurant.menus[i].Add(ind == 0, meal, "", prices[i][ind])
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
+16
-8
@@ -17,28 +17,36 @@ func getAttribute(node *html.Node, key string) (string, error) {
|
||||
return "", errors.New("couldn't find the provided key")
|
||||
}
|
||||
|
||||
func hasClass(node *html.Node, class string) bool {
|
||||
func hasKeyValue(node *html.Node, key string, value string) bool {
|
||||
if node.Type == html.ElementNode {
|
||||
c, err := getAttribute(node, "class")
|
||||
attr, err := getAttribute(node, key)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return c == class
|
||||
return attr == value
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func findNodeByClass(node *html.Node, class string) (*html.Node, error) {
|
||||
if hasClass(node, class) {
|
||||
func findNodeBy(node *html.Node, key string, value string) (*html.Node, error) {
|
||||
if hasKeyValue(node, key, value) {
|
||||
return node, nil
|
||||
}
|
||||
for n := node.FirstChild; n != nil; n = n.NextSibling {
|
||||
c, err := findNodeByClass(n, class)
|
||||
res, err := findNodeBy(n, key, value)
|
||||
if err == nil {
|
||||
return c, nil
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("couldn't find a node with provided class")
|
||||
return nil, errors.New("couldn't find a node with provided " + key + " \"" + value + "\"")
|
||||
}
|
||||
|
||||
func findNodeByClass(node *html.Node, class string) (*html.Node, error) {
|
||||
return findNodeBy(node, "class", class)
|
||||
}
|
||||
|
||||
func findNodeById(node *html.Node, id string) (*html.Node, error) {
|
||||
return findNodeBy(node, "id", id)
|
||||
}
|
||||
|
||||
func getTextInternal(node *html.Node) (string, error) {
|
||||
|
||||
@@ -66,7 +66,7 @@ func (restaurant *MenickaRestaurant) Parse() {
|
||||
return
|
||||
}
|
||||
for menu := content.FirstChild; menu != nil; menu = menu.NextSibling {
|
||||
if hasClass(menu, "menicka") {
|
||||
if hasKeyValue(menu, "class", "menicka") {
|
||||
day, err := findNodeByClass(menu, "nadpis")
|
||||
if err != nil {
|
||||
continue
|
||||
@@ -107,7 +107,7 @@ func (restaurant *MenickaRestaurant) Parse() {
|
||||
price = -1
|
||||
}
|
||||
}
|
||||
if hasClass(meal, "polevka") {
|
||||
if hasKeyValue(meal, "class", "polevka") {
|
||||
restaurant.menus[dayIndex].Add(true, strings.TrimSpace(name), "", price)
|
||||
} else {
|
||||
restaurant.menus[dayIndex].Add(false, strings.TrimSpace(name), "", price)
|
||||
@@ -115,4 +115,11 @@ func (restaurant *MenickaRestaurant) Parse() {
|
||||
}
|
||||
}
|
||||
}
|
||||
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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user