From 076e0f9319c5293f5196fe82446ed8474331dad3 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Wed, 30 Sep 2009 15:05:48 +0200 Subject: [PATCH] MOD refactoring haskell... --- src/Pompage.hs | 91 ++++++++++++++++++++++++++++++++++---------------- xml/test.xml | 14 ++++---- 2 files changed, 70 insertions(+), 35 deletions(-) diff --git a/src/Pompage.hs b/src/Pompage.hs index e3066f2..e89942b 100644 --- a/src/Pompage.hs +++ b/src/Pompage.hs @@ -10,26 +10,33 @@ import Text.Printf (printf) import Text.XML.Light import Control.Monad (foldM) +-- Some constants. +coversDir = "../img/covers" +movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"] +usage = "Usage : %s -d -x " + type Movies = [Movie] data Movie = Movie { - movieFiles :: [FilePath] - , movieId :: Int + movieId :: Int , movieTitle :: String , movieYear :: Maybe Int + , movieUserRating :: Maybe Float , movieDirectors :: [String] + , movieGenres :: [String] + , moviePlot :: String , movieActors :: [String] + , movieLength :: Maybe Int -- [min]. , movieCountries :: [String] - , movieLength :: Maybe Int - , movieUserRating :: Maybe Int - , moviePressRating :: Maybe Int - , movieGenres :: [String] - , movieSynopsis :: String - , movieBudget :: Int - , movieBudgetUnit :: String + , movieBudget :: Maybe (Int, String) -- (, ). + , movieFiles :: [FilePath] , movieUrl :: String + , movieSourceId :: String } deriving (Show) +movieSample = Movie 1 "Batman" (Just 1989) (Just 7.6) ["Tim Burton"] ["Action", "Crime", "Thriller"] "The Dark Knight of Gotham City begins his war on crime with his first major enemy being the clownishly homicidal Joker." ["Michael Keaton", "Jack Nicholson", "Kim Basinger"] (Just 126) ["USA", "UK"] Nothing ["/home/gburr/divx/batman.mkv"] "http://www.imdb.com/title/tt0096895/" "0096895" + + data Arg = XML | MovieDir deriving (Show, Eq) type Args = [(Arg, String)] @@ -37,19 +44,17 @@ main = do args <- getArgs progName <- getProgName case checkArgs $ readArgs args of - Nothing -> printf usage progName + Nothing -> printf (usage ++ "\n") progName Just args -> do let Just dir = lookup MovieDir args let Just xmlFile = lookup XML args paths <- moviePaths dir - movies <- readXMLFile xmlFile + movies <- catch (readXMLFile xmlFile) (\e -> return []) print movies - print paths - -coversDir = "../img/covers" -movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"] -usage = "Usage : %s -d -x \n" - + putStrLn "" + searchTest "pouet" + --print paths + checkArgs :: Maybe Args -> Maybe Args checkArgs Nothing = Nothing checkArgs (Just args) = @@ -70,7 +75,7 @@ readArgs (_) = Just [] moviePaths :: FilePath -> IO [FilePath] moviePaths dir = do - paths <- filePaths (\filename -> + paths <- filePaths (\filename -> any (`isSuffixOf` filename) movieExtenstions) dir -- Keep only the relative path. return $ map (drop $ Data.List.length dir + 1) paths @@ -83,7 +88,7 @@ filePaths predicat baseDir = do contents <- getDirectoryContents dir foldM (\acc entry -> do let absDir = dir entry - doesDirectoryExist absDir >>= \exists -> + doesDirectoryExist absDir >>= \exists -> if exists then do paths <- processDir absDir @@ -97,13 +102,14 @@ filePaths predicat baseDir = do [] (contents \\ ["..", "."]) +--- XML --- readXMLFile :: FilePath -> IO Movies readXMLFile file = do content <- readFile file let Just root = parseXMLDoc content return $ - foldl (\acc elem -> + foldl (\acc elem -> case elementXMLToMovie elem of Nothing -> acc Just movie -> movie : acc) @@ -111,32 +117,61 @@ readXMLFile file = do (elChildren root) elementXMLToMovie :: Element -> Maybe Movie -elementXMLToMovie elem = undefined +elementXMLToMovie element = + findAttr (simpleName "id") element>>= + \id -> findChild (simpleName "files") element >>= + \filesElement -> + let files = map strContent (findChildren (simpleName "file") filesElement) in + findChild (simpleName "title") element >>= + \titleElement -> + let title = strContent titleElement in + Just $ movieSample{movieId = read id, movieFiles = files, movieTitle = title} -- TODO + +simpleName :: String -> QName +simpleName s = QName s Nothing Nothing + {- -findAttr (QName "id" Nothing Nothing) elem of - Nothing -> acc - Just id -> --} + \a -> of + Nothing -> Nothing + Just id -> + findAttr (QName "id" Nothing Nothing) elem of + -} writeXMLFile :: Movies -> FilePath -> IO () writeXMLFile movies file = undefined + +--- Web --- + + filesPath :: FilePath -> IO [FilePath] filesPath basePath = undefined movieName :: FilePath -> String movieName = undefined +searchTest :: String -> IO () +searchTest name = do + result <- searchAMovie name moduleIMDB + return () + +moduleIMDB :: Module +moduleIMDB = Module { + search = \s -> undefined +} + -- Int is the module id. data SearchResult = OK (Int, Movie) | Many [(Int, String)] -- String is the name of the movie. | NotFound +-- TODO : add a socket parameter to all function. data Module = Module { search :: String -> IO SearchResult - -- Int is the module id. FilePath is a path to the image like "../img/4353" - , downloadImage :: Int -> FilePath + -- Int is the movie id. FilePath is a path to the image like "../img/4353" + -- , downloadImage :: Int -> FilePath + -- downloadInfo :: Int -> Movie -> IO Movie } {- @@ -145,6 +180,6 @@ data Module = Module { If there is many possibilities then it will ask the user. -} searchAMovie :: String -> Module -> IO SearchResult -searchAMovie filename mod = undefined +searchAMovie filename mod = search mod filename diff --git a/xml/test.xml b/xml/test.xml index e4bdf8b..bd9cb65 100644 --- a/xml/test.xml +++ b/xml/test.xml @@ -1,11 +1,11 @@ - - + + - - Rocky [x264 Aac] [Fr Eng] [Fr Eng].mkv - - Rocky Balboa + + Rocky [x264 Aac] [Fr Eng] [Fr Eng].mkv + + Rocky Balboa 2006 Sylvester Stallone @@ -422,4 +422,4 @@ http://www.allocine.fr/film/fichefilm_gen_cfilm=50152.html - \ No newline at end of file + \ No newline at end of file -- 2.43.0