ADD Progress on XML parsing (not finished yet)
[pompage.git] / src / Pompage.hs
index e3066f2..2316f3f 100644 (file)
@@ -1,3 +1,5 @@
+{-# LANGUAGE TypeSynonymInstances, OverlappingInstances, NoMonomorphismRestriction, ScopedTypeVariables #-}
+
 import System.IO (readFile, FilePath(..))
 import System.Directory (
      getDirectoryContents
@@ -8,13 +10,14 @@ import System.Environment (getArgs, getProgName)
 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]
@@ -25,11 +28,15 @@ data Movie = Movie {
    , 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)]
 
@@ -44,7 +51,7 @@ main = do
          paths <- moviePaths dir
          movies <- readXMLFile xmlFile
          print movies
-         print paths
+         --print paths
 
 coversDir = "../img/covers"
 movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"]
@@ -97,7 +104,6 @@ filePaths predicat baseDir = do
             []
             (contents \\ ["..", "."])
 
-
 readXMLFile :: FilePath -> IO Movies
 readXMLFile file = do
    content <- readFile file
@@ -111,13 +117,56 @@ readXMLFile file = do
       (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