Create separate fetchFile function and add some comments

This commit is contained in:
horhik 2022-02-10 10:08:19 +03:00
parent e56575d5c4
commit e03153780b

View file

@ -6,7 +6,7 @@ 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 import Network.HTTP.Conduit (simpleHttp)
import Data.List.Split import Data.List.Split
import Data.Algorithm.Diff import Data.Algorithm.Diff
import Data.Algorithm.DiffOutput import Data.Algorithm.DiffOutput
@ -20,38 +20,47 @@ isChanged :: Diff f -> Bool
isChanged (First _) = True isChanged (First _) = True
isChanged _ = False isChanged _ = False
hasDiff :: [a] -> Bool
hasDiff [] = False
hasDiff _ = True
-- Changes as one string if they exsists
getChanges :: [String] -> [String] -> Maybe String getChanges :: [String] -> [String] -> Maybe String
getChanges now before = if (hasDiff ch) then Just res else Nothing getChanges now before = if (hasDiff ch) then Just res else Nothing
where ch = filter isChanged diff where ch = filter isChanged diff
diff = getDiff now before diff = getDiff now before
res = foldr (\(First x) y -> x ++ " \n " ++ y) "" ch res = foldr (\(First x) y -> x ++ " \n " ++ y) "" ch
-- Should stop the script if there's a checkbox with a keyword `stop` -- TODO
-- Should return signal to stop the script if there's a checkbox with a keyword `stop`
showRunning :: [String] -> GeneratorState showRunning :: [String] -> GeneratorState
showRunning html = IsRunning showRunning html = IsRunning
-- Printing time diff in %H:%M:%S format
myFormatDiffTime :: (UTCTime, UTCTime) -> String myFormatDiffTime :: (UTCTime, UTCTime) -> String
myFormatDiffTime (a,b)= formatTime defaultTimeLocale "%H:%M:%S" . posixSecondsToUTCTime $ diffUTCTime a b myFormatDiffTime (a,b)= formatTime defaultTimeLocale "%H:%M:%S" . posixSecondsToUTCTime $ diffUTCTime a b
hasDiff :: [a] -> Bool -- Printing changes is they exsists
hasDiff [] = False
hasDiff _ = True
genOutput :: Maybe String -> String -> IO () genOutput :: Maybe String -> String -> IO ()
genOutput (Just changes) time = do genOutput (Just changes) time = do
putStrLn $ time ++ "\n " ++ changes putStrLn $ time ++ "\n " ++ changes
genOutput Nothing _ = return() genOutput Nothing _ = return()
fetchFile :: IO [String]
fetchFile = do
html <- simpleHttp "https://hd.socks.town/s/h0jnEJQWy/download"
return $ (splitOn ("\n")) . toString $ html
-- Main Loop
timecodeGenerator :: GeneratorState -> [String] -> UTCTime -> IO () timecodeGenerator :: GeneratorState -> [String] -> UTCTime -> IO ()
timecodeGenerator IsFinished _ _ = return () timecodeGenerator IsFinished _ _ = return ()
timecodeGenerator IsRunning prevFile time = do timecodeGenerator IsRunning prevFile time = do
html <- simpleHttp "https://hd.socks.town/s/h0jnEJQWy/download" newFile <- fetchFile
currTime <- getCurrentTime currTime <- getCurrentTime
let newFile = (splitOn ("\n")) . toString $ html
let changes = getChanges newFile prevFile let changes = getChanges newFile prevFile
let diffTime = myFormatDiffTime (currTime, time) let diffTime = myFormatDiffTime (currTime, time)
genOutput changes diffTime genOutput changes diffTime
-- Waiting for 1 second -- Waiting for 1 second
threadDelay 10000 threadDelay 10000
@ -64,4 +73,5 @@ timecodeGenerator IsRunning prevFile time = do
main :: IO () main :: IO ()
main = do main = do
time <- getCurrentTime time <- getCurrentTime
timecodeGenerator IsRunning [""] time file <- fetchFile
timecodeGenerator IsRunning file time