]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Twitter/Status.hs
59b98761fce86d62ed6fdaab46d8c950c19fb174
[dead/halcyon.git] / src / Twitter / Status.hs
1 -- |Functions and data for working with Twitter statuses.
2 module Twitter.Status
3 where
4
5 import Data.Maybe
6 import Text.XML.HaXml
7
8 import Twitter.User
9 import Twitter.Xml
10
11 -- |Represents one Twitter status. We don't care about any of their
12 -- other properties.
13 data Status = Status { status_id :: Integer,
14 created_at :: String,
15 text :: String,
16 user :: User }
17 deriving (Show, Eq)
18
19
20 -- |Given some XML content, create a 'Status' from it.
21 status_from_content :: Content -> (Maybe Status)
22 status_from_content content =
23
24 if (length status_ids) == 0
25 || (length created_ats) == 0
26 || (length texts) == 0
27 || (length users) == 0
28 then
29 Nothing
30 else
31 case first_status_id of
32 Nothing -> Nothing
33 (Just status_id_data) ->
34 case first_created_at of
35 Nothing -> Nothing
36 (Just created_at_data) ->
37 case first_user of
38 Nothing -> Nothing
39 (Just user_object) ->
40 case (reads status_id_data :: [(Integer, String)]) of
41 [] -> Nothing
42 parseresult:_ -> Just (Status (fst parseresult) created_at_data all_text user_object)
43
44 where
45 status_ids = (unique_id content)
46 first_status_id = get_char_data (status_ids !! 0)
47
48 created_ats = (status_created_at content)
49 first_created_at = get_char_data (created_ats !! 0)
50
51 texts = (status_text content)
52 all_text = concat $ catMaybes (map get_char_data texts)
53
54 users = (status_user content)
55 first_user = user_from_content (users !! 0)
56
57
58 -- |Takes an XML String as an argument, and returns the
59 -- status that was parsed from it. Should only be used
60 -- on XML string where a <status> is a top-level element.
61 parse_status :: String -> [Status]
62 parse_status xml_data =
63 catMaybes maybe_status
64 where
65 (Document _ _ root _) = xmlParse xml_file_name xml_data
66 root_elem = CElem root
67 status_element = (single_status root_elem)
68 maybe_status = map status_from_content status_element
69
70
71 -- |Takes an XML String as an argument, and returns the list of
72 -- statuses that can be parsed from it.
73 parse_statuses :: String -> [Status]
74 parse_statuses xml_data =
75 catMaybes maybe_statuses
76 where
77 (Document _ _ root _) = xmlParse xml_file_name xml_data
78 root_elem = CElem root
79 status_elements = (all_statuses root_elem)
80 maybe_statuses = map status_from_content status_elements
81
82
83 -- |This is a required parameter to the xmlParse function used in
84 -- error reporting. We're not parsing a function, though, so we leave
85 -- it blank.
86 xml_file_name :: String
87 xml_file_name = ""
88
89 -- |Returns a nicely-formatted String representing the given 'Status'
90 -- object.
91 pretty_print :: Status -> String
92 pretty_print status =
93 concat [ name,
94 " - ",
95 (created_at status),
96 "\n",
97 replicate ((length name) + 3 + (length (created_at status))) '-',
98 "\n",
99 replace_entities (text status),
100 "\n" ]
101 where
102 name = screen_name (user status)
103
104
105
106 -- |Given a list of statuses, returns the greatest status_id belonging
107 -- to one of the statuses in the list.
108 get_max_status_id :: [Status] -> Integer
109 get_max_status_id statuses =
110 maximum status_ids
111 where
112 status_ids = map status_id statuses