checking response to return 2xx

This commit is contained in:
horhik 2022-02-11 19:44:08 +03:00
parent 86d5adbc83
commit 0a055af3e2
1 changed files with 37 additions and 21 deletions

View File

@ -6,13 +6,13 @@ import System.Environment
import Data.Time.Clock import Data.Time.Clock
import Data.Time.Format import Data.Time.Format
import Data.Time.Clock.POSIX import Data.Time.Clock.POSIX
import Network.HTTP.Conduit (simpleHttp) import Network.HTTP.Conduit
import Network.HTTP.Simple (getResponseStatusCode, getResponseBody)
import Data.List.Split import Data.List.Split
import Data.Algorithm.Diff import Data.Algorithm.Diff
import Data.Algorithm.DiffOutput import Data.Algorithm.DiffOutput
import Data.ByteString.Lazy.UTF8 (toString) import Data.ByteString.Lazy.UTF8 (toString)
import System.IO import System.IO
data GeneratorState = IsRunning | IsFinished data GeneratorState = IsRunning | IsFinished
deriving (Eq) deriving (Eq)
@ -52,29 +52,41 @@ genOutput (Just changes) time timecodeFile= do
genOutput Nothing _ _ = return() genOutput Nothing _ _ = return()
-- Returning fetched url as a string separated by \n -- Returning fetched url as a string separated by \n
fetchFile :: String -> IO [String] fetchFile :: String -> IO (Either String [String])
fetchFile url = do fetchFile url = do
html <- simpleHttp url request <- parseRequest url
return $ (splitOn ("\n")) . toString $ html manager <- newManager tlsManagerSettings
response <- httpLbs request manager
let statusCode = getResponseStatusCode response
if statusCode `div` 100 == 2 then do
return $ Right ((splitOn ("\n")) . toString $ getResponseBody response)
else do
return $ Left (show statusCode)
-- Main Loop -- Main Loop
timecodeGenerator :: GeneratorState -> [String] -> UTCTime -> String -> String -> IO () timecodeGenerator :: GeneratorState -> [String] -> UTCTime -> String -> String -> IO ()
timecodeGenerator IsFinished _ _ _ _ = return () timecodeGenerator IsFinished _ _ _ _ = return ()
timecodeGenerator IsRunning prevFile time url timecodeFile = do timecodeGenerator IsRunning prevFile time url timecodeFile = do
newFile <- fetchFile url fetched <- fetchFile url
currTime <- getCurrentTime case fetched of
let changes = getChanges newFile prevFile Right newFile -> do
let diffTime = myFormatDiffTime (currTime, time) currTime <- getCurrentTime
let changes = getChanges newFile prevFile
let diffTime = myFormatDiffTime (currTime, time)
genOutput changes diffTime timecodeFile genOutput changes diffTime timecodeFile
-- Waiting for 1 second -- Waiting for 1 second
threadDelay 10000 threadDelay 10000
-- Creating a loop until `IsFinished` -- Creating a loop until `IsFinished`
if showRunning newFile == IsRunning then if showRunning newFile == IsRunning then
timecodeGenerator IsRunning newFile time url timecodeFile timecodeGenerator IsRunning newFile time url timecodeFile
else else
timecodeGenerator IsFinished newFile time url timecodeFile timecodeGenerator IsFinished newFile time url timecodeFile
Left err -> do
print err
timecodeGenerator IsRunning prevFile time url timecodeFile
-- Commandline arguments: -- Commandline arguments:
-- 1. Link to markdown file -- 1. Link to markdown file
-- 2. File to write timecodes to -- 2. File to write timecodes to
@ -84,9 +96,13 @@ main = do
let url:timecodeFile:xs = args let url:timecodeFile:xs = args
time <- getCurrentTime time <- getCurrentTime
file <- fetchFile url file <- fetchFile url
appendToFile timecodeFile $ "New Timecodes starts here \n " case file of
appendToFile timecodeFile $ show time Left e -> do
timecodeGenerator IsRunning file time url timecodeFile putStrLn $ (++) "Error: " $ show e
Right file -> do
appendToFile timecodeFile $ "New Timecodes starts here \n "
appendToFile timecodeFile $ show time
timecodeGenerator IsRunning file time url timecodeFile
--let url = "https://hd.socks.town/s/h0jnEJQWy/download" --let url = "https://hd.socks.town/s/h0jnEJQWy/download"
--let timecodeFile = "timecodes.md" --let timecodeFile = "timecodes.md"