kawhi-0.2.1: stats.NBA.com library

CopyrightAaron Taylor, 2016
LicenseMIT
Maintaineraaron@hamsterdam.co
Safe HaskellNone
LanguageHaskell2010

Data.NBA.Stats

Contents

Description

Functions and types for interacting with NBA Stats.

Synopsis

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 Data.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

getSplitRows Source #

Arguments

:: 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.

getSplitRow Source #

Arguments

:: (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

getSplitRowsGeneric Source #

Arguments

:: (MonadIO m, MonadHttp m, MonadError StatsError m, MonadThrow 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.

getSplitRowGeneric Source #

Arguments

:: (MonadIO m, MonadHttp m, MonadError StatsError m, MonadThrow 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

data Stats Source #

An NBA Stats resource.

This type represents the top-level JSON object returned from the NBA Stats REST API.

Constructors

Stats

Constructor for stats resource.

Fields

data Split Source #

An NBA Stats split.

This type represents splits available from NBA Stats.

Constructors

Split

Constructor for a split.

Fields

type SplitName = Text Source #

An NBA Stats split name.

type SplitColumn = Text Source #

A column name in an NBA Stats split.

type SplitRow = [Value] Source #

A row of data 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.

Constructors

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.