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
)
16 movieFiles ::
[FilePath
]
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
29 , movieBudgetUnit :: 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 coversDir
= "../img/covers"
50 movieExtenstions
= ["avi", "mkv", "rmvb", "ogm", "divx"]
51 usage
= "Usage : %s -d <Movies dir> -x <XML file>\n"
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
60 readArgs ::
[String
] -> Maybe Args
61 readArgs
(name:value:rest
)
62 | name
== "-x" = (XML
, value
) <:
> readArgs rest
63 | name
== "-d" = (MovieDir
, value
) <:
> readArgs rest
66 _
<:
> Nothing
= Nothing
67 arg
<:
> Just args
= Just
$ arg : args
68 readArgs
(_:
[]) = Nothing
69 readArgs
(_
) = Just
[]
71 moviePaths :: FilePath
-> IO
[FilePath
]
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
78 filePaths ::
(String
-> Bool
) -> FilePath
-> IO
[FilePath
]
79 filePaths predicat baseDir
= do
83 contents
<- getDirectoryContents dir
84 foldM
(\acc entry
-> do
85 let absDir
= dir
</> entry
86 doesDirectoryExist absDir
>>= \exists
->
89 paths
<- processDir absDir
98 (contents \\
["..", "."])
101 readXMLFile :: FilePath
-> IO Movies
102 readXMLFile file
= do
103 content
<- readFile file
104 let Just root
= parseXMLDoc content
107 case elementXMLToMovie elem
of
109 Just movie
-> movie : acc
)
113 elementXMLToMovie :: Element
-> Maybe Movie
114 elementXMLToMovie elem
= undefined
116 findAttr (QName "id" Nothing Nothing) elem of
122 writeXMLFile :: Movies
-> FilePath
-> IO
()
123 writeXMLFile movies file
= undefined
125 filesPath :: FilePath
-> IO
[FilePath
]
126 filesPath basePath
= undefined
128 movieName :: FilePath
-> String
129 movieName
= undefined
131 -- Int is the module id.
132 data SearchResult
= OK
(Int
, Movie
)
133 | Many
[(Int
, String
)] -- String is the name of the movie.
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
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.
147 searchAMovie :: String
-> Module
-> IO SearchResult
148 searchAMovie filename mod
= undefined