6cf4349929e3d19a99755a322fd9e24e6db38cf1
[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 files :: [FilePath]
17 , id :: Int
18 , title :: String
19 , year :: Maybe Int
20 , directors :: [String]
21 , actors :: [String]
22 , countries :: [String]
23 , length :: Maybe Int
24 , userRating :: Maybe Int
25 , pressRating :: Maybe Int
26 , genre :: [String]
27 , synopsis :: String
28 , budget :: Int
29 , budgetUnit :: String
30 , url :: 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 movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"]
50 usage = "Usage : %s -d <Movies dir> -x <XML file>\n"
51
52 checkArgs :: Maybe Args -> Maybe Args
53 checkArgs Nothing = Nothing
54 checkArgs (Just args) =
55 case (lookup XML args, lookup MovieDir args) of
56 (Just _, Just _) -> Just args
57 otherwise -> Nothing
58
59 readArgs :: [String] -> Maybe Args
60 readArgs (name:value:rest)
61 | name == "-x" = (XML, value) <:> readArgs rest
62 | name == "-d" = (MovieDir, value) <:> readArgs rest
63 | otherwise = Nothing
64 where
65 _ <:> Nothing = Nothing
66 arg <:> Just args = Just $ arg : args
67 readArgs (_:[]) = Nothing
68 readArgs (_) = Just []
69
70 moviePaths :: FilePath -> IO [FilePath]
71 moviePaths dir = do
72 paths <- filePaths (\filename ->
73 any (`isSuffixOf` filename) movieExtenstions) dir
74 -- Keep only the relative path.
75 return $ map (drop $ Data.List.length dir + 1) paths
76
77 filePaths :: (String -> Bool) -> FilePath -> IO [FilePath]
78 filePaths predicat baseDir = do
79 processDir baseDir
80 where
81 processDir dir = do
82 contents <- getDirectoryContents dir
83 foldM (\acc entry -> do
84 let absDir = dir </> entry
85 doesDirectoryExist absDir >>= \exists ->
86 if exists
87 then do
88 paths <- processDir absDir
89 return (paths ++ acc)
90 else
91 if predicat entry
92 then
93 return (absDir : acc)
94 else
95 return acc)
96 []
97 (contents \\ ["..", "."])
98
99
100 readXMLFile :: FilePath -> IO Movies
101 readXMLFile file = undefined
102 {-
103 file <- readFile "../xml/test.xml"
104 --print $ parseXMLDoc file
105 -}
106
107 writeXMLFile :: Movies -> FilePath -> IO ()
108 writeXMLFile movies file = undefined
109
110 filesPath :: FilePath -> IO [FilePath]
111 filesPath basePath = undefined
112
113 movieName :: FilePath -> String
114 movieName = undefined
115
116 -- Int is the module id.
117 data SearchResult = OK (Int, Movie)
118 | Many [(Int, String)] -- String is the name of the movie.
119 | NotFound
120
121 data Module = Module {
122 search :: String -> IO SearchResult
123 -- Int is the module id. FilePath is a path to the image like "../img/4353"
124 , downloadImage :: Int -> FilePath
125 }
126
127 {-
128 Gets a movie by asking the given module to find a movie.
129 A movie is seeked by its the filename.
130 If there is many possibilities then it will ask the user.
131 -}
132 searchAMovie :: String -> Module -> IO SearchResult
133 searchAMovie filename mod = undefined
134
135