1 import System
.IO
(readFile
, FilePath
(..))
2 import System
.Directory
(
6 import System
.FilePath
((</>))
7 import System
.Environment
(getArgs
, getProgName
)
9 import Text
.Printf
(printf
)
11 import Control
.Monad
(foldM
)
20 , directors ::
[String
]
22 , countries ::
[String
]
24 , userRating :: Maybe Int
25 , pressRating :: Maybe Int
29 , budgetUnit :: String
33 data Arg
= XML
| MovieDir
deriving (Show
, Eq
)
34 type Args
= [(Arg
, String
)]
38 progName
<- getProgName
39 case checkArgs
$ readArgs args
of
40 Nothing
-> printf usage progName
42 let Just dir
= lookup MovieDir args
43 let Just xmlFile
= lookup XML args
44 paths
<- moviePaths dir
45 movies
<- readXMLFile xmlFile
49 movieExtenstions
= ["avi", "mkv", "rmvb", "ogm", "divx"]
50 usage
= "Usage : %s -d <Movies dir> -x <XML file>\n"
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
59 readArgs ::
[String
] -> Maybe Args
60 readArgs
(name:value:rest
)
61 | name
== "-x" = (XML
, value
) <:
> readArgs rest
62 | name
== "-d" = (MovieDir
, value
) <:
> readArgs rest
65 _
<:
> Nothing
= Nothing
66 arg
<:
> Just args
= Just
$ arg : args
67 readArgs
(_:
[]) = Nothing
68 readArgs
(_
) = Just
[]
70 moviePaths :: FilePath
-> IO
[FilePath
]
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
77 filePaths ::
(String
-> Bool
) -> FilePath
-> IO
[FilePath
]
78 filePaths predicat baseDir
= do
82 contents
<- getDirectoryContents dir
83 foldM
(\acc entry
-> do
84 let absDir
= dir
</> entry
85 doesDirectoryExist absDir
>>= \exists
->
88 paths
<- processDir absDir
97 (contents \\
["..", "."])
100 readXMLFile :: FilePath
-> IO Movies
101 readXMLFile file
= undefined
103 file <- readFile "../xml/test.xml"
104 --print $ parseXMLDoc file
107 writeXMLFile :: Movies
-> FilePath
-> IO
()
108 writeXMLFile movies file
= undefined
110 filesPath :: FilePath
-> IO
[FilePath
]
111 filesPath basePath
= undefined
113 movieName :: FilePath
-> String
114 movieName
= undefined
116 -- Int is the module id.
117 data SearchResult
= OK
(Int
, Movie
)
118 | Many
[(Int
, String
)] -- String is the name of the movie.
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
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.
132 searchAMovie :: String
-> Module
-> IO SearchResult
133 searchAMovie filename mod
= undefined