-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsfzc.go
154 lines (133 loc) · 3.59 KB
/
sfzc.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
149
150
151
152
153
154
package zenfo
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
)
// Sfzc crawls sfzc.org, satisfies Worker interface
type Sfzc struct {
venueMap map[string]*Venue
client *Client
log chan string
}
type eventJSON struct {
Name string `json:"Name"`
Blurb string `json:"Introduction"`
Desc string `json:"Description"`
Start time.Time `json:"DateStart"`
End time.Time `json:"DateEnd"`
URL string `json:"Link"`
ContentID int `json:"ContentId"`
Location string `json:"Location"`
Type string `json:"EventType"`
}
func html2text(html string) (string, error) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
return "", errors.Wrap(err, "goquery.NewDocument()")
}
out := doc.Text()
return strings.TrimSpace(out), nil
}
// Name return human-friendly name for worker for logs
func (s *Sfzc) Name() string {
return "San Francisco Zen Center (sfzc.org)"
}
// Init sets HTTP client and defines internal venue map
func (s *Sfzc) Init(client *Client, log chan string) error {
s.client = client
s.venueMap = make(map[string]*Venue)
s.log = log
s.venueMap["city center"] = &Venue{
Name: "San Francisco Zen Center",
Addr: "300 Page St, San Francisco, CA 94102",
Phone: "+1 (415) 863-3136",
Email: "ccoffice@sfzc.org",
Lat: 37.773789,
Lng: -122.426153,
Website: "https://sfzc.org",
}
s.venueMap["tassajara"] = &Venue{
Name: "Tassajra Zen Mountain Center",
Addr: "39171 Tassajara Road, Carmel Valley, CA 93924",
Phone: "+1 (831) 659-2229",
Email: "rezoffice@sfzc.org",
Lat: 36.234131,
Lng: -121.550031,
Website: "http://sfzc.org/tassajara",
}
s.venueMap["green gulch"] = &Venue{
Name: "Green Gulch Farm Zen Center",
Addr: "1601 Shoreline Highway, Muir Beach, CA 94965",
Phone: "+1 (415) 383-3134",
Email: "ggfoffice@sfzc.org",
Lat: 37.865967,
Lng: -122.563911,
Website: "http://sfzc.org/green-gulch",
}
s.venueMap["online"] = s.venueMap["City Center"]
s.log <- "Initialized!"
return nil
}
// Desc returns description for website crawled
func (s *Sfzc) Desc() string {
return "San Francisco Zen Center (sfzc.org)"
}
// Events hits sfcz JSON API and returns slice of Event types
func (s *Sfzc) Events() ([]*Event, error) {
resp, err := s.client.Get("http://sfzc.org/api/eventsapi/allevents")
if err != nil {
return nil, err
}
defer resp.Body.Close()
var events []eventJSON
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 {
blurb, err := html2text(e.Blurb)
if err != nil {
return nil, err
}
desc, err := html2text(e.Desc)
if err != nil {
return nil, err
}
e.Blurb = blurb
e.Desc = desc
if e.End.Before(e.Start) {
e.End = e.Start
}
var link string
if !strings.HasPrefix(e.URL, "http") {
link = "http://sfzc.org"
}
u := fmt.Sprintf("%s%s", link, e.URL)
// I've seen "Green Gulch" before
cleanLocation := clean(e.Location)
venue, ok := s.venueMap[cleanLocation]
if !ok {
return nil, fmt.Errorf("Failed to match venue for '%s' - event=%+v", e.Location, e)
}
finalEvent := &Event{
Name: e.Name,
Blurb: e.Blurb,
Desc: e.Desc,
Start: e.Start,
End: e.End,
URL: u,
Venue: venue,
}
s.log <- fmt.Sprintf("Found event: %s", e.Name)
final = append(final, finalEvent)
}
s.log <- fmt.Sprintf("Found %d total events", len(final))
return final, nil
}