module Web.Tweet.API where
import Control.Composition
import Control.Lens
import Control.Monad
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.Void
import Text.Megaparsec.Error
import Web.Tweet.Types
import Web.Tweet.Utils
import Web.Tweet.Utils.API
getMarkov :: String -> Maybe Int -> FilePath -> IO [String]
getMarkov = (fmap (map (view text))) .** getAll
getAll :: String -> Maybe Int -> FilePath -> IO Timeline
getAll sn maxId filepath = do
tweets <- either (error "Parse tweets failed") id <$> getProfileMax sn 200 filepath maxId
let lastId = _tweetId . last $ tweets
if (Just lastId) == maxId then
pure []
else
do
putStrLn $ "fetching tweets since " ++ show lastId ++ "..."
next <- getAll sn (Just lastId) filepath
pure (tweets ++ next)
tweetDataMem :: Tweet -> Config -> IO Int
tweetDataMem tweet config = do
let requestString = urlString tweet
bytes <- postRequestMem ("https://api.twitter.com/1.1/statuses/update.json" ++ requestString) config
putStrLn $ displayTimelineColor . either (error "failed to parse tweet") id . getTweets . BSL.toStrict $ bytes
pure . (view tweetId) . head . either (error "failed to parse tweet") id . getTweets . BSL.toStrict $ bytes
tweetData :: Tweet -> FilePath -> IO Int
tweetData tweet filepath = do
let requestString = urlString tweet
bytes <- postRequest ("https://api.twitter.com/1.1/statuses/update.json" ++ requestString) filepath
putStrLn $ displayTimelineColor . either (error "failed to parse tweet") id . getTweets . BSL.toStrict $ bytes
pure . (view tweetId) . head . either (error "failed to parse tweet") id . getTweets . BSL.toStrict $ bytes
getProfileMax :: String -> Int -> FilePath -> Maybe Int -> IO (Either (ParseError Char Void) Timeline)
getProfileMax = fmap (getTweets . BSL.toStrict) .*** getProfileRaw
getProfileMaxMem :: String -> Int -> Config -> Maybe Int -> IO (Either (ParseError Char Void) Timeline)
getProfileMaxMem = fmap (getTweets . BSL.toStrict) .*** getProfileRawMem
getProfileRaw :: String -> Int -> FilePath -> Maybe Int -> IO BSL.ByteString
getProfileRaw sn count filepath maxId = getRequest ("https://api.twitter.com/1.1/statuses/user_timeline.json" ++ requestString) filepath
where requestString = case maxId of {
(Just i) -> "?screen_name=" ++ sn ++ "&count=" ++ (show count) ++ "&max_id=" ++ (show i) ;
Nothing -> "?screen_name=" ++ sn ++ "&count=" ++ (show count) }
getProfileRawMem :: String -> Int -> Config -> Maybe Int -> IO BSL.ByteString
getProfileRawMem sn count config maxId = getRequestMem ("https://api.twitter.com/1.1/statuses/user_timeline.json" ++ requestString) config
where requestString = case maxId of {
(Just i) -> "?screen_name=" ++ sn ++ "&count=" ++ (show count) ++ "&max_id=" ++ (show i) ;
Nothing -> "?screen_name=" ++ sn ++ "&count=" ++ (show count) }
mentions :: Int -> FilePath -> IO (Either (ParseError Char Void) Timeline)
mentions = fmap (getTweets . BSL.toStrict) .* mentionsRaw
mentionsMem :: Int -> Config -> IO (Either (ParseError Char Void) Timeline)
mentionsMem = fmap (getTweets . BSL.toStrict) .* mentionsRawMem
mentionsRaw :: Int -> FilePath -> IO BSL.ByteString
mentionsRaw count = getRequest ("https://api.twitter.com/1.1/statuses/mentions_timeline.json" ++ requestString)
where requestString = "?count=" ++ (show count)
mentionsRawMem :: Int -> Config -> IO BSL.ByteString
mentionsRawMem count = getRequestMem ("https://api.twitter.com/1.1/statuses/mentions_timeline.json" ++ requestString)
where requestString = "?count=" ++ (show count)
getProfile :: String -> Int -> FilePath -> IO (Either (ParseError Char Void) Timeline)
getProfile sn count filepath = getProfileMax sn count filepath Nothing
mute :: String -> FilePath -> IO ()
mute = (fmap void) . muteUserRaw
muteMem :: String -> Config -> IO ()
muteMem = (fmap void) . muteUserRawMem
unmute :: String -> FilePath -> IO ()
unmute = (fmap void) . unmuteUserRaw
unmuteMem :: String -> Config -> IO ()
unmuteMem = (fmap void) . unmuteUserRawMem
muteUserRaw :: String -> FilePath -> IO BSL.ByteString
muteUserRaw sn = postRequest ("https://api.twitter.com/1.1/mutes/users/create.json?screen_name=" ++ sn)
muteUserRawMem :: String -> Config -> IO BSL.ByteString
muteUserRawMem sn = postRequestMem ("https://api.twitter.com/1.1/mutes/users/create.json?screen_name=" ++ sn)
unmuteUserRaw :: String -> FilePath -> IO BSL.ByteString
unmuteUserRaw sn = postRequest ("https://api.twitter.com/1.1/mutes/users/destroy.json?screen_name=" ++ sn)
unmuteUserRawMem :: String -> Config -> IO BSL.ByteString
unmuteUserRawMem sn = postRequestMem ("https://api.twitter.com/1.1/mutes/users/destroy.json?screen_name=" ++ sn)
getDMsRaw :: Show p => p -> FilePath -> IO BSL.ByteString
getDMsRaw count = getRequest ("https://api.twitter.com/1.1/direct_messages.json" ++ requestString)
where requestString = "?count=" ++ (show count)
getFavorites :: Int -> String -> FilePath -> IO (Either (ParseError Char Void) Timeline)
getFavorites count = fmap (fmap (take count)) . (fmap (getTweets . BSL.toStrict)) .* favoriteTweetListRaw
getTimeline :: Int -> FilePath -> IO (Either (ParseError Char Void) Timeline)
getTimeline = (fmap (getTweets . BSL.toStrict)) .* getTimelineRaw
getTimelineMem :: Int -> Config -> IO (Either (ParseError Char Void) Timeline)
getTimelineMem = (fmap (getTweets . BSL.toStrict)) .* getTimelineRawMem
getTimelineRaw :: Int -> FilePath -> IO BSL.ByteString
getTimelineRaw count = getRequest ("https://api.twitter.com/1.1/statuses/home_timeline.json" ++ requestString)
where requestString = "?count=" ++ (show count)
getTimelineRawMem :: Int -> Config -> IO BSL.ByteString
getTimelineRawMem count = getRequestMem ("https://api.twitter.com/1.1/statuses/home_timeline.json" ++ requestString)
where requestString = "?count=" ++ (show count)
deleteTweet :: Integer -> FilePath -> IO ()
deleteTweet = (fmap void) . deleteTweetRaw
deleteTweetMem :: Integer -> Config -> IO ()
deleteTweetMem = (fmap void) . deleteTweetRawMem
deleteTweetResponse :: Integer -> FilePath -> IO (Either (ParseError Char Void) Timeline)
deleteTweetResponse = fmap (getTweets . BSL.toStrict) .* deleteTweetRaw
deleteTweetResponseMem :: Integer -> Config -> IO (Either (ParseError Char Void) Timeline)
deleteTweetResponseMem = fmap (getTweets . BSL.toStrict) .* deleteTweetRawMem
favoriteTweet :: Integer -> FilePath -> IO ()
favoriteTweet = (fmap void) . favoriteTweetRaw
favoriteTweetMem :: Integer -> Config -> IO ()
favoriteTweetMem = (fmap void) . favoriteTweetRawMem
favoriteTweetList :: String -> FilePath -> IO (Either (ParseError Char Void) Timeline)
favoriteTweetList = fmap (getTweets . BSL.toStrict) .* favoriteTweetListRaw
favoriteTweetListMem :: String -> Config -> IO (Either (ParseError Char Void) Timeline)
favoriteTweetListMem = fmap (getTweets . BSL.toStrict) .* favoriteTweetListRawMem
favoriteTweetResponse :: Integer -> FilePath -> IO (Either (ParseError Char Void) Timeline)
favoriteTweetResponse = fmap (getTweets . BSL.toStrict) .* favoriteTweetRaw
unfavoriteTweet :: Integer -> FilePath -> IO ()
unfavoriteTweet = (fmap void) . unfavoriteTweetRaw
unfavoriteTweetMem :: Integer -> Config -> IO ()
unfavoriteTweetMem = (fmap void) . unfavoriteTweetRawMem
unfavoriteTweetResponse :: Integer -> FilePath -> IO (Either (ParseError Char Void) Timeline)
unfavoriteTweetResponse = fmap (getTweets . BSL.toStrict) .* unfavoriteTweetRaw
unfavoriteTweetResponseMem :: Integer -> Config -> IO (Either (ParseError Char Void) Timeline)
unfavoriteTweetResponseMem = fmap (getTweets . BSL.toStrict) .* unfavoriteTweetRawMem
unretweetTweet :: Integer -> FilePath -> IO ()
unretweetTweet = (fmap void) . unretweetTweetRaw
unretweetTweetMem :: Integer -> Config -> IO ()
unretweetTweetMem = (fmap void) . unretweetTweetRawMem
unretweetTweetResponse :: Integer -> FilePath -> IO (Either (ParseError Char Void) Timeline)
unretweetTweetResponse = fmap (getTweets . BSL.toStrict) .* unretweetTweetRaw
unretweetTweetResponseMem :: Integer -> Config -> IO (Either (ParseError Char Void) Timeline)
unretweetTweetResponseMem = fmap (getTweets . BSL.toStrict) .* unretweetTweetRawMem
unfollow :: String -> FilePath -> IO ()
unfollow = (fmap void) . unfollowUserRaw
unfollowMem :: String -> Config -> IO ()
unfollowMem = (fmap void) . unfollowUserRawMem
follow :: String -> FilePath -> IO ()
follow = (fmap void) . followUserRaw
followMem :: String -> Config -> IO ()
followMem = (fmap void) . followUserRawMem
block :: String -> FilePath -> IO ()
block = (fmap void) . blockUserRaw
blockMem :: String -> Config -> IO ()
blockMem = (fmap void) . blockUserRawMem
unblock :: String -> FilePath -> IO ()
unblock = (fmap void) . unblockUserRaw
unblockMem :: String -> Config -> IO ()
unblockMem = (fmap void) . unblockUserRawMem
retweetTweet :: Integer -> FilePath -> IO ()
retweetTweet = (fmap void) . retweetTweetRaw
retweetTweetMem :: Integer -> Config -> IO ()
retweetTweetMem = (fmap void) . retweetTweetRawMem
retweetTweetResponse :: Integer -> FilePath -> IO (Either (ParseError Char Void) Timeline)
retweetTweetResponse = fmap (getTweets . BSL.toStrict) .* retweetTweetRaw
retweetTweetResponseMem :: Integer -> Config -> IO (Either (ParseError Char Void) Timeline)
retweetTweetResponseMem = fmap (getTweets . BSL.toStrict) .* retweetTweetRawMem
favoriteTweetListRaw :: String -> FilePath -> IO BSL.ByteString
favoriteTweetListRaw sn = getRequest ("https://api.twitter.com/1.1/favorites/list.json?screen_name=" ++ sn)
favoriteTweetListRawMem :: String -> Config -> IO BSL.ByteString
favoriteTweetListRawMem sn = getRequestMem ("https://api.twitter.com/1.1/favorites/list.json?screen_name=" ++ sn)
favoriteTweetRaw :: Integer -> FilePath -> IO BSL.ByteString
favoriteTweetRaw idNum = postRequest ("https://api.twitter.com/1.1/favorites/create.json?id=" ++ (show idNum))
favoriteTweetRawMem :: Integer -> Config -> IO BSL.ByteString
favoriteTweetRawMem idNum = postRequestMem ("https://api.twitter.com/1.1/favorites/create.json?id=" ++ (show idNum))
retweetTweetRaw :: Integer -> FilePath -> IO BSL.ByteString
retweetTweetRaw idNum = postRequest ("https://api.twitter.com/1.1/statuses/retweet/" ++ (show idNum) ++ ".json")
retweetTweetRawMem :: Integer -> Config -> IO BSL.ByteString
retweetTweetRawMem idNum = postRequestMem ("https://api.twitter.com/1.1/statuses/retweet/" ++ (show idNum) ++ ".json")
sendDMRaw :: String -> [Char] -> FilePath -> IO BSL.ByteString
sendDMRaw txt sn = postRequest ("https://api.twitter.com/1.1/direct_messages/new.json?text=" ++ encoded ++ "&screen_name" ++ sn ++ ".json")
where encoded = strEncode txt
getDMs :: Int -> FilePath -> IO BSL.ByteString
getDMs count = getRequest ("https://dev.twitter.com/rest/reference/get/direct_messages.json?count=" ++ (show count))
getDMMem :: Int -> Config -> IO BSL.ByteString
getDMMem count = getRequestMem ("https://dev.twitter.com/rest/reference/get/direct_messages.json?count=" ++ (show count))
followUserRaw :: String -> FilePath -> IO BSL.ByteString
followUserRaw sn = postRequest ("https://api.twitter.com/1.1/friendships/create.json?screen_name=" ++ sn)
followUserRawMem :: String -> Config -> IO BSL.ByteString
followUserRawMem sn = postRequestMem ("https://api.twitter.com/1.1/friendships/create.json?screen_name=" ++ sn)
blockUserRaw :: String -> FilePath -> IO BSL.ByteString
blockUserRaw sn = postRequest ("https://api.twitter.com/1.1/blocks/create.json?screen_name=" ++ sn)
blockUserRawMem :: String -> Config -> IO BSL.ByteString
blockUserRawMem sn = postRequestMem ("https://api.twitter.com/1.1/blocks/create.json?screen_name=" ++ sn)
unblockUserRaw :: String -> FilePath -> IO BSL.ByteString
unblockUserRaw sn = postRequest ("https://api.twitter.com/1.1/blocks/destroy.json?screen_name=" ++ sn)
unblockUserRawMem :: String -> Config -> IO BSL.ByteString
unblockUserRawMem sn = postRequestMem ("https://api.twitter.com/1.1/blocks/destroy.json?screen_name=" ++ sn)
unfollowUserRaw :: String -> FilePath -> IO BSL.ByteString
unfollowUserRaw sn = postRequest ("https://api.twitter.com/1.1/friendships/destroy.json?screen_name=" ++ sn)
unfollowUserRawMem :: String -> Config -> IO BSL.ByteString
unfollowUserRawMem sn = postRequestMem ("https://api.twitter.com/1.1/friendships/destroy.json?screen_name=" ++ sn)
unretweetTweetRaw :: Integer -> FilePath -> IO BSL.ByteString
unretweetTweetRaw idNum = postRequest ("https://api.twitter.com/1.1/statuses/unretweet/" ++ (show idNum) ++ ".json")
unretweetTweetRawMem :: Integer -> Config -> IO BSL.ByteString
unretweetTweetRawMem idNum = postRequestMem ("https://api.twitter.com/1.1/statuses/unretweet/" ++ (show idNum) ++ ".json")
unfavoriteTweetRaw :: Integer -> FilePath -> IO BSL.ByteString
unfavoriteTweetRaw idNum = postRequest ("https://api.twitter.com/1.1/favorites/destroy.json?id=" ++ (show idNum))
unfavoriteTweetRawMem :: Integer -> Config -> IO BSL.ByteString
unfavoriteTweetRawMem idNum = postRequestMem ("https://api.twitter.com/1.1/favorites/destroy.json?id=" ++ (show idNum))
deleteTweetRaw :: Integer -> FilePath -> IO BSL.ByteString
deleteTweetRaw idNum = postRequest ("https://api.twitter.com/1.1/statuses/destroy/" ++ (show idNum) ++ ".json")
deleteTweetRawMem :: Integer -> Config -> IO BSL.ByteString
deleteTweetRawMem idNum = postRequestMem ("https://api.twitter.com/1.1/statuses/destroy/" ++ (show idNum) ++ ".json")