1 {-# LANGUAGE NoMonomorphismRestriction #-}
3 -- | Functions and data for working with Twitter statuses.
4 module Twitter.Status (
13 import Control.Applicative ( (<$>), (<*>) )
14 import Control.Monad ( liftM )
15 import Data.Aeson ( (.:), FromJSON(..), Value(Object) )
16 import Data.Maybe ( mapMaybe, isJust )
17 import Data.Monoid ( mempty )
18 import Data.String.Utils ( join, splitWs )
19 import Data.Text ( pack )
20 import Data.Time ( formatTime )
21 import Data.Time.Clock ( UTCTime )
22 import Data.Time.Format ( parseTime )
23 import Data.Time.LocalTime ( TimeZone, utcToZonedTime )
24 import System.Locale ( defaultTimeLocale, rfc822DateFormat )
25 import Test.Tasty ( TestTree, testGroup )
26 import Test.Tasty.HUnit ( (@?=), testCase )
27 import Text.Regex ( matchRegex, mkRegex )
29 import Html ( replace_entities )
30 import StringUtils ( listify )
31 import Twitter.User ( User(..), screen_name_to_timeline_url )
33 data Status = Status {
34 created_at :: Maybe UTCTime,
42 type Timeline = [Status]
44 instance FromJSON Status where
45 parseJSON (Object t) =
47 liftM parse_status_time (t .: created_at_field) <*>
49 liftM isJustInt (t .: in_reply_to_status_id_field) <*>
50 (t .: retweeted_field) <*>
51 liftM replace_entities (t .: text_field) <*>
54 -- The typechecker flips out without this.
55 isJustInt :: Maybe Int -> Bool
58 created_at_field = pack "created_at"
60 in_reply_to_status_id_field = pack "in_reply_to_status_id"
61 retweeted_field = pack "retweeted"
62 text_field = pack "text"
63 user_field = pack "user"
68 parse_status_time :: String -> Maybe UTCTime
70 parseTime defaultTimeLocale status_format
72 -- | Should match e.g. "Sun Oct 24 18:21:41 +0000 2010"
73 status_format :: String
74 status_format = "%a %b %d %H:%M:%S %z %Y"
76 utc_time_to_rfc822 :: Maybe TimeZone -> UTCTime -> String
77 utc_time_to_rfc822 mtz utc =
80 Just tz -> foo $ utcToZonedTime tz utc
82 foo = formatTime defaultTimeLocale rfc822DateFormat
85 show_created_at :: Maybe TimeZone -> Status -> String
87 (maybe "" (utc_time_to_rfc822 mtz)) . created_at
89 -- | Returns a nicely-formatted String representing the given 'Status'
92 pretty_print :: Maybe TimeZone -> Status -> String
93 pretty_print mtz status =
98 replicate bar_length '-',
102 join "\n" user_timeline_urls,
105 sca = show_created_at mtz status
106 name = screen_name (user status)
107 user_timeline_urls = listify (make_user_timeline_urls status)
108 bar_length = (length name) + 3 + (length sca)
111 -- | Given a list of statuses, returns the greatest status_id
112 -- belonging to one of the statuses in the list.
114 get_max_status_id :: Timeline -> Integer
115 get_max_status_id statuses =
118 status_ids = map status_id statuses
121 -- | 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.
136 parse_usernames_from_status :: Status -> [String]
137 parse_usernames_from_status status =
138 mapMaybe parse_username status_words
140 status_words = splitWs (text status)
143 -- | Get all referenced users' timeline URLs.
145 make_user_timeline_urls :: Status -> [String]
146 make_user_timeline_urls status =
147 map screen_name_to_timeline_url usernames
149 usernames = parse_usernames_from_status status
152 status_tests :: TestTree
154 testGroup "Status Tests" [ test_parse_usernames ]
157 test_parse_usernames :: TestTree
158 test_parse_usernames =
159 testCase description $ actual @?= expected
161 description = "all usernames are parsed"
163 dummy_user = User { screen_name = "nobody" }
164 dummy_text = "Hypothesis: @donsbot and @bonus500 are two " ++
165 "personalities belonging to the same person."
166 dummy_status = Status { status_id = 1,
167 created_at = Nothing,
174 actual = parse_usernames_from_status dummy_status
175 expected = ["donsbot", "bonus500"]