+{-# LANGUAGE TypeSynonymInstances, OverlappingInstances, NoMonomorphismRestriction, ScopedTypeVariables #-}
+
import System.IO (readFile, FilePath(..))
import System.Directory (
getDirectoryContents
import Data.List
import Text.Printf (printf)
import Text.XML.Light
+import qualified Text.XML.Light.Cursor as C
import Control.Monad (foldM)
+import Control.Exception (SomeException(..), handle, bracket)
type Movies = [Movie]
data Movie = Movie {
- movieFiles :: [FilePath]
- , movieId :: Int
+ movieId :: Int
, movieTitle :: String
, movieYear :: Maybe Int
, movieDirectors :: [String]
, moviePressRating :: Maybe Int
, movieGenres :: [String]
, movieSynopsis :: String
- , movieBudget :: Int
+ , movieBudget :: Maybe Int
, movieBudgetUnit :: String
+ , movieFiles :: [FilePath]
, movieUrl :: String
} deriving (Show)
+emptyMovie = Movie 0 "no title" Nothing [] [] []
+ Nothing Nothing Nothing [] "" Nothing "" [] ""
+
data Arg = XML | MovieDir deriving (Show, Eq)
type Args = [(Arg, String)]
paths <- moviePaths dir
movies <- readXMLFile xmlFile
print movies
- print paths
+ --print paths
coversDir = "../img/covers"
movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"]
[]
(contents \\ ["..", "."])
-
readXMLFile :: FilePath -> IO Movies
readXMLFile file = do
content <- readFile file
(elChildren root)
elementXMLToMovie :: Element -> Maybe Movie
-elementXMLToMovie elem = undefined
-{-
-findAttr (QName "id" Nothing Nothing) elem of
- Nothing -> acc
- Just id ->
--}
+elementXMLToMovie elem =
+ Just (emptyMovie, C.fromElement elem) >>?
+ (\(m, c) ->
+ case C.current c of
+ Elem elem ->
+ case findAttr (simpleQName "id") elem of
+ Nothing -> Nothing
+ Just id -> Just (m { movieId = read id :: Int }, c)
+ otherwise -> Nothing) >>?
+ (\(m, c) ->
+ case firstChildElement c of
+ Just (elem, c') -> Just (m { movieTitle = strContent elem }, c')
+ otherwise -> Nothing) >>?
+ (\(m, c) ->
+ case nextSibilingElement c of
+ Just (elem, c') -> Just m { movieYear = intElement elem }
+ otherwise -> Nothing)
+
+-- A bit naive
+(>>?) :: Maybe alpha -> (alpha -> Maybe beta) -> Maybe beta
+Nothing >>? _ = Nothing
+Just v >>? f = f v
+
+-- Some XML helper functions
+simpleQName name = QName name Nothing Nothing
+firstChildElement :: C.Cursor -> Maybe (Element, C.Cursor)
+firstChildElement c =
+ case C.firstChild c of
+ Just c' ->
+ case C.current c' of
+ Elem elem -> Just (elem, c')
+ otherwise -> nextSibilingElement c'
+ otherwise -> Nothing
+nextSibilingElement :: C.Cursor -> Maybe (Element, C.Cursor)
+nextSibilingElement c =
+ case C.right c of
+ Just c' ->
+ case C.current c' of
+ Elem elem -> Just (elem, c')
+ otherwise -> nextSibilingElement c'
+ Nothing -> Nothing
+
+-- Try to cast an element content to an Int.
+intElement :: Element -> Maybe Int
+intElement elem =
+ if content == []
+ then Nothing
+ else Just (read content :: Int)
+ where content = strContent elem
writeXMLFile :: Movies -> FilePath -> IO ()
writeXMLFile movies file = undefined
--- /dev/null
+<?xml version='1.0' encoding='UTF-8'?>
+<?xml-stylesheet type="text/xsl" href="../xsl/yopyop.xsl"?>
+<filmography>
+ <movie id='1'>
+ <title>Rocky Balboa</title>
+ <year>2006</year>
+ <directors>
+ <director>Sylvester Stallone</director>
+ </directors>
+ <actors>
+ <actor>Sylvester Stallone</actor>
+ <actor>Burt Young</actor>
+ <actor>Antonio Tarver</actor>
+ </actors>
+ <countries>
+ <country>américain</country>
+ </countries>
+ <length>105</length>
+ <pressRating>4</pressRating>
+ <userRating>3</userRating>
+ <genres>
+ <genre>Drame</genre>
+ <genre>Action</genre>
+ </genres>
+ <synopsis>
+ <p>Rocky Balboa, le légendaire boxeur, a depuis longtemps quitté le ring. De ses succès, il ne reste plus que des histoires qu'il raconte aux clients de son restaurant. La mort de son épouse lui pèse chaque jour et son fils ne vient jamais le voir.</p>
+ <p>Le champion d'aujourd'hui s'appelle Mason Dixon, et tout le monde s'accorde à le définir comme un tueur sans élégance ni coeur. Alors que les promoteurs lui cherchent désespérément un adversaire à sa taille, la légende de Rocky refait surface. L'idée d'opposer deux écoles, deux époques et deux titans aussi différents enflamme tout le monde. Pour Balboa, c'est l'occasion de ranimer les braises d'une passion qui ne l'a jamais quitté. L'esprit d'un champion ne meurt jamais...</p>
+ </synopsis>
+ <budget unit='euro'/>
+ <files>
+ <file>Rocky [x264 Aac] [Fr Eng] [Fr Eng].mkv</file>
+ </files>
+ <url>http://www.allocine.fr/film/fichefilm_gen_cfilm=109061.html</url>
+ </movie>
+</filmography>
\ No newline at end of file