1 -- |Functions and data for working with Twitter statuses.
6 import Data.String.Utils (join, splitWs)
8 import Text.Regex (matchRegex, mkRegex)
10 import Text.XML.HaXml.Posn (noPos)
12 import StringUtils (listify)
16 -- |Represents one Twitter status. We don't care about any of their
18 data Status = Status { status_id :: Integer,
25 -- |Given some XML content, create a 'Status' from it.
26 status_from_content :: Content i -> (Maybe Status)
27 status_from_content content =
29 if (length status_ids) == 0
30 || (length created_ats) == 0
31 || (length texts) == 0
32 || (length users) == 0
36 case first_status_id of
38 (Just status_id_data) ->
39 case first_created_at of
41 (Just created_at_data) ->
45 case (reads status_id_data :: [(Integer, String)]) of
47 parseresult:_ -> Just (Status (fst parseresult) created_at_data all_text user_object)
50 status_ids = (unique_id content)
51 first_status_id = get_char_data (status_ids !! 0)
53 created_ats = (status_created_at content)
54 first_created_at = get_char_data (created_ats !! 0)
56 texts = (status_text content)
57 all_text = concat $ catMaybes (map get_char_data texts)
59 users = (status_user content)
60 first_user = user_from_content (users !! 0)
63 -- |Takes an XML String as an argument, and returns the
64 -- status that was parsed from it. Should only be used
65 -- on XML string where a <status> is a top-level element.
66 parse_status :: String -> [Status]
67 parse_status xml_data =
68 catMaybes maybe_status
70 (Document _ _ root _) = xmlParse xml_file_name xml_data
71 root_elem = CElem root noPos
72 status_element = (single_status root_elem)
73 maybe_status = map status_from_content status_element
76 -- |Takes an XML String as an argument, and returns the list of
77 -- statuses that can be parsed from it.
78 parse_statuses :: String -> [Status]
79 parse_statuses xml_data =
80 catMaybes maybe_statuses
82 (Document _ _ root _) = xmlParse xml_file_name xml_data
83 root_elem = CElem root noPos
84 status_elements = (all_statuses root_elem)
85 maybe_statuses = map status_from_content status_elements
88 -- |This is a required parameter to the xmlParse function used in
89 -- error reporting. We're not parsing a function, though, so we leave
91 xml_file_name :: String
94 -- |Returns a nicely-formatted String representing the given 'Status'
96 pretty_print :: Status -> String
102 replicate ((length name) + 3 + (length (created_at status))) '-',
104 replace_entities (text status),
106 join "\n" user_timeline_urls,
109 name = screen_name (user status)
110 user_timeline_urls = listify (make_user_timeline_urls status)
113 -- |Given a list of statuses, returns the greatest status_id belonging
114 -- to one of the statuses in the list.
115 get_max_status_id :: [Status] -> Integer
116 get_max_status_id statuses =
119 status_ids = map status_id statuses
122 -- |Parse one username from a word.
123 parse_username :: String -> Maybe String
124 parse_username word =
128 Just (first_match:_) -> Just first_match
130 username_regex = mkRegex "@([a-zA-Z0-9_]+)"
131 matches = matchRegex username_regex word
134 -- |Parse all usernames of the form \@username from a status.
135 parse_usernames_from_status :: Status -> [String]
136 parse_usernames_from_status status =
137 catMaybes (map parse_username status_words)
139 status_words = splitWs (text status)
141 -- |Get all referenced users' timeline URLs.
142 make_user_timeline_urls :: Status -> [String]
143 make_user_timeline_urls status =
144 map screen_name_to_timeline_url usernames
146 usernames = parse_usernames_from_status status
149 status_tests :: [Test]
150 status_tests = [ test_parse_usernames ]
153 test_parse_usernames :: Test
154 test_parse_usernames =
155 TestCase $ assertEqual "All usernames are parsed." expected_usernames actual_usernames
157 dummy_user = User { screen_name = "nobody" }
158 dummy_status = Status { status_id = 1,
159 created_at = "never",
160 text = "Hypothesis: @donsbot and @bonus500 are two personalities belonging to the same person.",
163 actual_usernames = parse_usernames_from_status dummy_status
164 expected_usernames = ["donsbot", "bonus500"]