timecodeg/src/Main.hs

54 lines
1.5 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
2022-02-08 19:20:34 +00:00
import Network.HTTP.Conduit
import Data.List.Split
import Data.Algorithm.Diff
import Data.Algorithm.DiffOutput
2022-02-09 05:06:36 +00:00
import qualified Data.ByteString.Lazy.Char8 as L8
2022-02-08 19:20:34 +00:00
2022-02-08 19:20:34 +00:00
data GeneratorState = IsRunning | IsFinished
deriving (Eq)
2022-02-08 19:44:40 +00:00
getChanges :: Diff f -> Bool
getChanges (First _) = True
getChanges _ = False
2022-02-09 05:06:36 +00:00
-- Should stop the script if there's a checkbox with a keyword `stop`
showRunning :: [String] -> GeneratorState
2022-02-08 19:20:34 +00:00
showRunning html = IsRunning
myFormatDiffTime :: (UTCTime, UTCTime) -> String
myFormatDiffTime (a,b)= formatTime defaultTimeLocale "%H:%M:%S" . posixSecondsToUTCTime $ diffUTCTime a b
hasDiff :: [a] -> Bool
hasDiff [] = False
hasDiff _ = True
2022-02-08 19:20:34 +00:00
timecodeGenerator :: GeneratorState -> [String] -> UTCTime -> IO ()
timecodeGenerator IsFinished _ _ = return ()
timecodeGenerator IsRunning text time = do
html <- simpleHttp "https://hd.socks.town/s/h0jnEJQWy/download"
currTime <- getCurrentTime
2022-02-08 19:20:34 +00:00
let body = (splitOn ("\n")) . L8.unpack $ html
2022-02-08 19:44:40 +00:00
print $ filter getChanges $ getDiff body text
print $ myFormatDiffTime (currTime, time)
2022-02-09 05:06:36 +00:00
-- Waiting for 1 second
2022-02-08 19:20:34 +00:00
threadDelay 1000000
2022-02-09 05:06:36 +00:00
-- Creating a loop until `IsFinished`
2022-02-08 19:20:34 +00:00
if showRunning body == IsRunning then
timecodeGenerator IsRunning body time
else
timecodeGenerator IsFinished body time
main :: IO ()
main = do
time <- getCurrentTime
timecodeGenerator IsRunning [""] time