X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTwitter%2FStatus.hs;h=01ef0ab04504596881188ebde26373281d42a1a3;hb=230072d26d55aed92737308aa04ce8a0daa0b71a;hp=59b98761fce86d62ed6fdaab46d8c950c19fb174;hpb=27c6a7e62a428ceb1d2a60d456b075feee196da9;p=dead%2Fhalcyon.git diff --git a/src/Twitter/Status.hs b/src/Twitter/Status.hs index 59b9876..01ef0ab 100644 --- a/src/Twitter/Status.hs +++ b/src/Twitter/Status.hs @@ -3,8 +3,15 @@ module Twitter.Status where import Data.Maybe +import Data.String.Utils (join, splitWs) +import Data.Time (ZonedTime, formatTime, readsTime) +import System.Locale (defaultTimeLocale, rfc822DateFormat) +import Test.HUnit +import Text.Regex (matchRegex, mkRegex) import Text.XML.HaXml +import Text.XML.HaXml.Posn (noPos) +import StringUtils (listify) import Twitter.User import Twitter.Xml @@ -18,7 +25,7 @@ data Status = Status { status_id :: Integer, -- |Given some XML content, create a 'Status' from it. -status_from_content :: Content -> (Maybe Status) +status_from_content :: Content i -> (Maybe Status) status_from_content content = if (length status_ids) == 0 @@ -63,7 +70,7 @@ parse_status xml_data = catMaybes maybe_status where (Document _ _ root _) = xmlParse xml_file_name xml_data - root_elem = CElem root + root_elem = CElem root noPos status_element = (single_status root_elem) maybe_status = map status_from_content status_element @@ -75,7 +82,7 @@ parse_statuses xml_data = catMaybes maybe_statuses where (Document _ _ root _) = xmlParse xml_file_name xml_data - root_elem = CElem root + root_elem = CElem root noPos status_elements = (all_statuses root_elem) maybe_statuses = map status_from_content status_elements @@ -86,6 +93,21 @@ parse_statuses xml_data = xml_file_name :: String xml_file_name = "" + +created_at_to_rfc822 :: String -> Maybe String +created_at_to_rfc822 s = + case reads_result of + [(t,_)] -> + Just $ formatTime defaultTimeLocale rfc822DateFormat t + _ -> Nothing + where + -- Should match e.g. "Sun Oct 24 18:21:41 +0000 2010" + fmt :: String + fmt = "%a %b %d %H:%M:%S %z %Y" + + reads_result :: [(ZonedTime, String)] + reads_result = readsTime defaultTimeLocale fmt s + -- |Returns a nicely-formatted String representing the given 'Status' -- object. pretty_print :: Status -> String @@ -97,10 +119,12 @@ pretty_print status = replicate ((length name) + 3 + (length (created_at status))) '-', "\n", replace_entities (text status), + "\n\n", + join "\n" user_timeline_urls, "\n" ] where name = screen_name (user status) - + user_timeline_urls = listify (make_user_timeline_urls status) -- |Given a list of statuses, returns the greatest status_id belonging @@ -110,3 +134,48 @@ get_max_status_id statuses = maximum status_ids where status_ids = map status_id statuses + + +-- |Parse one username from a word. +parse_username :: String -> Maybe String +parse_username word = + case matches of + Nothing -> Nothing + Just [] -> Nothing + Just (first_match:_) -> Just first_match + where + username_regex = mkRegex "@([a-zA-Z0-9_]+)" + matches = matchRegex username_regex word + + +-- |Parse all usernames of the form \@username from a status. +parse_usernames_from_status :: Status -> [String] +parse_usernames_from_status status = + catMaybes (map parse_username status_words) + where + status_words = splitWs (text status) + +-- |Get all referenced users' timeline URLs. +make_user_timeline_urls :: Status -> [String] +make_user_timeline_urls status = + map screen_name_to_timeline_url usernames + where + usernames = parse_usernames_from_status status + + +status_tests :: [Test] +status_tests = [ test_parse_usernames ] + + +test_parse_usernames :: Test +test_parse_usernames = + TestCase $ assertEqual "All usernames are parsed." expected_usernames actual_usernames + where + dummy_user = User { screen_name = "nobody" } + dummy_status = Status { status_id = 1, + created_at = "never", + text = "Hypothesis: @donsbot and @bonus500 are two personalities belonging to the same person.", + user = dummy_user } + + actual_usernames = parse_usernames_from_status dummy_status + expected_usernames = ["donsbot", "bonus500"]