1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package main
import (
"fmt"
"regexp"
"strings"
"github.com/gocolly/colly"
)
const (
cheeseboardURL = "https://cheeseboardcollective.coop/pizza/"
)
var (
boilerPlateParBake = regexp.MustCompile(`Parbake pizza is available in the bakery from \d a.m. to \d p.m., while supplies last.`)
boilerPlateParBakeLunch = regexp.MustCompile(`Parbake pizza is available in the bakery from \d a.m. to \d p.m..Lunch from 1\d:30-\d:30 and dinner from \d-\d.`)
boilerPlateNoHotPizza = regexp.MustCompile(`No hot pizza today.`)
cheeseboardIsClosed = regexp.MustCompile(`The Pizzeria is closed today`)
)
type Pizza struct {
Date string
Toppings string
}
func main() {
c := colly.NewCollector()
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
var pizzas []Pizza
c.OnHTML("div.pizza-list", func(e *colly.HTMLElement) {
e.ForEach("article", func(c int, el *colly.HTMLElement) {
var pizza Pizza
pizza.Date = el.ChildText("div.date")
toppings := el.ChildText("div.menu > p:nth-child(2)")
if cheeseboardIsClosed.MatchString(toppings) {
return
}
toppings = boilerPlateParBake.ReplaceAllString(toppings, "")
toppings = boilerPlateParBakeLunch.ReplaceAllString(toppings, "")
toppings = boilerPlateNoHotPizza.ReplaceAllString(toppings, "")
toppings = strings.TrimLeft(toppings, " ")
pizza.Toppings = toppings
pizzas = append(pizzas, pizza)
})
})
c.Visit(cheeseboardURL)
for _, pizza := range pizzas {
fmt.Printf("%s: %s\n", pizza.Date, pizza.Toppings)
}
}
|