-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaste.hs
31 lines (26 loc) · 962 Bytes
/
Paste.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{-# LANGUAGE OverloadedStrings #-}
module Paste (paste) where
import Control.Exception
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding
import Network.HTTP.Simple
import Network.HTTP.Conduit
import Network.HTTP.Client.MultipartFormData
-- |Function to check if text should be pasted
shouldPaste :: Text -> Bool
shouldPaste text = T.length text > 140 || T.any (== '\n') text
paste :: Text -> IO Text
paste text = do
if shouldPaste text
then catch (pasteTo0x0 text) $ \e -> do
print (e :: IOException)
pure text
else pure text
pasteTo0x0 :: Text -> IO Text
pasteTo0x0 text = do
let formData = pure $ partFileRequestBody "file" "paste" $ RequestBodyBS $ encodeUtf8 text
request <- parseRequest "POST https://0x0.st"
request' <- formDataBody formData $ addRequestHeader "User-Agent" "smacbot" request
response <- httpBS request'
pure $ T.init $ decodeUtf8 $ responseBody response