-
Notifications
You must be signed in to change notification settings - Fork 0
/
councilmembers.go
259 lines (238 loc) · 6.13 KB
/
councilmembers.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"sort"
"strings"
"time"
"github.com/jehiah/legislator/db"
)
type Person struct {
db.Person
PersonMetadata
}
func (p Person) ID() int {
return p.Person.ID
}
func (p Person) Borough() string {
city := strings.TrimSpace(p.Person.DistrictOffice.City)
switch city {
case "Brooklyn", "Bronx", "Queens", "Staten Island", "Bronx and Manhattan":
return city
case "New York", "New York, NY 10033":
return "Manhattan"
case "Bayside", "Astoria", "Jackson Heights", "Far Rockaway", "Middle Village", "St. Albans", "Oakland Gardens", "Sunnyside", "Ozone Park", "Hillcrest":
return "Queens"
}
// try based on district
if strings.HasPrefix(p.WWW, "https://council.nyc.gov/district-") {
district := strings.TrimSuffix(strings.TrimPrefix(p.WWW, "https://council.nyc.gov/district-"), "/")
switch district {
case "1", "2", "3", "4", "5", "6", "7", "9", "10":
return "Manhattan"
case "8":
return "Manhattan and Bronx"
case "11", "12", "13", "14", "15", "16", "17", "18":
return "Bronx"
case "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32":
return "Queens"
case "33", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48":
return "Brooklyn"
case "34":
return "Brooklyn and Queens"
case "49", "50", "51":
return "Staten Island"
}
}
return ""
}
type PersonMetadata struct {
ID int
District int
Slug string
Twitter, TwitterPersonal string
Facebook, FacebookPersonal string
Instagram, InstagramPersonal string
SocialAccounts []SocialAccount
}
type SocialAccount struct {
Username string
Link string
Official bool
Personal bool
Platform string // twitter | instagram | facebook | threads
}
func (s SocialAccount) CSSClass() string {
return s.Platform
}
func twitterUsername(s string) string {
if s == "" {
return ""
}
u, err := url.Parse(s)
if err != nil {
return ""
}
return "@" + strings.TrimPrefix(u.Path, "/")
}
func facebookUsername(s string) string {
if s == "" {
return ""
}
u, err := url.Parse(s)
if err != nil {
return ""
}
if strings.Contains(u.Path, "profile.php") {
return "Facebook"
}
return strings.Trim(u.Path, "/")
}
func instagramUsername(s string) string {
if s == "" {
return ""
}
u, err := url.Parse(s)
if err != nil {
return ""
}
return strings.Trim(u.Path, "/")
}
func (t PersonMetadata) GetSocialAccounts() []SocialAccount {
if len(t.SocialAccounts) > 0 {
return t.SocialAccounts
}
accounts := []SocialAccount{
{Username: twitterUsername(t.Twitter), Link: t.Twitter, Platform: "twitter"},
{Username: twitterUsername(t.TwitterPersonal), Link: t.TwitterPersonal, Platform: "twitter"},
{Username: facebookUsername(t.Facebook), Link: t.Facebook, Platform: "facebook"},
{Username: facebookUsername(t.FacebookPersonal), Link: t.FacebookPersonal, Platform: "facebook"},
{Username: instagramUsername(t.Instagram), Link: t.Instagram, Platform: "instagram"},
{Username: instagramUsername(t.InstagramPersonal), Link: t.InstagramPersonal, Platform: "instagram"},
}
var o []SocialAccount
for _, a := range accounts {
if a.Link != "" {
o = append(o, a)
}
}
return o
}
func (p Person) CouncilTitle() string {
now := time.Now()
for _, oo := range p.OfficeRecords {
if oo.BodyName != "City Council" {
continue
}
if oo.End.Before(now) {
continue
}
if oo.MemberType == "PRIMARY SPEAKER" {
return "Speaker"
}
return oo.Title
}
return ""
}
func (p Person) ActiveOfficeRecords() []db.OfficeRecord {
var final []db.OfficeRecord
now := time.Now()
for _, oo := range p.OfficeRecords {
if oo.End.Before(now) {
continue
}
switch oo.BodyName {
case "Committee of the Whole":
continue
case "City Council":
continue
case "Minority (Republican) Conference of the Council of the City of New York ":
continue
case "Democratic Conference of the Council of the City of New York ":
continue
}
final = append(final, oo)
}
sort.Slice(final, func(i, j int) bool { return final[i].BodyName < final[j].BodyName })
return final
}
// Party returns "(D)", "(R)"
func (p Person) Party() string {
party := p.PartyShort()
if party != "" {
return fmt.Sprintf("(%s)", party)
}
return ""
}
// PartyShort returns "D", "R"
func (p Person) PartyShort() string {
for _, oo := range p.OfficeRecords {
switch oo.BodyName {
case "Minority (Republican) Conference of the Council of the City of New York ":
return "R"
case "Democratic Conference of the Council of the City of New York ":
return "D"
}
}
return ""
}
// Councilmembers returns the list of councilmembers at /councilmembers
func (a *App) Councilmembers(w http.ResponseWriter, r *http.Request) {
T := Printer(r.Context())
t := newTemplate(a.templateFS, "councilmembers.html")
var people []db.Person
err := a.getJSONFile(r.Context(), "build/people_active.json", &people)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
cacheTTL := time.Minute * 30
type Page struct {
Page string
Title string
People []Person
LastSync LastSync
}
body := Page{
Page: "councilmembers",
Title: T.Sprintf("NYC Council Members"),
}
var metadata []PersonMetadata
err = a.getJSONFile(r.Context(), "build/people_metadata.json", &metadata)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
metadataLookup := make(map[int]PersonMetadata)
for _, m := range metadata {
metadataLookup[m.ID] = m
}
now := time.Now()
for _, p := range people {
if p.Start.After(now) {
continue
}
if p.End.Before(now) {
continue
}
body.People = append(body.People, Person{Person: p, PersonMetadata: metadataLookup[p.ID]})
}
err = a.getJSONFile(r.Context(), "build/last_sync.json", &body.LastSync)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
w.Header().Set("content-type", "text/html")
a.addExpireHeaders(w, cacheTTL)
err = t.ExecuteTemplate(w, "councilmembers.html", body)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
}