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]
data Movie = Movie {
- files :: [FilePath]
- , id :: Int
- , title :: String
- , year :: Maybe Int
- , directors :: [String]
- , actors :: [String]
- , countries :: [String]
- , length :: Maybe Int
- , userRating :: Maybe Int
- , pressRating :: Maybe Int
- , genre :: [String]
- , synopsis :: String
- , budget :: Int
- , budgetUnit :: String
- , url :: String
+ movieFiles :: [FilePath]
+ , movieId :: Int
+ , movieTitle :: String
+ , movieYear :: Maybe Int
+ , movieDirectors :: [String]
+ , movieActors :: [String]
+ , movieCountries :: [String]
+ , movieLength :: Maybe Int
+ , movieUserRating :: Maybe Int
+ , moviePressRating :: Maybe Int
+ , movieGenres :: [String]
+ , movieSynopsis :: String
+ , movieBudget :: Int
+ , movieBudgetUnit :: String
+ , movieUrl :: String
} deriving (Show)
-data Arg = XML | MoviesDir deriving (Show, Eq)
+data Arg = XML | MovieDir deriving (Show, Eq)
type Args = [(Arg, String)]
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
+coversDir = "../img/covers"
movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"]
-usage = "Usage : %s -x <XML file> [-d <Movies dir>]\n"
+usage = "Usage : %s -d <Movies dir> -x <XML file>\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
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
+readXMLFile :: FilePath -> IO Movies
+readXMLFile file = do
+ content <- readFile file
+ let Just root = parseXMLDoc content
+ return $
+ foldl (\acc elem ->
+ case elementXMLToMovie elem of
+ Nothing -> acc
+ Just movie -> movie : acc)
+ []
+ (elChildren root)
+
+elementXMLToMovie :: Element -> Maybe Movie
+elementXMLToMovie elem = undefined
+{-
+findAttr (QName "id" Nothing Nothing) elem of
+ Nothing -> acc
+ Just id ->
+-}
writeXMLFile :: Movies -> FilePath -> IO ()