-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (111 loc) · 3.62 KB
/
main.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/gocolly/colly/v2"
)
//Ride details
type Ride struct {
Park string `json:"park"`
Name string `json:"name"`
ShortDescription string `json:"description"`
ImageURL string `json:"image"`
HeightRequirementInInches string `json:"height"`
RideURL string `json:"url"`
Tags []string `json:"tags"`
}
//Rides represents slice of Ride structs
type Rides []Ride
// POC - most likely websites will contain a single page with all info
// OR we will have to go to each ride page to get info
// this could be made generic with Config args for park, url, query selectors for each attribute
func buschgardens(c colly.Collector) []Ride {
var ridesList Rides
parkName := "Busch Gardens Williamsburg"
c.OnHTML("#page-content > div > div > ul", func(e *colly.HTMLElement) {
e.ForEach("li", func(_ int, el *colly.HTMLElement) {
image := e.Request.URL.String() + el.ChildAttr("span > a > img", "src")
name := el.ChildText("div > h2 > a")
shortDescription := el.ChildText("div > p")
tags := el.ChildTexts("div > ul > li")
if name != "" {
//follow link
rideURL := e.Request.URL.Scheme + "://" + e.Request.URL.Host + e.Request.URL.Opaque + el.ChildAttr("span > a", "href")
c2 := colly.NewCollector()
c2.OnHTML("#page-content", func(e2 *colly.HTMLElement) {
height := e2.ChildText("#page-content > div.container > div > div.col-sm-8 > div > ul > li:nth-child(1) > dl > dd")
height = strings.TrimSuffix(height, "\"")
ridesList = append(ridesList, Ride{Park: parkName, Name: name, ImageURL: image, ShortDescription: shortDescription, Tags: tags, HeightRequirementInInches: height, RideURL: rideURL})
})
c2.Visit(rideURL)
}
})
})
c.Visit("https://buschgardens.com/williamsburg/rides")
return ridesList
}
func main() {
c := colly.NewCollector()
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.OnError(func(_ *colly.Response, err error) {
log.Println("Something went wrong:", err)
})
//make generic with interface to collect ride info (extract) and write
bgRidesList := buschgardens(*c)
filenameJSON := write(bgRidesList)
writeCSV(filenameJSON)
}
func write(r Rides) string {
var fileName strings.Builder
ridesJSON, err := json.MarshalIndent(r, "", " ")
if err != nil {
log.Fatal("Cannot encode to JSON ", err)
}
fmt.Println(string(ridesJSON))
fileName.WriteString("rides-")
fileName.WriteString(time.Now().Format("20060102150405"))
fileName.WriteString(".json")
_ = ioutil.WriteFile(fileName.String(), ridesJSON, 0644)
return fileName.String()
}
func writeCSV(f string) {
jsonDataFromFile, err := ioutil.ReadFile(f)
if err != nil {
fmt.Println(err)
}
// Unmarshal JSON data
var jsonData []Ride
err = json.Unmarshal([]byte(jsonDataFromFile), &jsonData)
if err != nil {
fmt.Println(err)
}
csvFile, err := os.Create("./rides.csv")
if err != nil {
fmt.Println(err)
}
defer csvFile.Close()
writer := csv.NewWriter(csvFile)
headerRow := []string{"park", "name", "description", "image", "url", "height", "tags"}
writer.Write(headerRow)
for _, ride := range jsonData {
var row []string
row = append(row, ride.Park)
row = append(row, ride.Name)
row = append(row, ride.ShortDescription)
row = append(row, ride.ImageURL)
row = append(row, ride.RideURL)
row = append(row, ride.HeightRequirementInInches)
row = append(row, strings.Join(ride.Tags, " "))
writer.Write(row)
}
// remember to flush!
writer.Flush()
}