-
Notifications
You must be signed in to change notification settings - Fork 5
/
organizations.go
320 lines (273 loc) · 8.01 KB
/
organizations.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
This file contains the code for the student organization scraper.
*/
package scrapers
import (
"bufio"
"context"
"encoding/base64"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/UTDNebula/api-tools/utils"
"github.com/UTDNebula/nebula-api/api/schema"
"github.com/chromedp/cdproto/browser"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson/primitive"
)
const (
socBaseUrl = `https://cometmail.sharepoint.com`
socLoginUrl = socBaseUrl + `/sites/StudentOrganizationCenterSP/Lists/Student%20Organization%20Directory/All%20Items%20gallery.aspx`
localPartCharClass = `[:alnum:]!#$%&'*+/=?^_` + "`" + `{|}~-`
subdomainPattern = `([[:alnum:]]([[:alnum:]-]*[[:alnum:]])?\.)+`
topdomainPattern = `[[:alnum:]]([[:alnum:]-]*[[:alnum:]])?`
)
var (
baseUrlStruct, _ = url.Parse(socBaseUrl)
localPartPattern = fmt.Sprintf(`[%[1]s]+(\.[%[1]s]+)*`, localPartCharClass)
emailRegex = regexp.MustCompile(fmt.Sprintf(`%s@%s%s`, localPartPattern, subdomainPattern, topdomainPattern))
)
func ScrapeOrganizations(outdir string) {
log.Println("Scraping SOC ...")
if err := godotenv.Load(); err != nil {
log.Panic("error loading .env file")
}
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag("headless", false))
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel := chromedp.NewContext(allocCtx)
// ensure cleanup occurs
defer cancel()
if err := loginToSoc(ctx); err != nil {
panic(err)
}
if err := scrapeData(ctx, outdir); err != nil {
panic(err)
}
}
func lookupEnvWithError(name string) (string, error) {
value, exists := os.LookupEnv(name)
if !exists {
return "", errors.New(name + " is missing from .env!")
}
return value, nil
}
func loginToSoc(ctx context.Context) error {
log.Println("Logging into SOC ...")
netID, err := lookupEnvWithError("LOGIN_NETID")
if err != nil {
return err
}
password, err := lookupEnvWithError("LOGIN_PASSWORD")
if err != nil {
return err
}
_, err = chromedp.RunResponse(ctx,
network.ClearBrowserCookies(),
chromedp.Navigate(socLoginUrl),
chromedp.SendKeys(`input[type="email"]`, netID+"@utdallas.edu"),
chromedp.Click(`input[type="submit"]`),
chromedp.SendKeys(`input[type="password"]`, password),
// wait for sign in button to load (regular WaitVisible and WaitReady methods do not work)
chromedp.Sleep(1*time.Second),
chromedp.Click(`input[type="submit"]`),
chromedp.Sleep(2*time.Second),
chromedp.Click(`button.auth-button`),
chromedp.WaitReady(`body`),
)
return err
}
func scrapeData(ctx context.Context, outdir string) error {
log.Println("Scraping data ...")
// download file method adapted from https://github.com/chromedp/examples/blob/master/download_file/main.go
timedCtx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
done := make(chan string, 1)
// listen for download events
chromedp.ListenTarget(timedCtx, func(v interface{}) {
ev, ok := v.(*browser.EventDownloadProgress)
if !ok {
return
}
if ev.State == browser.DownloadProgressStateCompleted {
// stop listening for further download events and send guid
cancel()
done <- ev.GUID
close(done)
}
})
tempDir, _ := filepath.Abs(filepath.Join(outdir, "tmp"))
utils.VPrintf("Downloading CSV to %s ...", tempDir)
if err := os.MkdirAll(tempDir, 0755); err != nil {
return err
}
if err := chromedp.Run(ctx,
chromedp.Sleep(1*time.Second),
chromedp.Click(`button[name="Export"]`, chromedp.NodeReady),
browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllowAndName).WithDownloadPath(tempDir).WithEventsEnabled(true),
chromedp.Sleep(1*time.Second),
chromedp.Click(`button[name="Export to CSV"]`, chromedp.NodeReady),
); err != nil {
return err
}
// get GUID of download and reconstruct path
guid := <-done
guidPath := filepath.Join(tempDir, guid)
defer func() {
// remove temp file and directory
os.Remove(guidPath)
}()
outPath := filepath.Join(outdir, "organizations.json")
if err := processCsv(ctx, guidPath, outPath); err != nil {
return err
}
return nil
}
func processCsv(ctx context.Context, inputPath string, storageFilePath string) error {
// open csv for reading
csvFile, err := os.Open(inputPath)
if err != nil {
return err
}
// init csv reader
bufReader := bufio.NewReader(csvFile)
// discard headers
if _, _, err := bufReader.ReadLine(); err != nil {
return err
}
csvReader := csv.NewReader(bufReader)
// write to json
storageFile, err := os.Create(storageFilePath)
if err != nil {
return err
}
encoder := json.NewEncoder(bufio.NewWriter(storageFile))
encoder.SetIndent("", "\t")
var orgs []*schema.Organization
// process each row of csv
for i := 1; true; i++ {
entry, err := csvReader.Read()
if err != nil {
if err == io.EOF {
break
}
return err
}
utils.VPrintf("Processing row %d", i)
org, err := parseCsvRecord(ctx, entry)
if err != nil {
return err
}
orgs = append(orgs, org)
}
// Write JSON to file
if err = encoder.Encode(orgs); err != nil {
return err
}
if err := csvFile.Close(); err != nil {
return err
}
if err := storageFile.Close(); err != nil {
return err
}
return nil
}
func parseCsvRecord(ctx context.Context, entry []string) (*schema.Organization, error) {
// initial cleaning
for i, v := range entry {
v = strings.ReplaceAll(v, "\u0026", "")
v = strings.TrimSpace(v)
entry[i] = v
}
imageData, err := retrieveImage(ctx, entry[5])
if err != nil {
utils.VPrintf("Error retrieving image for %s: %v", entry[0], err)
}
return &schema.Organization{
Id: schema.IdWrapper(primitive.NewObjectID().Hex()),
Title: entry[0],
Categories: parseCategories(entry[1]),
Description: entry[2],
President_name: entry[3],
Emails: parseEmails(entry[4]),
Picture_data: imageData,
}, nil
}
func parseCategories(cats string) []string {
cats = strings.TrimLeft(cats, "[")
cats = strings.TrimRight(cats, "]")
// strange character appears in csv; need to remove it
cats = strings.ReplaceAll(cats, `"`, "")
// split by comma
catsArray := strings.Split(cats, ",")
// strip whitespace from ends
for j, v := range catsArray {
catsArray[j] = strings.TrimSpace(v)
}
return catsArray
}
func parseEmails(emails string) []string {
return emailRegex.FindAllString(emails, -1)
}
func retrieveImage(ctx context.Context, imageUri string) (string, error) {
if imageUri == "" {
return "", nil
}
urlStruct, err := url.Parse(imageUri)
if err != nil {
return "", err
}
requestUrl := baseUrlStruct.ResolveReference(urlStruct).String()
//log.Printf("loading image %s", requestUrl)
// method adapted from https://github.com/chromedp/examples/blob/master/download_image/main.go
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
done := make(chan bool)
// this will be used to capture the request id for matching network events
var requestID network.RequestID
// listen for network requests and choose desired
chromedp.ListenTarget(ctx, func(v interface{}) {
switch ev := v.(type) {
case *network.EventRequestWillBeSent:
if ev.Request.URL == requestUrl {
requestID = ev.RequestID
}
case *network.EventLoadingFinished:
if ev.RequestID == requestID {
close(done)
}
}
})
if err := chromedp.Run(ctx, chromedp.Navigate(requestUrl)); err != nil {
log.Printf("Error navigating to %s: %v", requestUrl, err)
return "", err
}
// wait for image request to finish
<-done
//log.Printf("Done retrieving image from %s", requestUrl)
var buf []byte
if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
var err error
buf, err = network.GetResponseBody(requestID).Do(ctx)
if err != nil {
log.Printf("Error getting response body for %s: %v", requestUrl, err)
}
return err
})); err != nil {
return "", err
}
encoded := base64.StdEncoding.EncodeToString(buf)
// get response body
return encoded, nil
}