From e56575d5c4192df275a306d616762c913fe3197f Mon Sep 17 00:00:00 2001 From: horhik Date: Thu, 10 Feb 2022 09:57:23 +0300 Subject: [PATCH] Add untf8 support --- src/Main.hs | 38 ++++++++++++++++++++++++++------------ timecode-generator.cabal | 5 +++-- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index e4ebee7..881d009 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -10,15 +10,21 @@ import Network.HTTP.Conduit import Data.List.Split import Data.Algorithm.Diff import Data.Algorithm.DiffOutput -import qualified Data.ByteString.Lazy.Char8 as L8 +import Data.ByteString.Lazy.UTF8 (toString) data GeneratorState = IsRunning | IsFinished deriving (Eq) -getChanges :: Diff f -> Bool -getChanges (First _) = True -getChanges _ = False +isChanged :: Diff f -> Bool +isChanged (First _) = True +isChanged _ = False + +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 -- Should stop the script if there's a checkbox with a keyword `stop` showRunning :: [String] -> GeneratorState @@ -31,21 +37,29 @@ hasDiff :: [a] -> Bool hasDiff [] = False hasDiff _ = True + +genOutput :: Maybe String -> String -> IO () +genOutput (Just changes) time = do + putStrLn $ time ++ "\n " ++ changes +genOutput Nothing _ = return() + + timecodeGenerator :: GeneratorState -> [String] -> UTCTime -> IO () timecodeGenerator IsFinished _ _ = return () -timecodeGenerator IsRunning text time = do +timecodeGenerator IsRunning prevFile time = do html <- simpleHttp "https://hd.socks.town/s/h0jnEJQWy/download" currTime <- getCurrentTime - let body = (splitOn ("\n")) . L8.unpack $ html - print $ filter getChanges $ getDiff body text - print $ myFormatDiffTime (currTime, time) + let newFile = (splitOn ("\n")) . toString $ html + let changes = getChanges newFile prevFile + let diffTime = myFormatDiffTime (currTime, time) + genOutput changes diffTime -- Waiting for 1 second - threadDelay 1000000 + threadDelay 10000 -- Creating a loop until `IsFinished` - if showRunning body == IsRunning then - timecodeGenerator IsRunning body time + if showRunning newFile == IsRunning then + timecodeGenerator IsRunning newFile time else - timecodeGenerator IsFinished body time + timecodeGenerator IsFinished newFile time main :: IO () main = do diff --git a/timecode-generator.cabal b/timecode-generator.cabal index ac9142e..e2d460f 100644 --- a/timecode-generator.cabal +++ b/timecode-generator.cabal @@ -10,11 +10,12 @@ executable timecode-generator build-depends: base >= 4.14.3, bytestring, + utf8-string >= 1.0.2, aeson, time, split, + Diff, HTTP >= 4000, http-client>= 0.6.4.1, http-conduit >= 2.3.8, - tagsoup >= 0.14.6, - Diff + tagsoup >= 0.14.6