summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-06 22:09:20 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-06 22:09:20 +0400
commitc45c87c587046fcc7f2656bc1eb7302286c0ef96 (patch)
tree3bc4317db109c4c887a87de49b52b0331470b5d5 /src
parent068751854cc6c111bf4bec14802fb2552c0a26bf (diff)
Add ppTorrent function
Diffstat (limited to 'src')
-rw-r--r--src/Data/Torrent.hs44
-rw-r--r--src/Data/Torrent/Piece.hs4
2 files changed, 46 insertions, 2 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs
index bb7485cf..59c4af8c 100644
--- a/src/Data/Torrent.hs
+++ b/src/Data/Torrent.hs
@@ -26,6 +26,7 @@
26module Data.Torrent 26module Data.Torrent
27 ( -- * Info dictionary 27 ( -- * Info dictionary
28 InfoDict (..) 28 InfoDict (..)
29 , ppInfoDict
29 30
30 -- ** Lenses 31 -- ** Lenses
31 , infohash 32 , infohash
@@ -35,6 +36,7 @@ module Data.Torrent
35 36
36 -- * Torrent file 37 -- * Torrent file
37 , Torrent(..) 38 , Torrent(..)
39 , ppTorrent
38 40
39 -- ** Lenses 41 -- ** Lenses
40 , announce 42 , announce
@@ -85,6 +87,7 @@ import Data.Time
85import Data.Time.Clock.POSIX 87import Data.Time.Clock.POSIX
86import Data.Typeable 88import Data.Typeable
87import Network.URI 89import Network.URI
90import Text.PrettyPrint as PP
88import System.FilePath 91import System.FilePath
89 92
90import Data.Torrent.InfoHash as IH 93import Data.Torrent.InfoHash as IH
@@ -152,6 +155,17 @@ instance BEncode InfoDict where
152 where 155 where
153 ih = IH.hashlazy (encode dict) 156 ih = IH.hashlazy (encode dict)
154 157
158ppPrivacy :: Bool -> Doc
159ppPrivacy privacy =
160 "Privacy: " <> if privacy then "private" else "public"
161
162-- | Format info dictionary in human-readable form.
163ppInfoDict :: InfoDict -> Doc
164ppInfoDict InfoDict {..} =
165 ppLayoutInfo idLayoutInfo $$
166 ppPieceInfo idPieceInfo $$
167 ppPrivacy idPrivate
168
155{----------------------------------------------------------------------- 169{-----------------------------------------------------------------------
156-- Torrent info 170-- Torrent info
157-----------------------------------------------------------------------} 171-----------------------------------------------------------------------}
@@ -266,6 +280,36 @@ instance BEncode Torrent where
266 <*>? "publisher-url" 280 <*>? "publisher-url"
267 <*>? "signature" 281 <*>? "signature"
268 282
283(<:>) :: Doc -> Doc -> Doc
284name <:> v = name <> ":" <+> v
285
286(<:>?) :: Doc -> Maybe Doc -> Doc
287_ <:>? Nothing = PP.empty
288name <:>? (Just d) = name <:> d
289
290ppTorrent :: Torrent -> Doc
291ppTorrent Torrent {..} =
292 "InfoHash: " <> ppInfoHash (idInfoHash tInfoDict)
293 $$ hang "General" 4 generalInfo
294 $$ hang "Tracker" 4 trackers
295 $$ ppInfoDict tInfoDict
296 where
297 trackers = case tAnnounceList of
298 Nothing -> text (show tAnnounce)
299 Just xxs -> vcat $ L.map ppTier $ L.zip [1..] xxs
300 where
301 ppTier (n, xs) = "Tier #" <> int n <:> vcat (L.map (text . show) xs)
302
303 generalInfo =
304 "Comment" <:>? ((text . T.unpack) <$> tComment) $$
305 "Created by" <:>? ((text . T.unpack) <$> tCreatedBy) $$
306 "Created on" <:>? ((text . show . posixSecondsToUTCTime)
307 <$> tCreationDate) $$
308 "Encoding" <:>? ((text . T.unpack) <$> tEncoding) $$
309 "Publisher" <:>? ((text . show) <$> tPublisher) $$
310 "Publisher URL" <:>? ((text . show) <$> tPublisherURL) $$
311 "Signature" <:>? ((text . show) <$> tSignature)
312
269-- | A simple torrent contains only required fields. 313-- | A simple torrent contains only required fields.
270nullTorrent :: URI -> InfoDict -> Torrent 314nullTorrent :: URI -> InfoDict -> Torrent
271nullTorrent ann info = Torrent 315nullTorrent ann info = Torrent
diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs
index 9e7280b0..c8727a65 100644
--- a/src/Data/Torrent/Piece.hs
+++ b/src/Data/Torrent/Piece.hs
@@ -194,8 +194,8 @@ instance BEncode PieceInfo where
194 194
195-- | Format piece info in human readable form. Hashes are omitted. 195-- | Format piece info in human readable form. Hashes are omitted.
196ppPieceInfo :: PieceInfo -> Doc 196ppPieceInfo :: PieceInfo -> Doc
197ppPieceInfo PieceInfo { piPieceLength = len } = 197ppPieceInfo PieceInfo {..} =
198 "PieceInfo" <+> braces ("length" <+> "=" <+> int len) 198 "Piece size: " <> int piPieceLength
199 199
200hashsize :: Int 200hashsize :: Int
201hashsize = 20 201hashsize = 20