|
| 1 | +package main |
| 2 | + |
| 3 | +// Mostly copied from: https://github.com/csesoc/csesoc.unsw.edu.au/blob/dev/backend/server/events/events.go |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +type ( |
| 16 | + FbResponse struct { |
| 17 | + Data []FbRespEvent `json:"data"` |
| 18 | + Error FbRespError `json:"error"` |
| 19 | + } |
| 20 | + |
| 21 | + // FbRespEvent - struct to unmarshal event specifics |
| 22 | + FbRespEvent struct { |
| 23 | + Description string `json:"description"` |
| 24 | + Name string `json:"name"` |
| 25 | + Start string `json:"start_time"` |
| 26 | + End string `json:"end_time"` |
| 27 | + ID string `json:"id"` |
| 28 | + Place FbRespPlace `json:"place"` |
| 29 | + IsCancelled bool `json:"is_cancelled"` |
| 30 | + IsOnline bool `json:"is_online"` |
| 31 | + Cover FbRespCover `json:"cover"` |
| 32 | + } |
| 33 | + |
| 34 | + // FbRespError - struct to unmarshal any error response |
| 35 | + FbRespError struct { |
| 36 | + ErrorType int `json:"type"` |
| 37 | + Message string `json:"message"` |
| 38 | + } |
| 39 | + |
| 40 | + // FbRespCover - struct to unmarshal the URI of the cover image |
| 41 | + FbRespCover struct { |
| 42 | + CoverURI string `json:"source"` |
| 43 | + } |
| 44 | + |
| 45 | + // FbRespPlace - event location can come with added information, so we only take the name |
| 46 | + FbRespPlace struct { |
| 47 | + Name string `json:"name"` |
| 48 | + } |
| 49 | + |
| 50 | + // MarshalledEvents - struct to pack up events with the last update time to be marshalled. |
| 51 | + MarshalledEvents struct { |
| 52 | + LastUpdate int64 `json:"updated"` |
| 53 | + Events []Event `json:"events"` |
| 54 | + } |
| 55 | + |
| 56 | + // Event - struct to store an individual event with all the info we want |
| 57 | + Event struct { |
| 58 | + Name string `json:"name"` |
| 59 | + Description string `json:"description"` |
| 60 | + Start string `json:"start_time"` |
| 61 | + End string `json:"end_time"` |
| 62 | + ID string `json:"fb_event_id"` |
| 63 | + Place string `json:"place"` |
| 64 | + CoverURL string `json:"fb_cover_img"` |
| 65 | + } |
| 66 | +) |
| 67 | + |
| 68 | +const ( |
| 69 | + FBEventPath = "/csesoc/events" |
| 70 | + FBGraphAPIPath = "https://graph.facebook.com/v15.0" |
| 71 | +) |
| 72 | + |
| 73 | +var FBAccessToken = os.Getenv("FB_TOKEN") |
| 74 | + |
| 75 | +// fetchEvents just fetches directly from the FB api, much of this code is bonked from the old website |
| 76 | +// i wish go had monads :( |
| 77 | +func queryFbApi() (FbResponse, error) { |
| 78 | + eventsReq, err := http.Get( |
| 79 | + fmt.Sprintf( |
| 80 | + "%s%s?access_token=%s&since=%d", |
| 81 | + FBGraphAPIPath, FBEventPath, FBAccessToken, time.Now().Unix(), |
| 82 | + ), |
| 83 | + ) |
| 84 | + if err != nil { |
| 85 | + return FbResponse{}, fmt.Errorf("there was an error fetching events from FB: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + defer eventsReq.Body.Close() |
| 89 | + if eventsReq.StatusCode != http.StatusOK { |
| 90 | + return FbResponse{}, fmt.Errorf("facebook api returned a http status: %d", eventsReq.StatusCode) |
| 91 | + } |
| 92 | + |
| 93 | + unparsedEvents, readErr := ioutil.ReadAll(eventsReq.Body) |
| 94 | + if readErr != nil { |
| 95 | + return FbResponse{}, fmt.Errorf("failed to read response body %w", readErr) |
| 96 | + } |
| 97 | + |
| 98 | + var apiResp FbResponse |
| 99 | + json.Unmarshal(unparsedEvents, &apiResp) |
| 100 | + if (apiResp.Error != FbRespError{}) { |
| 101 | + return FbResponse{}, errors.New("facebook api returned an error") |
| 102 | + } |
| 103 | + |
| 104 | + return apiResp, nil |
| 105 | +} |
| 106 | + |
| 107 | +// parseFbResponse takes a raw fb API response and converts it into a sequence of events |
| 108 | +func parseFbResponse(resp FbResponse) []Event { |
| 109 | + events := []Event{} |
| 110 | + |
| 111 | + for _, event := range resp.Data { |
| 112 | + if !event.IsCancelled { |
| 113 | + parsedEvent := Event{ |
| 114 | + Name: event.Name, Description: event.Description, |
| 115 | + Start: event.Start, End: event.End, |
| 116 | + ID: event.ID, Place: event.Place.Name, |
| 117 | + CoverURL: event.Cover.CoverURI, |
| 118 | + } |
| 119 | + |
| 120 | + if event.IsOnline { |
| 121 | + parsedEvent.Place = "Online" |
| 122 | + } |
| 123 | + |
| 124 | + events = append(events, parsedEvent) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return events |
| 129 | +} |
| 130 | + |
| 131 | +// GetFBEvents returns a sequence of all CSESoc events |
| 132 | +func GetFBEvents() ([]Event, error) { |
| 133 | + if rawFbData, err := queryFbApi(); err != nil { |
| 134 | + return parseFbResponse(rawFbData), nil |
| 135 | + } else { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | +} |
0 commit comments