|
|
@@ -1,8 +1,10 @@
|
|
|
package balldontlie
|
|
|
|
|
|
+import requests._
|
|
|
import upickle.default._
|
|
|
import game._
|
|
|
import os.Path
|
|
|
+import scala.util.{Try, Success, Failure}
|
|
|
|
|
|
case class ResponseMetadata(
|
|
|
total_pages: Int,
|
|
|
@@ -21,13 +23,33 @@ object Utils {
|
|
|
endpoint: String,
|
|
|
params: List[(String, Any)] = List()
|
|
|
): T = {
|
|
|
- println(endpoint, params)
|
|
|
- val r = requests.get(
|
|
|
- endpoint,
|
|
|
- params = params.map({ case (k, v) => (k, v.toString()) })
|
|
|
+ // Try to be a good web citizen and not flood the server with rapid requests
|
|
|
+ Thread.sleep(100)
|
|
|
+
|
|
|
+ val r = Try(
|
|
|
+ requests.get(
|
|
|
+ endpoint,
|
|
|
+ params = params.map({ case (k, v) => (k, v.toString()) })
|
|
|
+ )
|
|
|
)
|
|
|
- val json = ujson.read(r.text())
|
|
|
- upickle.default.read[T](json)
|
|
|
+
|
|
|
+ r match {
|
|
|
+ case Success(response) =>
|
|
|
+ println(endpoint, params)
|
|
|
+
|
|
|
+ val json = ujson.read(response.text())
|
|
|
+ upickle.default.read[T](json)
|
|
|
+
|
|
|
+ case Failure(ex) =>
|
|
|
+ ex match {
|
|
|
+ case exc: requests.RequestFailedException
|
|
|
+ if exc.response.statusCode == 429 => {
|
|
|
+ println("Got 429 error, retrying after 1s")
|
|
|
+ Thread.sleep(1000)
|
|
|
+ get_and_decode[T](endpoint, params)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
def writeToFile[T: Writer](data: T, file: Path): Unit = {
|
|
|
@@ -59,8 +81,8 @@ object EndpointResponse {
|
|
|
|
|
|
val teams_per_page = 25
|
|
|
val games_per_page = 25
|
|
|
- val stats_per_page = 25
|
|
|
- val stats_page_limit = 2
|
|
|
+ val stats_per_page = 100
|
|
|
+ val stats_page_limit = 100
|
|
|
|
|
|
def doPaginatedRequest[T: Reader](
|
|
|
endpoint: String,
|
|
|
@@ -146,7 +168,7 @@ object EndpointResponse {
|
|
|
page: Option[Int] = Some(1)
|
|
|
): ListOfStats = {
|
|
|
page match {
|
|
|
- case None => dataSoFar
|
|
|
+ case None => dataSoFar
|
|
|
case Some(value) if value > stats_page_limit => dataSoFar
|
|
|
case Some(value) =>
|
|
|
val response =
|
|
|
@@ -156,7 +178,7 @@ object EndpointResponse {
|
|
|
stats_per_page,
|
|
|
params
|
|
|
)
|
|
|
- println(response.meta)
|
|
|
+ println(response.meta)
|
|
|
getAllData(
|
|
|
endpoint,
|
|
|
params,
|