clit 0.2.3.1 → 0.3.0.1
raw patch · 5 files changed
+104/−33 lines, 5 filesdep +MissingHdep +ansi-wl-pprintdep +megaparsecdep ~clit
Dependencies added: MissingH, ansi-wl-pprint, megaparsec
Dependency ranges changed: clit
Files
- clit.cabal +6/−3
- src/Web/Tweet.hs +12/−8
- src/Web/Tweet/Exec.hs +45/−17
- src/Web/Tweet/Types.hs +2/−5
- src/Web/Tweet/Utils.hs +39/−0
clit.cabal view
@@ -1,5 +1,5 @@ name: clit-version: 0.2.3.1+version: 0.3.0.1 cabal-version: >=1.10 build-type: Simple license: BSD3@@ -36,7 +36,10 @@ optparse-applicative >=0.13.1.0 && <0.14, lens >=4.15.1 && <4.16, data-default >=0.7.1.1 && <0.8,- text >=1.2.2.1 && <1.3+ text >=1.2.2.1 && <1.3,+ megaparsec >=5.2.0 && <5.3,+ ansi-wl-pprint >=0.6.7.3 && <0.7,+ MissingH >=1.4.0.1 && <1.5 default-language: Haskell2010 hs-source-dirs: src other-modules:@@ -48,7 +51,7 @@ main-is: Main.hs build-depends: base >=4.9.1.0 && <4.10,- clit >=0.2.3.1 && <0.3+ clit >=0.3.0.1 && <0.4 default-language: Haskell2010 hs-source-dirs: app ghc-options: -threaded -rtsopts -with-rtsopts=-N
src/Web/Tweet.hs view
@@ -29,6 +29,7 @@ -- * Functions to generate a URL string from a `Tweet` , urlString , getTimeline+ , showTimeline ) where import Network.HTTP.Client@@ -47,6 +48,7 @@ import Control.Lens import Web.Authenticate.OAuth import Web.Tweet.Sign+import Data.List.Utils -- | thread tweets together nicely. Takes a string, a list of handles to reply to, plus the ID of the status you're replying to. -- If you need to thread tweets without replying, pass a `Nothing` as the third argument.@@ -95,25 +97,27 @@ manager <- newManager tlsManagerSettings initialRequest <- parseRequest ("https://api.twitter.com/1.1/statuses/update.json" ++ requestString) request <- signRequest filepath $ initialRequest { method = "POST" }- response request manager+ responseInt request manager -getTimeline :: Int -> FilePath -> IO ()+showTimeline count color filepath = replace "\\/" "/" . fromRight . (fmap (if color then displayTimelineColor else displayTimeline)) <$> getTimeline count filepath+ where fromRight (Right a) = a+ getTimeline count filepath = do let requestString = "?count=" ++ (show count) manager <- newManager tlsManagerSettings initialRequest <- parseRequest ("https://api.twitter.com/1.1/statuses/home_timeline.json" ++ requestString) request <- signRequest filepath $ initialRequest { method = "GET" }- responseNull request manager+ getTweets . BSL.unpack <$> responseBS request manager -responseNull :: Request -> Manager -> IO ()-responseNull request manager = do+responseBS :: Request -> Manager -> IO BSL.ByteString+responseBS request manager = do response <- httpLbs request manager putStrLn $ "The status code was: " ++ show (statusCode $ responseStatus response)- BSL.putStrLn $ responseBody response+ pure . responseBody $ response -- | print output of a request and return status id as an `Int`. -response :: Request -> Manager -> IO Int-response request manager = do+responseInt :: Request -> Manager -> IO Int+responseInt request manager = do response <- httpLbs request manager putStrLn $ "The status code was: " ++ show (statusCode $ responseStatus response) BSL.putStrLn $ responseBody response
src/Web/Tweet/Exec.hs view
@@ -10,8 +10,10 @@ import Data.Monoid -- | Data type for our program: one optional path to a credential file, (optionally) the number of tweets to make, the id of the status you're replying to, and a list of users you wish to mention.-data Program = Program { cred :: Maybe FilePath , tweets :: Maybe Int, replyId :: Maybe String, replyHandles :: Maybe [String] }+data Program = Program { subcommand :: Command , cred :: Maybe FilePath } +data Command = Timeline { count :: Maybe Int , color :: Bool } | Send { tweets :: Maybe Int, replyId :: Maybe String, replyHandles :: Maybe [String] }+ -- | query twitter to post stdin with no fancy options fromStdIn :: Int -> FilePath -> IO () fromStdIn = threadStdIn [] Nothing@@ -28,33 +30,59 @@ where opts = info (helper <*> program) (fullDesc- <> progDesc "Tweet from stdin!"- <> header "clit - a Command Line Interface Tweeter")+ <> progDesc "Send from stdin!"+ <> header "clit - a Command Line Interface Sender") -- | Executes program select :: Program -> IO ()-select (Program Nothing (Just n) Nothing Nothing) = fromStdIn n ".cred"-select (Program Nothing Nothing Nothing Nothing) = fromStdIn 4 ".cred"-select (Program (Just file) (Just n) Nothing Nothing) = fromStdIn n file-select (Program (Just file) Nothing Nothing Nothing) = fromStdIn 4 file-select (Program Nothing (Just n) (Just id) (Just handles)) = threadStdIn handles (read id) n ".cred"-select (Program (Just file) (Just n) (Just id) (Just handles)) = threadStdIn handles (pure . read $ id) n file-select (Program (Just file) Nothing (Just id) (Just handles)) = threadStdIn handles (pure . read $ id) 4 file-select (Program Nothing (Just n) (Just id) Nothing) = threadStdIn [] (pure . read $ id) n ".cred"-select (Program Nothing Nothing (Just id) Nothing) = threadStdIn [] (pure . read $ id) 4 ".cred"-select (Program Nothing Nothing (Just id) (Just handles)) = threadStdIn handles (pure . read $ id) 4 ".cred"-select (Program (Just file) (Just n) (Just id) Nothing) = threadStdIn [] (pure . read $ id) n file-select (Program (Just file) (Just n) Nothing (Just handles)) = threadStdIn handles Nothing n file+select (Program (Send (Just n) Nothing Nothing) Nothing) = fromStdIn n ".cred"+select (Program (Send Nothing Nothing Nothing) Nothing) = fromStdIn 4 ".cred"+select (Program (Send (Just n) Nothing Nothing) (Just file)) = fromStdIn n file+select (Program (Send Nothing Nothing Nothing) (Just file) ) = fromStdIn 4 file+select (Program (Send (Just n) (Just id) (Just handles)) Nothing) = threadStdIn handles (read id) n ".cred"+select (Program (Send (Just n) (Just id) (Just handles)) (Just file)) = threadStdIn handles (pure . read $ id) n file+select (Program (Send Nothing (Just id) (Just handles)) (Just file)) = threadStdIn handles (pure . read $ id) 4 file+select (Program (Send (Just n) (Just id) Nothing) Nothing) = threadStdIn [] (pure . read $ id) n ".cred"+select (Program (Send Nothing (Just id) Nothing) Nothing) = threadStdIn [] (pure . read $ id) 4 ".cred"+select (Program (Send Nothing (Just id) (Just handles)) Nothing) = threadStdIn handles (pure . read $ id) 4 ".cred"+select (Program (Send (Just n) (Just id) Nothing) (Just file)) = threadStdIn [] (pure . read $ id) n file+select (Program (Send (Just n) Nothing (Just handles)) (Just file)) = threadStdIn handles Nothing n file+select (Program (Timeline Nothing False) Nothing) = putStrLn =<< showTimeline 8 False ".cred"+select (Program (Timeline Nothing False) (Just file)) = putStrLn =<< showTimeline 8 False file+select (Program (Timeline (Just n) False) (Just file)) = putStrLn =<< showTimeline 8 False file+select (Program (Timeline (Just n) False) Nothing) = putStrLn =<< showTimeline 8 False ".cred"+select (Program (Timeline Nothing True) Nothing) = putStrLn =<< showTimeline 8 True ".cred"+select (Program (Timeline Nothing True) (Just file)) = putStrLn =<< showTimeline 8 True file+select (Program (Timeline (Just n) True) (Just file)) = putStrLn =<< showTimeline 8 True file+select (Program (Timeline (Just n) True) Nothing) = putStrLn =<< showTimeline 8 True ".cred" -- | Parser to return a program datatype program :: Parser Program program = Program- <$> (optional $ strOption+ <$> (hsubparser+ (command "send" (info tweet (progDesc "Send a tweet"))+ <> command "view" (info timeline (progDesc "Get your timeline"))))+ <*> (optional $ strOption (long "cred" <> short 'c' <> metavar "CREDENTIALS" <> help "path to credentials"))- <*> (optional $ read <$> strOption++timeline :: Parser Command+timeline = Timeline+ <$> (optional $ read <$> strOption+ (long "count"+ <> short 'n'+ <> metavar "NUM"+ <> help "number of tweets to fetch, default 5"))+ <*> switch+ (long "color"+ <> short 'l'+ <> help "Display timeline with colorized terminal output.")++tweet :: Parser Command+tweet = Send+ <$> (optional $ read <$> strOption (long "tweets" <> short 't' <> metavar "NUM"
src/Web/Tweet/Types.hs view
@@ -22,11 +22,8 @@ , _replyID :: Maybe Int } deriving (Generic, Default) -data Timeline = Timeline- { _screenName :: String- , _includeRTs :: Maybe Bool- , _count :: Int- }+-- | Stores data like (text, screenName)+type Timeline = [(String, String)] makeLenses ''Tweet
src/Web/Tweet/Utils.hs view
@@ -2,6 +2,45 @@ module Web.Tweet.Utils where import qualified Data.ByteString.Char8 as BS+import Text.Megaparsec.String+import Text.Megaparsec+import Data.Monoid+import Web.Tweet.Types+import Control.Monad+import Text.PrettyPrint.ANSI.Leijen hiding ((<$>), char, (<>), string)++-- example text+-- Emma Watson, seins nus dans \u00abVanity Fair\u00bb, f\u00e9ministe ou hypocrite? \u27a1\ufe0f https:\/\/t.co\/MIPx1EpRSK https:\/\/t.co\/uSnLayoPi6+--"screen_name":"C_mrmt"+--toUnicode :: Parser String+--toUnicode "\u00e9" = ++displayTimelineColor :: Timeline -> String+displayTimelineColor ((user,content):rest) = ((show . yellow . text $ user) <> ":\n " <> content <> "\n") <> (displayTimeline rest)+displayTimelineColor [] = []++displayTimeline :: Timeline -> String+displayTimeline ((user,content):rest) = (user <> ":\n " <> content <> "\n") <> (displayTimeline rest) -- color should be configurable at least! +displayTimeline [] = []++-- | Get tweets from a response, disgarding all but author and +getTweets str = zip <$> (parse (filterStr "screen_name") "" str) <*> (parse (filterStr "text") "" str)++filterTl :: Parser Timeline+filterTl = zip <$> (filterStr "screen_name") <*> (filterStr "text")++filterStr :: String -> Parser [String]+filterStr str = (fmap (filter (/=""))) . many $+ filterTag str <|> (const "" <$> anyChar)++filterTag :: String -> Parser String+filterTag str = do+ string $ "\"" <> str <> "\""+ char ':'+ char '\"'+ want <- many $ noneOf "\""+ char '\"'+ pure want -- | helper function to get the key as read from a file keyLinePie :: String -> String