ADD beginning of the XML parsing
[pompage.git] / src / Pompage.hs
1 import System.IO (readFile, FilePath(..))
2 import System.Directory (
3 getDirectoryContents
4 , doesDirectoryExist
5 )
6 import System.FilePath ((</>))
7 import System.Environment (getArgs, getProgName)
8 import Data.List
9 import Text.Printf (printf)
10 import Text.XML.Light
11 import Control.Monad (foldM)
12
13 type Movies = [Movie]
14
15 data Movie = Movie {
16 movieFiles :: [FilePath]
17 , movieId :: Int
18 , movieTitle :: String
19 , movieYear :: Maybe Int
20 , movieDirectors :: [String]
21 , movieActors :: [String]
22 , movieCountries :: [String]
23 , movieLength :: Maybe Int
24 , movieUserRating :: Maybe Int
25 , moviePressRating :: Maybe Int
26 , movieGenres :: [String]
27 , movieSynopsis :: String
28 , movieBudget :: Int
29 , movieBudgetUnit :: String
30 , movieUrl :: String
31 } deriving (Show)
32
33 data Arg = XML | MovieDir deriving (Show, Eq)
34 type Args = [(Arg, String)]
35
36 main = do
37 args <- getArgs
38 progName <- getProgName
39 case checkArgs $ readArgs args of
40 Nothing -> printf usage progName
41 Just args -> do
42 let Just dir = lookup MovieDir args
43 let Just xmlFile = lookup XML args
44 paths <- moviePaths dir
45 movies <- readXMLFile xmlFile
46 print movies
47 print paths
48
49 coversDir = "../img/covers"
50 movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"]
51 usage = "Usage : %s -d <Movies dir> -x <XML file>\n"
52
53 checkArgs :: Maybe Args -> Maybe Args
54 checkArgs Nothing = Nothing
55 checkArgs (Just args) =
56 case (lookup XML args, lookup MovieDir args) of
57 (Just _, Just _) -> Just args
58 otherwise -> Nothing
59
60 readArgs :: [String] -> Maybe Args
61 readArgs (name:value:rest)
62 | name == "-x" = (XML, value) <:> readArgs rest
63 | name == "-d" = (MovieDir, value) <:> readArgs rest
64 | otherwise = Nothing
65 where
66 _ <:> Nothing = Nothing
67 arg <:> Just args = Just $ arg : args
68 readArgs (_:[]) = Nothing
69 readArgs (_) = Just []
70
71 moviePaths :: FilePath -> IO [FilePath]
72 moviePaths dir = do
73 paths <- filePaths (\filename ->
74 any (`isSuffixOf` filename) movieExtenstions) dir
75 -- Keep only the relative path.
76 return $ map (drop $ Data.List.length dir + 1) paths
77
78 filePaths :: (String -> Bool) -> FilePath -> IO [FilePath]
79 filePaths predicat baseDir = do
80 processDir baseDir
81 where
82 processDir dir = do
83 contents <- getDirectoryContents dir
84 foldM (\acc entry -> do
85 let absDir = dir </> entry
86 doesDirectoryExist absDir >>= \exists ->
87 if exists
88 then do
89 paths <- processDir absDir
90 return (paths ++ acc)
91 else
92 if predicat entry
93 then
94 return (absDir : acc)
95 else
96 return acc)
97 []
98 (contents \\ ["..", "."])
99
100
101 readXMLFile :: FilePath -> IO Movies
102 readXMLFile file = do
103 content <- readFile file
104 let Just root = parseXMLDoc content
105 return $
106 foldl (\acc elem ->
107 case elementXMLToMovie elem of
108 Nothing -> acc
109 Just movie -> movie : acc)
110 []
111 (elChildren root)
112
113 elementXMLToMovie :: Element -> Maybe Movie
114 elementXMLToMovie elem = undefined
115 {-
116 findAttr (QName "id" Nothing Nothing) elem of
117 Nothing -> acc
118 Just id ->
119 -}
120
121
122 writeXMLFile :: Movies -> FilePath -> IO ()
123 writeXMLFile movies file = undefined
124
125 filesPath :: FilePath -> IO [FilePath]
126 filesPath basePath = undefined
127
128 movieName :: FilePath -> String
129 movieName = undefined
130
131 -- Int is the module id.
132 data SearchResult = OK (Int, Movie)
133 | Many [(Int, String)] -- String is the name of the movie.
134 | NotFound
135
136 data Module = Module {
137 search :: String -> IO SearchResult
138 -- Int is the module id. FilePath is a path to the image like "../img/4353"
139 , downloadImage :: Int -> FilePath
140 }
141
142 {-
143 Gets a movie by asking the given module to find a movie.
144 A movie is seeked by its the filename.
145 If there is many possibilities then it will ask the user.
146 -}
147 searchAMovie :: String -> Module -> IO SearchResult
148 searchAMovie filename mod = undefined
149
150