timecodeg/src/Main.hs

78 lines
2.2 KiB
Haskell
Raw Normal View History

2022-02-08 19:20:34 +00:00
module Main where
import Data.Char
import Control.Concurrent
import System.Environment
import Data.Time.Clock
import Data.Time.Format
import Data.Time.Clock.POSIX
import Network.HTTP.Conduit (simpleHttp)
2022-02-08 19:20:34 +00:00
import Data.List.Split
import Data.Algorithm.Diff
import Data.Algorithm.DiffOutput
2022-02-10 06:57:23 +00:00
import Data.ByteString.Lazy.UTF8 (toString)
2022-02-08 19:20:34 +00:00
2022-02-08 19:20:34 +00:00
data GeneratorState = IsRunning | IsFinished
deriving (Eq)
2022-02-10 06:57:23 +00:00
isChanged :: Diff f -> Bool
isChanged (First _) = True
isChanged _ = False
hasDiff :: [a] -> Bool
hasDiff [] = False
hasDiff _ = True
-- Changes as one string if they exsists
2022-02-10 06:57:23 +00:00
getChanges :: [String] -> [String] -> Maybe String
getChanges now before = if (hasDiff ch) then Just res else Nothing
where ch = filter isChanged diff
diff = getDiff now before
res = foldr (\(First x) y -> x ++ " \n " ++ y) "" ch
2022-02-08 19:44:40 +00:00
-- TODO
-- Should return signal to stop the script if there's a checkbox with a keyword `stop`
2022-02-09 05:06:36 +00:00
showRunning :: [String] -> GeneratorState
2022-02-08 19:20:34 +00:00
showRunning html = IsRunning
-- Printing time diff in %H:%M:%S format
myFormatDiffTime :: (UTCTime, UTCTime) -> String
myFormatDiffTime (a,b)= formatTime defaultTimeLocale "%H:%M:%S" . posixSecondsToUTCTime $ diffUTCTime a b
-- Printing changes is they exsists
2022-02-10 06:57:23 +00:00
genOutput :: Maybe String -> String -> IO ()
genOutput (Just changes) time = do
putStrLn $ time ++ "\n " ++ changes
genOutput Nothing _ = return()
fetchFile :: IO [String]
fetchFile = do
html <- simpleHttp "https://hd.socks.town/s/h0jnEJQWy/download"
return $ (splitOn ("\n")) . toString $ html
-- Main Loop
2022-02-08 19:20:34 +00:00
timecodeGenerator :: GeneratorState -> [String] -> UTCTime -> IO ()
timecodeGenerator IsFinished _ _ = return ()
2022-02-10 06:57:23 +00:00
timecodeGenerator IsRunning prevFile time = do
newFile <- fetchFile
currTime <- getCurrentTime
2022-02-10 06:57:23 +00:00
let changes = getChanges newFile prevFile
let diffTime = myFormatDiffTime (currTime, time)
2022-02-10 06:57:23 +00:00
genOutput changes diffTime
2022-02-09 05:06:36 +00:00
-- Waiting for 1 second
2022-02-10 06:57:23 +00:00
threadDelay 10000
2022-02-09 05:06:36 +00:00
-- Creating a loop until `IsFinished`
2022-02-10 06:57:23 +00:00
if showRunning newFile == IsRunning then
timecodeGenerator IsRunning newFile time
2022-02-08 19:20:34 +00:00
else
2022-02-10 06:57:23 +00:00
timecodeGenerator IsFinished newFile time
2022-02-08 19:20:34 +00:00
main :: IO ()
main = do
time <- getCurrentTime
file <- fetchFile
timecodeGenerator IsRunning file time