Skip to content

Commit 308c192

Browse files
authored
Add Mazevo scraper
1 parent 2902535 commit 308c192

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ LOGIN_NETID=
33
LOGIN_PASSWORD=
44
LOGIN_ASTRA_USERNAME=
55
LOGIN_ASTRA_PASSWORD=
6+
#Login to https://east.mymazevo.com/main-home then go to https://east.mymazevo.com/api/tenantsettings/GetApiKey
7+
MAZEVO_API_KEY=
68

79
#Uploader
810
MONGODB_URI=

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func main() {
3838
scrapeEvents := flag.Bool("events", false, "Alongside -scrape, signifies that events should be scraped.")
3939
// Flag for astra scraping
4040
scrapeAstra := flag.Bool("astra", false, "Alongside -scrape, signifies that Astra should be scraped.")
41+
// Flag for mazevo scraping
42+
scrapeMazevo := flag.Bool("mazevo", false, "Alongside -scrape, signifies that Mazevo should be scraped.")
4143

4244
// Flags for parsing
4345
parse := flag.Bool("parse", false, "Puts the tool into parsing mode.")
@@ -100,6 +102,8 @@ func main() {
100102
scrapers.ScrapeEvents(*outDir)
101103
case *scrapeAstra:
102104
scrapers.ScrapeAstra(*outDir)
105+
case *scrapeMazevo:
106+
scrapers.ScrapeMazevo(*outDir)
103107
default:
104108
log.Panic("You must specify which type of scraping you would like to perform with one of the scraping flags!")
105109
}

scrapers/mazevo.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
This file contains the code for the Mazevo scraper.
3+
*/
4+
5+
package scrapers
6+
7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
11+
"io"
12+
"log"
13+
"net/http"
14+
"os"
15+
"time"
16+
17+
"github.com/joho/godotenv"
18+
)
19+
20+
func ScrapeMazevo(outDir string) {
21+
22+
// Load env vars
23+
if err := godotenv.Load(); err != nil {
24+
log.Panic("Error loading .env file")
25+
}
26+
apikey, present := os.LookupEnv("MAZEVO_API_KEY")
27+
if !present {
28+
log.Panic("MAZEVO_API_KEY is missing from .env!")
29+
}
30+
31+
// Make output folder
32+
err := os.MkdirAll(outDir, 0777)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
// Init http client
38+
tr := &http.Transport{
39+
MaxIdleConns: 10,
40+
IdleConnTimeout: 30 * time.Second,
41+
DisableCompression: true,
42+
}
43+
cli := &http.Client{Transport: tr}
44+
45+
// Start on previous date to make sure we have today's data, regardless of what timezone the scraper is in
46+
date := time.Now()
47+
startDate := date.Add(time.Hour * -24).Format(time.RFC3339)
48+
endDate := date.Add(time.Hour * 24 * 365).Format(time.RFC3339)
49+
50+
// Request events
51+
stringBody := ""
52+
{
53+
url := "https://east.mymazevo.com/api/PublicCalendar/GetCalendarEvents"
54+
requestBodyMap := map[string]string{
55+
"apiKey": apikey,
56+
"end": endDate,
57+
"start": startDate,
58+
}
59+
requestBodyBytes, _ := json.Marshal(requestBodyMap)
60+
requestBody := bytes.NewBuffer(requestBodyBytes)
61+
req, err := http.NewRequest("POST", url, requestBody)
62+
if err != nil {
63+
panic(err)
64+
}
65+
req.Header = http.Header{
66+
"Content-type": {"application/json"},
67+
"Accept": {"application/json"},
68+
}
69+
res, err := cli.Do(req)
70+
if err != nil {
71+
panic(err)
72+
}
73+
if res.StatusCode != 200 {
74+
log.Panicf("ERROR: Status was: %s\nIf the status is 404, you've likely been IP ratelimited!", res.Status)
75+
}
76+
body, err := io.ReadAll(res.Body)
77+
if err != nil {
78+
panic(err)
79+
}
80+
res.Body.Close()
81+
stringBody = string(body)
82+
}
83+
84+
// Write event data to output file
85+
fptr, err := os.Create(fmt.Sprintf("%s/mazevoReservations.json", outDir))
86+
if err != nil {
87+
panic(err)
88+
}
89+
_, err = fptr.Write([]byte(stringBody))
90+
if err != nil {
91+
panic(err)
92+
}
93+
fptr.Close()
94+
}

0 commit comments

Comments
 (0)