Het Satasiya | c6df332 | 2022-02-05 14:12:30 -0800 | [diff] [blame^] | 1 | package scraping |
| 2 | |
| 3 | // A library to grab match data from The Blue Alliance. |
| 4 | import ( |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "io/ioutil" |
| 8 | "log" |
| 9 | "net/http" |
| 10 | "os" |
| 11 | ) |
| 12 | |
| 13 | // Stores the TBA API key to access the API. |
| 14 | type params struct { |
| 15 | ApiKey string `json:"api_key"` |
| 16 | } |
| 17 | |
| 18 | // Takes in year and FIRST event code and returns all matches in that event according to TBA. |
| 19 | // Also takes in a file path to the JSON config file that contains your TBA API key. |
| 20 | // It defaults to <workspace root>/config.json |
| 21 | // the config is expected to have the following contents: |
| 22 | //{ |
| 23 | // api_key:"myTBAapiKey" |
| 24 | //} |
| 25 | func AllMatches(year, eventCode, filePath string) ([]Match, error) { |
| 26 | if filePath == "" { |
| 27 | filePath = os.Getenv("BUILD_WORKSPACE_DIRECTORY") + "/scouting_config.json" |
| 28 | } |
| 29 | // Takes the filepath and grabs the api key from the json. |
| 30 | content, err := ioutil.ReadFile(filePath) |
| 31 | if err != nil { |
| 32 | log.Fatal(err) |
| 33 | } |
| 34 | // Parses the JSON parameters into a struct. |
| 35 | var passed_params params |
| 36 | error := json.Unmarshal([]byte(content), &passed_params) |
| 37 | if error != nil { |
| 38 | log.Fatalf("You forgot to add the api_key parameter in the json file") |
| 39 | log.Fatalf("%s", err) |
| 40 | } |
| 41 | |
| 42 | // Create the TBA event key for the year and event code. |
| 43 | eventKey := year + eventCode |
| 44 | |
| 45 | // Create the client for HTTP requests. |
| 46 | client := &http.Client{} |
| 47 | |
| 48 | // Create a get request for the match info. |
| 49 | req, err := http.NewRequest("GET", "https://www.thebluealliance.com/api/v3/event/"+eventKey+"/matches", nil) |
| 50 | |
| 51 | if err != nil { |
| 52 | return nil, errors.New("failed to build http request") |
| 53 | } |
| 54 | |
| 55 | // Add the auth key header to the request. |
| 56 | req.Header.Add("X-TBA-Auth-Key", passed_params.ApiKey) |
| 57 | |
| 58 | // Make the API request |
| 59 | resp, err := client.Do(req) |
| 60 | |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | |
| 65 | if resp.Status != "200 OK" { |
| 66 | return nil, errors.New("Recieved a status of " + resp.Status + " expected : 200 OK") |
| 67 | } |
| 68 | |
| 69 | // Wait until the response is done. |
| 70 | defer resp.Body.Close() |
| 71 | |
| 72 | // Get all bytes from response body. |
| 73 | bodyBytes, err := ioutil.ReadAll(resp.Body) |
| 74 | if err != nil { |
| 75 | return nil, errors.New("failed to read response body with error :" + err.Error()) |
| 76 | } |
| 77 | |
| 78 | var matches []Match |
| 79 | // Unmarshal json into go usable format. |
| 80 | jsonError := json.Unmarshal([]byte(bodyBytes), &matches) |
| 81 | if jsonError != nil { |
| 82 | return nil, errors.New("failed to unmarshal json recieved from TBA") |
| 83 | } |
| 84 | |
| 85 | return matches, nil |
| 86 | } |