From: Greg Burri Date: Sun, 12 Apr 2009 10:24:47 +0000 (+0200) Subject: MOD continuing of the Haskell version X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=574c2c85c2925c3f80e40a75ca0d89d033c60993;p=pompage.git MOD continuing of the Haskell version --- diff --git a/src/Pompage.hs b/src/Pompage.hs index 200da4c..6cf4349 100644 --- a/src/Pompage.hs +++ b/src/Pompage.hs @@ -1,8 +1,14 @@ import System.IO (readFile, FilePath(..)) +import System.Directory ( + getDirectoryContents + , doesDirectoryExist + ) +import System.FilePath (()) import System.Environment (getArgs, getProgName) import Data.List import Text.Printf (printf) import Text.XML.Light +import Control.Monad (foldM) type Movies = [Movie] @@ -24,7 +30,7 @@ data Movie = Movie { , url :: String } deriving (Show) -data Arg = XML | MoviesDir deriving (Show, Eq) +data Arg = XML | MovieDir deriving (Show, Eq) type Args = [(Arg, String)] main = do @@ -33,26 +39,27 @@ main = do case checkArgs $ readArgs args of Nothing -> printf usage progName Just args -> do - let dir = case lookup MoviesDir args of - Just d -> d - Nothing -> "." + let Just dir = lookup MovieDir args + let Just xmlFile = lookup XML args paths <- moviePaths dir - file <- readFile "../xml/test.xml" - print $ parseXMLDoc file + movies <- readXMLFile xmlFile + print movies + print paths movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"] -usage = "Usage : %s -x [-d ]\n" +usage = "Usage : %s -d -x \n" checkArgs :: Maybe Args -> Maybe Args checkArgs Nothing = Nothing -checkArgs (Just args) = if any (\(a, _) -> a == XML) args - then Just args - else Nothing +checkArgs (Just args) = + case (lookup XML args, lookup MovieDir args) of + (Just _, Just _) -> Just args + otherwise -> Nothing readArgs :: [String] -> Maybe Args readArgs (name:value:rest) | name == "-x" = (XML, value) <:> readArgs rest - | name == "-d" = (MoviesDir, value) <:> readArgs rest + | name == "-d" = (MovieDir, value) <:> readArgs rest | otherwise = Nothing where _ <:> Nothing = Nothing @@ -61,12 +68,41 @@ readArgs (_:[]) = Nothing readArgs (_) = Just [] moviePaths :: FilePath -> IO [FilePath] -moviePaths dir = undefined +moviePaths dir = do + paths <- filePaths (\filename -> + any (`isSuffixOf` filename) movieExtenstions) dir + -- Keep only the relative path. + return $ map (drop $ Data.List.length dir + 1) paths + +filePaths :: (String -> Bool) -> FilePath -> IO [FilePath] +filePaths predicat baseDir = do + processDir baseDir + where + processDir dir = do + contents <- getDirectoryContents dir + foldM (\acc entry -> do + let absDir = dir entry + doesDirectoryExist absDir >>= \exists -> + if exists + then do + paths <- processDir absDir + return (paths ++ acc) + else + if predicat entry + then + return (absDir : acc) + else + return acc) + [] + (contents \\ ["..", "."]) + readXMLFile :: FilePath -> IO Movies readXMLFile file = undefined - - +{- + file <- readFile "../xml/test.xml" + --print $ parseXMLDoc file +-} writeXMLFile :: Movies -> FilePath -> IO () writeXMLFile movies file = undefined diff --git a/start.sh b/start.sh old mode 100644 new mode 100755 index ceff83d..84e742f --- a/start.sh +++ b/start.sh @@ -1,3 +1,3 @@ #!/bin/bash cd src -ruby yopyop.rb -x ../xml/divx.xml -d /home/gburri/mininux/fat/Films #plop/BIG/Films #/media/BIG/Films +runhaskell Pompage.hs -x ../xml/divx.xml -d /home/gburri/mininux/fat/Films \ No newline at end of file