-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBusmtc.hs
65 lines (54 loc) · 2.05 KB
/
Busmtc.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Busmtc (
getBusList
, getBusDetails
, busListUrl
) where
import Network.HTTP
import Text.HTML.TagSoup
import Text.StringLike (StringLike)
busListUrl = "http://www.mtcbus.org/Routes.asp"
prebus = "http://www.mtcbus.org/Routes.asp?cboRouteCode="
postbus = "&submit=Search"
getBusDetails :: String -> IO ([String], [String])
getBusDetails b = do
a <- getHtmlPage $ constructurl b
return $ exBusDetails $ getStartEndSection "<Tr BGColor='#EAEAEA'>" "<A Href='#' OnClick='history.back();'>" a
-- |Extract the information about the particular bus given as a parameter
exBusDetails :: Show a => [Tag a] -> ([a], [a])
exBusDetails t = (busd, busp) where
a = filter isTagText t
busd = map fromTagText $ take 5 a
busp = alternate $ init $ map fromTagText $ drop 7 a
alternate (x:y:xs) = y:(alternate xs)
alternate _ = []
getBusList :: String -> IO [String]
getBusList url = do
a <- getHtmlPage url
return $ exBusList $ init $ getStartEndSection "<Option>" "</Select>" a
-- |Extract the list of buses
exBusList :: Show a => [Tag a] -> [a]
exBusList ts = foldl helpa [] ts where
helpa acc t = if isTagText t
then (fromTagText t):acc
else acc
constructurl :: String -> String
constructurl b = concat [prebus, b, postbus]
{- Helper Functions -}
-- |Gets the source of a remote html page
getHtmlPage :: String -> IO String
getHtmlPage url = resp
>>= (\r -> getResponseBody r)
>>= (\b -> return b)
where
resp = simpleHTTP req
req = getRequest url
-- |Given a starting string, ending string and the html string, the function returns all the tags, attributes and text within the starting and
-- ending tags
getStartEndSection :: (StringLike str, TagRep t) => t -> t -> str -> [Tag str]
getStartEndSection ss es s = tillend where
parsed = parseTags s
fromstart = head $ sections (~== ss) parsed
tillend = fst $ break (~== es) fromstart
saveHtmlPage :: String -> String -> IO ()
saveHtmlPage fn url = getHtmlPage url
>>= (\s -> writeFile fn s)