add commandline arguments support

This commit is contained in:
horhik 2022-02-10 10:58:18 +03:00
parent db2c6750e0
commit e8eacc2f0c

View file

@ -31,8 +31,7 @@ getChanges now before = if (hasDiff ch) then Just res else Nothing
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
-- TODO -- TODO: Should return signal to stop the script if there's a checkbox with a keyword `stop`
-- 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
@ -41,11 +40,11 @@ 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
-- Printing changes is they exsists -- Printing changes is they exsists
genOutput :: Maybe String -> String -> IO () genOutput :: Maybe String -> String -> String -> IO ()
genOutput (Just changes) time = do genOutput (Just changes) time timecodeFile= do
file <- openFile "timecodes.md" AppendMode file <- openFile timecodeFile AppendMode
hPutStrLn file $ time ++ "\n " ++ changes hPutStrLn file $ time ++ "\n " ++ changes
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 [String]
@ -54,26 +53,33 @@ fetchFile url = do
return $ (splitOn ("\n")) . toString $ html return $ (splitOn ("\n")) . toString $ html
-- Main Loop -- Main Loop
timecodeGenerator :: GeneratorState -> [String] -> UTCTime -> String -> IO () timecodeGenerator :: GeneratorState -> [String] -> UTCTime -> String -> String -> IO ()
timecodeGenerator IsFinished _ _ _ = return () timecodeGenerator IsFinished _ _ _ _ = return ()
timecodeGenerator IsRunning prevFile time url = do timecodeGenerator IsRunning prevFile time url timecodeFile = do
newFile <- fetchFile url newFile <- fetchFile url
currTime <- getCurrentTime currTime <- getCurrentTime
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 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 timecodeGenerator IsRunning newFile time url timecodeFile
else else
timecodeGenerator IsFinished newFile time url timecodeGenerator IsFinished newFile time url timecodeFile
-- Commandline arguments:
-- 1. Link to markdown file
-- 2. File to write timecodes to
main :: IO () main :: IO ()
main = do main = do
let url = "https://hd.socks.town/s/h0jnEJQWy/download" args <- getArgs
let url:timecodeFile:xs = args
time <- getCurrentTime time <- getCurrentTime
file <- fetchFile url file <- fetchFile url
timecodeGenerator IsRunning file time url timecodeGenerator IsRunning file time url timecodeFile
--let url = "https://hd.socks.town/s/h0jnEJQWy/download"
--let timecodeFile = "timecodes.md"