Copyright | Aaron Taylor, 2016 |
---|---|
License | MIT |
Maintainer | aaron@hamsterdam.co |
Safe Haskell | None |
Language | Haskell2010 |
Functions and types for interacting with NBA Stats.
- getSplitRows :: FromJSON a => StatsPath -> SplitName -> StatsParameters -> IO (Either StatsError [a])
- getSplitRow :: (Eq v, Show v, FromJSON v, FromJSON a) => StatsPath -> SplitName -> SplitColumn -> v -> StatsParameters -> IO (Either StatsError a)
- getSplitRowsGeneric :: (MonadIO m, MonadHttp m, MonadError StatsError m, FromJSON a) => StatsPath -> SplitName -> StatsParameters -> m [a]
- getSplitRowGeneric :: (MonadIO m, MonadHttp m, MonadError StatsError m, Eq v, Show v, FromJSON v, FromJSON a) => StatsPath -> SplitName -> SplitColumn -> v -> StatsParameters -> m a
- data Stats = Stats {}
- data Split = Split {}
- type SplitName = Text
- type SplitColumn = Text
- type SplitRow = [Value]
- type StatsPath = ByteString
- type StatsParameters = [(ByteString, Maybe ByteString)]
- data StatsError
- domain :: ByteString
- getRequest :: StatsPath -> Request
How to use this library
The following is a working example of getting some "advanced statistics", split by month, for the San Antonio Spurs 2015-2016 regular season.
To learn how to find the NBA Stats values, like teamdashboardbygeneralsplits
, for this example, read the guide.
import qualified Data.Aeson as Aeson import qualified Data.Aeson.Types as Aeson import Data.Aeson ((.:)) import qualified NBA.Stats as Stats main :: IO () main = do eitherErrorOrStats <- advancedStatsByMonth case eitherErrorOrStats of Left statsError -> print statsError Right stats -> mapM_ print stats data AdvancedStats = AdvancedStats { month :: String, offensiveRating :: Double, defensiveRating :: Double } deriving (Show, Eq) instance Aeson.FromJSON AdvancedStats where parseJSON (Aeson.Object o) = do month <- o .: "SEASON_MONTH_NAME" offensiveRating <- o .: "OFF_RATING" defensiveRating <- o .: "DEF_RATING" return AdvancedStats {..} parseJSON invalid = Aeson.typeMismatch "AdvancedStats" invalid advancedStatsByMonth :: IO (Either Stats.StatsError [AdvancedStats]) advancedStatsByMonth = Stats.getSplitRows "teamdashboardbygeneralsplits" "MonthTeamDashboard" [ ("Conference", Nothing), ("DateFrom", Nothing), ("DateTo", Nothing), ("Division", Nothing), ("GameScope", Nothing), ("GameSegment", Nothing), ("LastNGames", Just "0"), ("LeagueID", Just "00"), ("Location", Nothing), ("MeasureType", Just "Advanced"), ("Month", Just "0"), ("OpponentTeamID", Just "0"), ("Outcome", Nothing), ("PaceAdjust", Just "N"), ("PerMode", Just "PerGame"), ("Period", Just "0"), ("PlayerExperience", Nothing), ("PlayerPosition", Nothing), ("PlusMinus", Just "N"), ("PORound", Just "0"), ("Rank", Just "N"), ("Season", Just "2015-16"), ("SeasonSegment", Nothing), ("SeasonType", Just "Regular Season"), ("ShotClockRange", Nothing), ("StarterBench", Nothing), ("TeamID", Just "1610612759"), ("VsConference", Nothing), ("VsDivision", Nothing) ]
This program's output at the time of writing is:
AdvancedStats {month = "October", offensiveRating = 102.7, defensiveRating = 93.4} AdvancedStats {month = "November", offensiveRating = 102.5, defensiveRating = 93.4} AdvancedStats {month = "December", offensiveRating = 111.8, defensiveRating = 91.5} AdvancedStats {month = "January", offensiveRating = 114.0, defensiveRating = 100.7} AdvancedStats {month = "February", offensiveRating = 110.7, defensiveRating = 99.1} AdvancedStats {month = "March", offensiveRating = 107.8, defensiveRating = 97.2} AdvancedStats {month = "April", offensiveRating = 102.3, defensiveRating = 103.5}
Simple API
:: FromJSON a | |
=> StatsPath | The URL path for the stats web page containing the split. |
-> SplitName | The split name. |
-> StatsParameters | The parameters for customizing the stats. |
-> IO (Either StatsError [a]) | The return value: an IO action resulting in an error or split rows. |
Gets all the rows in a NBA Stats split.
When using this function in a custom monad transformer, it may be desirable to use the generic version of this function, getSplitRowsGeneric
, instead.
:: (Eq v, Show v, FromJSON v, FromJSON a) | |
=> StatsPath | The URL path for the stats web page containing the split. |
-> SplitName | The split name. |
-> SplitColumn | The column name key for a the desired row. |
-> v | The expected row value associated with the column name key for a the desired row. |
-> StatsParameters | The parameters for customizing the stats. |
-> IO (Either StatsError a) | The return value: an IO action resulting in an error or a split row. |
Gets a row in a NBA Stats split.
When using this function in a custom monad transformer, it may be desirable to use the generic version of this function, getSplitRowGeneric
, instead.
Generic API
:: (MonadIO m, MonadHttp m, MonadError StatsError m, FromJSON a) | |
=> StatsPath | The URL path for the stats web page containing the split. |
-> SplitName | The split name. |
-> StatsParameters | The parameters for customizing the stats. |
-> m [a] | The return value: an action resulting in an error or split rows. |
Gets all the rows in a NBA Stats split.
The simpler version of this function, getSplitRows
, has a concrete m
.
:: (MonadIO m, MonadHttp m, MonadError StatsError m, Eq v, Show v, FromJSON v, FromJSON a) | |
=> StatsPath | The URL path for the stats web page containing the split. |
-> SplitName | The split name. |
-> SplitColumn | The column name key for a the desired row. |
-> v | The expected row value associated with the column name key for a the desired row. |
-> StatsParameters | The parameters for customizing the stats. |
-> m a | The return value: an action resulting in an error or a split row. |
Gets a row in an NBA Stats split.
The simpler version of this function, getSplitRows
, has a concrete m
.
Types
An NBA Stats resource.
This type represents the top-level JSON object returned from the NBA Stats REST API.
An NBA Stats split.
This type represents splits available from NBA Stats.
Split | Constructor for a split. |
type SplitColumn = Text Source #
A column name in an NBA Stats split.
type StatsPath = ByteString Source #
A URL path for an NBA Stats resource.
type StatsParameters = [(ByteString, Maybe ByteString)] Source #
A collection of parameters that customize NBA Stats resources.
data StatsError Source #
An error which may be generated by this library.
StatsResponseDecodeFailure String | An HTTP response has invalid JSON. |
SplitNameNotFound String | A stats resource does not have a split matching the given split name. |
SplitKeyNotFound String | A split does not have a row matching the given column key and row value. |
SplitRowCardinalityInconsistent String | A split row has a different cardinality than the associated columns. |
SplitColumnNameNotFound String | A split does not have a column name matching the given key. |
SplitRowParseFailure String | A failure to parse a split row's tabular data to the destination type. |
Utility
domain :: ByteString Source #
The NBA Stats domain name.
getRequest :: StatsPath -> Request Source #
Generates an HTTP GET request like the ones used internally.