]> git.rkrishnan.org Git - functorrent.git/commitdiff
parse and print Metainfo
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Thu, 5 Feb 2015 09:21:44 +0000 (14:51 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Thu, 5 Feb 2015 09:21:44 +0000 (14:51 +0530)
src/Main.hs
src/Metainfo.hs

index f5ee3890ad96882023d4a892f2d1b8de0ccb515c..16b2ca5b9f3a0e82b5c8def0e4a3e2f59b61ea45 100644 (file)
@@ -14,7 +14,8 @@ main = do
   args <- getArgs
   torrentStr <- BC.readFile (head args)
   case (Benc.decode torrentStr) of
-   Right d -> let minfo = MInfo.mkMetaInfo d
-              in MInfo.printInfo minfo
+   Right d -> case (MInfo.mkMetaInfo d) of
+               Nothing -> putStrLn "parse error"
+               Just m -> putStrLn (show m)
    Left e -> printError e
   putStrLn "done"
index b07b3f7e758c862fb552ae8b29dcc5713326562a..6ba76685a88668004b8e3447c82e9fa687c9c1a8 100644 (file)
@@ -12,30 +12,53 @@ data Info = Info { pieceLength :: Integer
                  , name :: String
                  , lengthInBytes :: Integer
                  , md5sum :: Maybe String }
-          deriving (Eq)
+          deriving (Eq, Show)
 
 data Metainfo = Metainfo { info :: Info
                          , announce :: String
                          , announceList :: Maybe [[String]]
-                         , creationDate :: Maybe UTCTime
+                         , creationDate :: Maybe String
                          , comment :: Maybe String
                          , createdBy :: Maybe String
                          , encoding :: Maybe String }
-              deriving (Eq)
+              deriving (Eq, Show)
+
+mkInfo :: Benc.BVal -> Maybe Info
+mkInfo (Benc.Bdict m) = let (Benc.Bint pieceLength') = m M.! (Benc.Bstr (BC.pack "piece length"))
+                            (Benc.Bstr pieces') = m M.! (Benc.Bstr (BC.pack "pieces"))
+                            private' = Nothing
+                            (Benc.Bstr name') = m M.! (Benc.Bstr (BC.pack "name"))
+                            (Benc.Bint length') = m M.! (Benc.Bstr (BC.pack "length"))
+                            md5sum' = Nothing
+                        in Just Info { pieceLength = pieceLength'
+                                     , pieces = pieces'
+                                     , private = private'
+                                     , name = BC.unpack name'
+                                     , lengthInBytes = length'
+                                     , md5sum = md5sum' }
+mkInfo _ = Nothing
+
+maybeBstrToString :: Maybe Benc.BVal -> Maybe String
+maybeBstrToString Nothing = Nothing
+maybeBstrToString (Just s) = let (Benc.Bstr bs) = s
+                             in Just (BC.unpack bs)
 
 mkMetaInfo :: Benc.BVal -> Maybe Metainfo
-mkMetaInfo (Benc.Bdict m) = let info = mkInfo (m M.! (Benc.Bstr (BC.pack "info")))
-                                announce = m M.! (Benc.Bstr (BC.pack "announce"))
-                                announceList = M.lookup (Benc.Bstr (BC.pack "announce list")) m
-                                creationDate = M.lookup (Benc.Bstr (BC.pack "creation date")) m
-                                comment = M.lookup (Benc.Bstr (BC.pack "comment")) m
-                                createdBy = M.lookup (Benc.Bstr (BC.pack "created by")) m
-                                encoding = M.lookup (Benc.Bstr (BC.pack "encoding")) m
-                            in Just Metainfo { info = info
-                                             , announce = announce
-                                             , announceList = announceList
-                                             , creationDate = creationDate
-                                             , comment = comment
-                                             , createdBy = createdBy
-                                             , encoding = encoding }
+mkMetaInfo (Benc.Bdict m) = let (Just info') = mkInfo (m M.! (Benc.Bstr (BC.pack "info")))
+                                (Benc.Bstr announce') = m M.! (Benc.Bstr (BC.pack "announce"))
+--                                announceList = M.lookup (Benc.Bstr (BC.pack "announce list"))
+                                announceList' = Nothing
+                                -- creationDate = M.lookup (Benc.Bstr (BC.pack "creation date")) m
+                                creationDate' = Nothing
+                                comment' = M.lookup (Benc.Bstr (BC.pack "comment")) m
+                                createdBy' = M.lookup (Benc.Bstr (BC.pack "created by")) m
+                                encoding' = M.lookup (Benc.Bstr (BC.pack "encoding")) m
+                            in Just Metainfo { info = info'
+                                             , announce = BC.unpack announce'
+                                             , announceList = announceList'
+                                             , creationDate = creationDate'
+                                             , comment = maybeBstrToString comment'
+                                             , createdBy = maybeBstrToString createdBy'
+                                             , encoding = maybeBstrToString encoding' }
 mkMetaInfo _ = Nothing
+