import Text.XML.Light
import Control.Monad (foldM)
+-- Some constants.
+coversDir = "../img/covers"
+movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"]
+usage = "Usage : %s -d <Movies dir> -x <XML file>"
+
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) -- (<budget>, <unit>).
+ , 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)]
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 <Movies dir> -x <XML file>\n"
-
+ putStrLn ""
+ searchTest "pouet"
+ --print paths
+
checkArgs :: Maybe Args -> Maybe Args
checkArgs Nothing = Nothing
checkArgs (Just args) =
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
contents <- getDirectoryContents dir
foldM (\acc entry -> do
let absDir = dir </> entry
- doesDirectoryExist absDir >>= \exists ->
+ doesDirectoryExist absDir >>= \exists ->
if exists
then do
paths <- processDir absDir
[]
(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)
(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
}
{-
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