X-Git-Url: http://git.euphorik.ch/?p=pompage.git;a=blobdiff_plain;f=src%2FPompage.hs;h=e89942be2527411bd12af60b1668e893e56b7de2;hp=6cf4349929e3d19a99755a322fd9e24e6db38cf1;hb=HEAD;hpb=574c2c85c2925c3f80e40a75ca0d89d033c60993 diff --git a/src/Pompage.hs b/src/Pompage.hs index 6cf4349..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 { - 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 + movieId :: Int + , movieTitle :: String + , movieYear :: Maybe Int + , movieUserRating :: Maybe Float + , movieDirectors :: [String] + , movieGenres :: [String] + , moviePlot :: String + , movieActors :: [String] + , movieLength :: Maybe Int -- [min]. + , movieCountries :: [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,18 +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 - -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) = @@ -69,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 @@ -82,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 @@ -96,32 +102,76 @@ filePaths predicat baseDir = do [] (contents \\ ["..", "."]) +--- XML --- readXMLFile :: FilePath -> IO Movies -readXMLFile file = undefined +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 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 + {- - file <- readFile "../xml/test.xml" - --print $ parseXMLDoc file --} + \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 } {- @@ -130,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