]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/Picklers.hs
Add a special pickler for the wacky team_ids.
[dead/htsn-import.git] / src / TSN / Picklers.hs
1 -- | (Un)picklers for data types present in The Sports Network XML
2 -- feed.
3 --
4 module TSN.Picklers (
5 xp_date,
6 xp_team_id )
7 where
8
9 import Data.Time.Clock ( UTCTime )
10 import Data.Time.Format ( formatTime, parseTime )
11 import System.Locale ( defaultTimeLocale )
12 import Text.Printf ( printf )
13 import Text.Read ( readMaybe )
14 import Text.XML.HXT.Arrow.Pickle (
15 XmlPickler(..),
16 xpText,
17 xpWrapMaybe )
18 import Text.XML.HXT.Arrow.Pickle.Xml ( PU )
19
20 instance XmlPickler Bool where
21 xpickle =
22 (to_bool, from_bool) `xpWrapMaybe` xpText
23 where
24 to_bool :: String -> Maybe Bool
25 to_bool = readMaybe
26
27 from_bool :: Bool -> String
28 from_bool = show
29
30
31 -- | (Un)pickle a UTCTime without the time portion.
32 --
33 xp_date :: PU UTCTime
34 xp_date =
35 (to_date, from_date) `xpWrapMaybe` xpText
36 where
37 format = "%-m/%-d/%Y"
38
39 to_date :: String -> Maybe UTCTime
40 to_date = parseTime defaultTimeLocale format
41
42 from_date :: UTCTime -> String
43 from_date = formatTime defaultTimeLocale format
44
45
46 -- | Parse a team_id. This *should* just be an 'Int', but TSN is doing
47 -- something weird. First of all, player IDs do look like normal
48 -- 'Int's. But the team IDs are all stuck in the triple digits, and
49 -- double-digit team IDs appear to be padded to three characters
50 -- with a leading '0'. So maybe they're treating these as text?
51 --
52 -- In any case, we do the simplest thing that is correct for all the
53 -- XML we've got: pad it to (only) three digits on pickling.
54 --
55 xp_team_id :: PU Int
56 xp_team_id =
57 (to_team_id, from_team_id) `xpWrapMaybe` xpText
58 where
59 to_team_id :: String -> Maybe Int
60 to_team_id = readMaybe
61
62 from_team_id :: Int -> String
63 from_team_id = printf "%03d"