Scrape ranking info from TBA

Signed-off-by: Yash Chainani <yashchainani28@gmail.com>
Change-Id: Ib20a770452f5a713ae6aef76233cb0d899b58e8e
diff --git a/scouting/scraping/scrape.go b/scouting/scraping/scrape.go
index 170fe50..19426cf 100644
--- a/scouting/scraping/scrape.go
+++ b/scouting/scraping/scrape.go
@@ -17,14 +17,14 @@
 	BaseUrl string `json:"base_url"`
 }
 
-// Takes in year and FIRST event code and returns all matches in that event according to TBA.
+// Takes in year and FIRST event code and returns requested information according to TBA.
 // Also takes in a file path to the JSON config file that contains your TBA API key.
 // It defaults to <workspace root>/config.json
 // the config is expected to have the following contents:
 //{
 //    api_key:"myTBAapiKey"
 //}
-func AllMatches(year int32, eventCode, configPath string) ([]Match, error) {
+func getJson(year int32, eventCode, configPath, category string) ([]byte, error) {
 	if configPath == "" {
 		configPath = os.Getenv("BUILD_WORKSPACE_DIRECTORY") + "/scouting_config.json"
 	}
@@ -52,7 +52,7 @@
 	eventKey := strconv.Itoa(int(year)) + eventCode
 
 	// Create a get request for the match info.
-	req, err := http.NewRequest("GET", config.BaseUrl+"/api/v3/event/"+eventKey+"/matches", nil)
+	req, err := http.NewRequest("GET", config.BaseUrl+"/api/v3/event/"+eventKey+"/"+category, nil)
 	if err != nil {
 		return nil, errors.New(fmt.Sprint("Failed to build http request: ", err))
 	}
@@ -78,6 +78,17 @@
 		return nil, errors.New(fmt.Sprint("Failed to read TBA API response: ", err))
 	}
 
+	return bodyBytes, nil
+}
+
+// Return all matches in event according to TBA
+func AllMatches(year int32, eventCode, configPath string) ([]Match, error) {
+	bodyBytes, err := getJson(year, eventCode, configPath, "matches")
+
+	if err != nil {
+		return nil, err
+	}
+
 	var matches []Match
 	// Unmarshal json into go usable format.
 	if err := json.Unmarshal([]byte(bodyBytes), &matches); err != nil {
@@ -86,3 +97,20 @@
 
 	return matches, nil
 }
+
+// Return event rankings according to TBA
+func AllRankings(year int32, eventCode, configPath string) (EventRanking, error) {
+	bodyBytes, err := getJson(year, eventCode, configPath, "rankings")
+
+	if err != nil {
+		return EventRanking{}, err
+	}
+
+	var rankings EventRanking
+	// Unmarshal json into go usable format.
+	if err := json.Unmarshal([]byte(bodyBytes), &rankings); err != nil {
+		return EventRanking{}, errors.New(fmt.Sprint("Failed to parse JSON received from TBA: ", err))
+	}
+
+	return rankings, nil
+}
diff --git a/scouting/scraping/scraping_demo.go b/scouting/scraping/scraping_demo.go
index 0ea3e53..69cdbff 100644
--- a/scouting/scraping/scraping_demo.go
+++ b/scouting/scraping/scraping_demo.go
@@ -13,22 +13,43 @@
 
 func main() {
 	jsonPtr := flag.Bool("json", false, "If set, dump as JSON, rather than Go debug output.")
+	demoCategory := flag.String("category", "matches", "Decide whether to demo matches or rankings.")
+
 	flag.Parse()
 
-	// Get all the matches.
-	matches, err := scraping.AllMatches(2016, "nytr", "")
-	if err != nil {
-		log.Fatal("Failed to scrape match list: ", err)
-	}
-
-	// Dump the matches.
-	if *jsonPtr {
-		jsonData, err := json.MarshalIndent(matches, "", "  ")
+	if *demoCategory == "rankings" {
+		// Get all the rankings.
+		rankings, err := scraping.AllRankings(2016, "nytr", "")
 		if err != nil {
-			log.Fatal("Failed to turn match list into JSON: ", err)
+			log.Fatal("Failed to scrape ranking list: ", err)
 		}
-		fmt.Println(string(jsonData))
-	} else {
-		spew.Dump(matches)
+
+		// Dump the rankings.
+		if *jsonPtr {
+			jsonData, err := json.MarshalIndent(rankings, "", "  ")
+			if err != nil {
+				log.Fatal("Failed to turn ranking list into JSON: ", err)
+			}
+			fmt.Println(string(jsonData))
+		} else {
+			spew.Dump(rankings)
+		}
+	} else if *demoCategory == "matches" {
+		// Get all the matches.
+		matches, err := scraping.AllMatches(2016, "nytr", "")
+		if err != nil {
+			log.Fatal("Failed to scrape match list: ", err)
+		}
+
+		// Dump the matches.
+		if *jsonPtr {
+			jsonData, err := json.MarshalIndent(matches, "", "  ")
+			if err != nil {
+				log.Fatal("Failed to turn match list into JSON: ", err)
+			}
+			fmt.Println(string(jsonData))
+		} else {
+			spew.Dump(matches)
+		}
 	}
 }
diff --git a/scouting/scraping/types.go b/scouting/scraping/types.go
index aa0d4be..9283ac2 100644
--- a/scouting/scraping/types.go
+++ b/scouting/scraping/types.go
@@ -1,5 +1,26 @@
 package scraping
 
+type EventRanking struct {
+	Rankings []Rank `json:"rankings"`
+}
+
+type Rank struct {
+	MatchesPlayed int       `json:"matches_played"`
+	QualAverage   int       `json:"qual_average"`
+	ExtraStats    []float64 `json:"extra_stats"`
+	SortOrders    []float64 `json:"sort_orders"`
+	Records       Record    `json:"record"`
+	Rank          int       `json:"rank"`
+	Dq            int       `json:"dq"`
+	TeamKey       string    `json:"team_key"`
+}
+
+type Record struct {
+	Losses int `json:"losses"`
+	Wins   int `json:"wins"`
+	Ties   int `json:"ties"`
+}
+
 // Match holds the TBA data for a given match
 type Match struct {
 	Key             string