10 import Data.Either.Utils ( maybeToEither )
11 import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime )
12 import Data.Time.Format ( parseTime )
13 import System.Locale ( defaultTimeLocale )
14 import Text.Read ( readMaybe )
15 import Text.XML.HXT.Core (
25 -- | Parse the \"message\" element out of a document tree and return
26 -- it as an 'XmlTree'. We use an Either for consistency.
28 -- Note: It's more trouble than it's worth to attempt to use this as
29 -- the basis for parse_xmlfid and parse_xml_time_stamp.
31 parse_message :: XmlTree -> Either String XmlTree
32 parse_message xmltree =
34 [] -> Left "No message elements found."
37 parse :: XmlTree -> [XmlTree]
38 parse = runLA $ hasName "/" /> hasName "message"
40 elements = parse xmltree
44 -- | Extract the \"XML_File_ID\" element from a document. If we fail
45 -- to parse an XML_File_ID, we return the reason wrapped in a 'Left'
46 -- constructor. The reason should be one of two things:
48 -- 1. No XML_File_ID elements were found.
50 -- 2. An XML_File_ID element was found, but it could not be read
53 -- We use an Either rather than a Maybe because we do expect some
54 -- non-integer XML_File_IDs. In the examples, you will see
55 -- NHL_DepthChart_XML.XML with an XML_File_ID of \"49618.61\" and
56 -- CFL_Boxscore_XML1.xml with an XML_File_ID of
57 -- \"R28916\". According to Brijesh Patel of TSN, these are special
58 -- category files and not part of the usual feed.
60 -- TODO: This should eventually be combined with XML.parse_xmlfid
61 -- from the htsn package.
63 parse_xmlfid :: XmlTree -> Either String Integer
64 parse_xmlfid xmltree =
66 [] -> Left "No XML_File_ID elements found."
69 parse :: XmlTree -> [String]
70 parse = runLA $ hasName "/"
72 /> hasName "XML_File_ID"
76 read_either_integer :: String -> Either String Integer
77 read_either_integer s =
78 let msg = "Could not parse XML_File_ID " ++ s ++ " as an integer."
80 maybeToEither msg (readMaybe s)
82 elements = parse xmltree
83 parse_results = map read_either_integer elements
87 -- | The format string for times appearing in the feed.
90 time_format = "%I:%M %p"
92 -- | The format string for a time_stamp. This omits the leading and
94 time_stamp_format :: String
95 time_stamp_format = "%B %-d, %Y, at " ++ time_format ++ " ET"
98 -- | Parse a time stamp from a 'String' (maybe).
100 -- TSN doesn't provide a proper time zone name, so we assume that
101 -- it's always Eastern Standard Time. EST is UTC-5, so we
102 -- add five hours to convert to UTC.
104 parse_time_stamp :: String -> Maybe UTCTime
106 fmap add_five . parseTime defaultTimeLocale time_stamp_format
108 five_hours :: NominalDiffTime
109 five_hours = 5 * 60 * 60
111 add_five :: UTCTime -> UTCTime
112 add_five = addUTCTime five_hours
115 -- | Extract the \"time_stamp\" element from a document. If we fail
116 -- to parse a time_stamp, we return the reason wrapped in a 'Left'
117 -- constructor. The reason should be one of two things:
119 -- 1. No time_Stamp elements were found.
121 -- 2. A time_stamp element was found, but it could not be read
124 -- Unline 'parse_xmlfid', we don't expect to run into any time_stamps
125 -- that we can't parse. But since parse_xmlfid returns an Either, we
126 -- do for consistency.
128 parse_xml_time_stamp :: XmlTree -> Either String UTCTime
129 parse_xml_time_stamp xmltree =
130 case parse_results of
131 [] -> Left "No time_stamp elements found."
134 parse :: XmlTree -> [String]
135 parse = runLA $ hasName "/"
137 /> hasName "time_stamp"
141 read_either_utctime :: String -> Either String UTCTime
142 read_either_utctime s =
143 let msg = "Could not parse time_stamp " ++ s ++ " as a date/time."
145 maybeToEither msg (parse_time_stamp s)
147 elements = parse xmltree
148 parse_results = map read_either_utctime elements