]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Twitter/Status.hs
93b3f4e1cc2621cebac388766d567fa2401a0f0d
[dead/halcyon.git] / src / Twitter / Status.hs
1 module Twitter.Status
2 where
3
4 import Data.Maybe
5 import Text.XML.HaXml
6
7 import Twitter.User
8 import Twitter.Xml
9
10 data Status = Status { status_id :: Integer,
11 created_at :: String,
12 text :: String,
13 user :: User }
14 deriving (Show, Eq)
15
16
17
18 status_from_content :: Content -> (Maybe Status)
19 status_from_content content =
20
21 if (length status_ids) == 0
22 || (length created_ats) == 0
23 || (length texts) == 0
24 || (length users) == 0
25 then
26 Nothing
27 else
28 case first_status_id of
29 Nothing -> Nothing
30 (Just status_id_data) ->
31 case first_created_at of
32 Nothing -> Nothing
33 (Just created_at_data) ->
34 case first_user of
35 Nothing -> Nothing
36 (Just user_object) ->
37 case (reads status_id_data :: [(Integer, String)]) of
38 [] -> Nothing
39 parseresult:_ -> Just (Status (fst parseresult) created_at_data all_text user_object)
40
41 where
42 status_ids = (unique_id content)
43 first_status_id = get_char_data (status_ids !! 0)
44
45 created_ats = (status_created_at content)
46 first_created_at = get_char_data (created_ats !! 0)
47
48 texts = (status_text content)
49 all_text = concat $ catMaybes (map get_char_data texts)
50
51 users = (status_user content)
52 first_user = user_from_content (users !! 0)
53
54
55
56 parse_statuses :: String -> [Status]
57 parse_statuses xml_data =
58 catMaybes maybe_statuses
59 where
60 (Document _ _ root _) = xmlParse xml_file_name xml_data
61 root_elem = CElem root
62 status_elements = (all_statuses root_elem)
63 maybe_statuses = map status_from_content status_elements
64
65
66 -- This is a required parameter to the xmlParse function used in
67 -- error reporting. We're not parsing a function, though, so we
68 -- leave it blank.
69 xml_file_name :: String
70 xml_file_name = ""
71
72 pretty_print :: Status -> String
73 pretty_print status =
74 concat [ name,
75 " - ",
76 (created_at status),
77 "\n",
78 replicate ((length name) + 3 + (length (created_at status))) '-',
79 "\n",
80 replace_entities (text status),
81 "\n" ]
82 where
83 name = screen_name (user status)
84
85
86
87 get_max_status_id :: [Status] -> Integer
88 get_max_status_id statuses =
89 maximum status_ids
90 where
91 status_ids = map status_id statuses