Add untf8 support

This commit is contained in:
horhik 2022-02-10 09:57:23 +03:00
parent 56291f80e6
commit e56575d5c4
2 changed files with 29 additions and 14 deletions

View file

@ -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

View file

@ -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