Refactor some code for scraping thebluealliance.com in the background
I want to make it so that the match list is imported automatically.
Right now we have to manually refresh the match list as elimination
matches happen.
This patch refactors the ranking scraping code so that we can add a
match list scraper easily in a future patch.
I made a few things generic so that it'll be easier to reuse the code
for more things later.
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: Ia8164b62deceddeda683ac3080e99e0fe1b5904a
diff --git a/scouting/webserver/rankings/rankings_test.go b/scouting/webserver/rankings/rankings_test.go
index aa23c76..6f8af3b 100644
--- a/scouting/webserver/rankings/rankings_test.go
+++ b/scouting/webserver/rankings/rankings_test.go
@@ -2,9 +2,11 @@
import (
"github.com/frc971/971-Robot-Code/scouting/db"
+ "github.com/frc971/971-Robot-Code/scouting/scraping/background"
"github.com/frc971/971-Robot-Code/scouting/webserver/server"
"net/http"
"reflect"
+ "strings"
"testing"
"time"
)
@@ -18,8 +20,13 @@
return nil
}
-func ServeRankings(h http.Handler) http.Handler {
+func ServeRankings(t *testing.T, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
+ // Make sure that the rankings are requested properly.
+ if !strings.HasSuffix(r.URL.Path, "/2016nytr/rankings") {
+ t.Error("Got unexpected URL: ", r.URL.Path)
+ }
+
r.URL.Path = "scraping/test_data/2016_nytr_rankings.json"
h.ServeHTTP(w, r)
@@ -30,13 +37,15 @@
func TestGetRankings(t *testing.T) {
database := MockDatabase{}
- scraper := RankingScraper{}
+ scraper := background.BackgroundScraper{}
tbaServer := server.NewScoutingServer()
- tbaServer.Handle("/", ServeRankings(http.FileServer(http.Dir("../../"))))
+ tbaServer.Handle("/", ServeRankings(t, http.FileServer(http.Dir("../../"))))
tbaServer.Start(8000)
defer tbaServer.Stop()
- scraper.Start(&database, 0, "", "scouting_test_config.json")
+ scraper.Start(func() {
+ GetRankings(&database, 0, "", "scouting_test_config.json")
+ })
defer scraper.Stop()
for {