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