|
| 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