scouting: Add an endpoint for populating the match schedule

This patch combines the scraping library with the scouting webserver.
There's now also a new end point for the web page (or debug CLI tool)
to ask the server to fetch the match list. The end point is
`/requests/refresh_match_list`.

All the tests are updated. The `cli_test` downloads a 2016 ny_tr match
list that I downloaded from TBA. It should be a decent integration
test as it uses representative data.

Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: I6c540590521b00887eb2ddde2a9369875c659551
diff --git a/scouting/webserver/main.go b/scouting/webserver/main.go
index 3c699fd..94e0222 100644
--- a/scouting/webserver/main.go
+++ b/scouting/webserver/main.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"errors"
 	"flag"
 	"fmt"
 	"io/ioutil"
@@ -11,6 +12,7 @@
 	"syscall"
 
 	"github.com/frc971/971-Robot-Code/scouting/db"
+	"github.com/frc971/971-Robot-Code/scouting/scraping"
 	"github.com/frc971/971-Robot-Code/scouting/webserver/requests"
 	"github.com/frc971/971-Robot-Code/scouting/webserver/server"
 	"github.com/frc971/971-Robot-Code/scouting/webserver/static"
@@ -40,6 +42,10 @@
 	portPtr := flag.Int("port", 8080, "The port number to bind to.")
 	dirPtr := flag.String("directory", ".", "The directory to serve at /.")
 	dbPathPtr := flag.String("database", getDefaultDatabasePath(), "The path to the database.")
+	blueAllianceConfigPtr := flag.String("tba_config", "",
+		"The path to your The Blue Alliance JSON config. "+
+			"It needs an \"api_key\" field with your TBA API key. "+
+			"Optionally, it can have a \"url\" field with the TBA API base URL.")
 	flag.Parse()
 
 	database, err := db.NewDatabase(*dbPathPtr)
@@ -47,9 +53,16 @@
 		log.Fatal("Failed to connect to database: ", err)
 	}
 
+	scrapeMatchList := func(year int32, eventCode string) ([]scraping.Match, error) {
+		if *blueAllianceConfigPtr == "" {
+			return nil, errors.New("Cannot scrape TBA's match list without a config file.")
+		}
+		return scraping.AllMatches(year, eventCode, *blueAllianceConfigPtr)
+	}
+
 	scoutingServer := server.NewScoutingServer()
 	static.ServePages(scoutingServer, *dirPtr)
-	requests.HandleRequests(database, scoutingServer)
+	requests.HandleRequests(database, scrapeMatchList, scoutingServer)
 	scoutingServer.Start(*portPtr)
 	fmt.Println("Serving", *dirPtr, "on port", *portPtr)