From d7e5230c077e86b5aa2c87d19d03aa3430846ce4 Mon Sep 17 00:00:00 2001 From: depsterr Date: Fri, 9 Jul 2021 18:16:55 +0200 Subject: [PATCH] made ytdl function way prettier --- src/YTDL.hs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/YTDL.hs b/src/YTDL.hs index 5f4227e..82a3810 100644 --- a/src/YTDL.hs +++ b/src/YTDL.hs @@ -3,7 +3,6 @@ module YTDL (Resolution(..), ytdl) where import Config import Control.Monad.Trans.Reader -import Control.Monad.Trans.Class import Control.Concurrent (forkIO, threadDelay) import Data.Digest.Pure.MD5 import qualified Data.ByteString.Lazy.UTF8 as BCU @@ -37,8 +36,7 @@ resToArgs (PMAX) = ["-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4", "--merge resToArgs (Audio) = ["-x", "--audio-format", "mp3"] ytdl :: String -> Resolution -> ReaderT ViddlConfig IO (Either String FilePath) -ytdl url res = do - cfg <- ask +ytdl url res = ReaderT $ \cfg -> do let ext = case res of { Audio -> ".mp3"; _ -> ".mp4" } @@ -47,30 +45,30 @@ ytdl url res = do let dir = concat [dlDir cfg, "/", ident] let fileName = concat [dir, "/", ident, ext] - processed <- lift $ doesFileExist fileName + processed <- doesFileExist fileName if processed then do - lift $ putStrLn $ "Returning existing ident " <> ident + putStrLn $ "Returning existing ident " <> ident pure (Right fileName) else do - lift $ putStrLn $ "Processing new ident " <> ident + putStrLn $ "Processing new ident " <> ident - lift $ createDirectoryIfMissing True dir + createDirectoryIfMissing True dir - lift $ print (resToArgs res <> ["-o", fileName, url]) + print (resToArgs res <> ["-o", fileName, url]) - ytdlProc <- lift $ createProcess (proc "youtube-dl" (resToArgs res <> ["-o", fileName, url])) + ytdlProc <- createProcess (proc "youtube-dl" (resToArgs res <> ["-o", fileName, url])) case ytdlProc of (_, _, _, ph) -> do - exitCode <- lift $ waitForProcess ph + exitCode <- waitForProcess ph case exitCode of ExitSuccess -> do - exists <- lift $ doesFileExist fileName + exists <- doesFileExist fileName if exists then do -- wait 5 minutes and then delete the directory - _ <- lift $ forkIO $ threadDelay 300000000 >> removeDirectoryRecursive dir + _ <- forkIO $ threadDelay 300000000 >> removeDirectoryRecursive dir pure (Right fileName) else do -- removeDirectoryRecursive dir