1 {-# LANGUAGE NoMonomorphismRestriction #-}
3 -- | Functions and data for working with Twitter statuses.
7 import Control.Applicative ((<$>), (<*>))
8 import Control.Monad (liftM)
9 import Data.Aeson ((.:), FromJSON(..), Value(Object))
10 import Data.Maybe (fromMaybe, mapMaybe, isJust)
11 import Data.Monoid (mempty)
12 import Data.String.Utils (join, splitWs)
13 import Data.Text (pack)
14 import Data.Time (formatTime)
15 import Data.Time.Clock (UTCTime)
16 import Data.Time.Format (parseTime)
17 import Data.Time.LocalTime (TimeZone, utcToZonedTime)
18 import System.Locale (defaultTimeLocale, rfc822DateFormat)
20 import Text.Regex (matchRegex, mkRegex)
22 import StringUtils (listify)
25 data Status = Status {
26 created_at :: Maybe UTCTime,
34 type Timeline = [Status]
36 instance FromJSON Status where
37 parseJSON (Object t) =
39 liftM parse_status_time (t .: created_at_field) <*>
41 liftM isJustInt (t .: in_reply_to_status_id_field) <*>
42 (t .: retweeted_field) <*>
46 -- The typechecker flips out without this.
47 isJustInt :: Maybe Int -> Bool
50 created_at_field = pack "created_at"
52 in_reply_to_status_id_field = pack "in_reply_to_status_id"
53 retweeted_field = pack "retweeted"
54 text_field = pack "text"
55 user_field = pack "user"
60 parse_status_time :: String -> Maybe UTCTime
62 parseTime defaultTimeLocale status_format
64 -- | Should match e.g. "Sun Oct 24 18:21:41 +0000 2010"
65 status_format :: String
66 status_format = "%a %b %d %H:%M:%S %z %Y"
68 utc_time_to_rfc822 :: Maybe TimeZone -> UTCTime -> String
69 utc_time_to_rfc822 mtz utc =
72 Just tz -> foo $ utcToZonedTime tz utc
74 foo = formatTime defaultTimeLocale rfc822DateFormat
77 show_created_at :: Maybe TimeZone -> Status -> String
79 (fromMaybe "") . (fmap $ utc_time_to_rfc822 mtz) . created_at
81 -- | Returns a nicely-formatted String representing the given 'Status'
83 pretty_print :: Maybe TimeZone -> Status -> String
84 pretty_print mtz status =
89 replicate bar_length '-',
93 join "\n" user_timeline_urls,
96 sca = show_created_at mtz status
97 name = screen_name (user status)
98 user_timeline_urls = listify (make_user_timeline_urls status)
99 bar_length = (length name) + 3 + (length sca)
102 -- | Given a list of statuses, returns the greatest status_id
103 -- belonging to one of the statuses in the list.
104 get_max_status_id :: Timeline -> Integer
105 get_max_status_id statuses =
108 status_ids = map status_id statuses
111 -- | Parse one username from a word.
112 parse_username :: String -> Maybe String
113 parse_username word =
117 Just (first_match:_) -> Just first_match
119 username_regex = mkRegex "@([a-zA-Z0-9_]+)"
120 matches = matchRegex username_regex word
123 -- | Parse all usernames of the form \@username from a status.
124 parse_usernames_from_status :: Status -> [String]
125 parse_usernames_from_status status =
126 mapMaybe parse_username status_words
128 status_words = splitWs (text status)
130 -- | Get all referenced users' timeline URLs.
131 make_user_timeline_urls :: Status -> [String]
132 make_user_timeline_urls status =
133 map screen_name_to_timeline_url usernames
135 usernames = parse_usernames_from_status status
138 status_tests :: [Test]
139 status_tests = [ test_parse_usernames ]
142 test_parse_usernames :: Test
143 test_parse_usernames =
146 "All usernames are parsed."
150 dummy_user = User { screen_name = "nobody" }
151 dummy_status = Status { status_id = 1,
152 created_at = Nothing,
153 text = "Hypothesis: @donsbot and @bonus500 are two personalities belonging to the same person.",
159 actual_usernames = parse_usernames_from_status dummy_status
160 expected_usernames = ["donsbot", "bonus500"]