-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvz.go
102 lines (84 loc) · 2.22 KB
/
vz.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
package zenfo
import (
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
)
// Vz crawls villagezendo.org, satisfies Worker interface
type Vz struct {
venueMap map[string]*Venue
client *Client
log chan string
}
type villageEventJSON struct {
Name string `json:"title"`
Desc string `json:"description"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
URL string `json:"url"`
Venue string `json:"venue_slug"`
}
// Name return human-friendly name for worker for logs
func (v *Vz) Name() string {
return "Village Zendo (villagezendo.org)"
}
// Init sets HTTP client and defines internal venue map
func (v *Vz) Init(client *Client, log chan string) error {
v.client = client
v.venueMap = make(map[string]*Venue)
v.log = log
v.venueMap["village-zendo"] = &Venue{
Name: "Village Zendo",
Addr: "588 Broadway, Suite 1108, New York, NY 10012",
Phone: "+1 (212) 340-4656",
Email: "info@villagezendo.org",
Lat: 40.724682,
Lng: -73.997087,
Website: "https://villagezendo.org",
}
v.log <- "Initialized!"
return nil
}
// Desc returns description for website crawled
func (v *Vz) Desc() string {
return "Village Zendo (villagezendo.org)"
}
// Events hits JSON API and returns slice of Event types
func (v *Vz) Events() ([]*Event, error) {
u := "https://villagezendo.org/wp-admin/admin-ajax.php?action=eventorganiser-fullcal&start=2019-02-24&timeformat=g%3Ai+a"
resp, err := v.client.Get(u)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var events []villageEventJSON
if err := json.NewDecoder(resp.Body).Decode(&events); err != nil {
return nil, err
}
if len(events) == 0 {
return nil, errors.New("No events found")
}
var final []*Event
for _, e := range events {
if e.End.Before(e.Start) {
e.End = e.Start
}
venue, ok := v.venueMap[e.Venue]
if !ok {
return nil, fmt.Errorf("Failed to match venue for '%s' - event=%+v", e.Venue, e)
}
finalEvent := &Event{
Name: e.Name,
Desc: e.Desc,
Start: e.Start,
End: e.End,
URL: u,
Venue: venue,
}
v.log <- fmt.Sprintf("Found event: %s", e.Name)
final = append(final, finalEvent)
}
v.log <- fmt.Sprintf("Found %d total events", len(final))
return final, nil
}