summaryrefslogtreecommitdiff
path: root/src/Data/Torrent.hs
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/Data/Torrent.hs
parent068751854cc6c111bf4bec14802fb2552c0a26bf (diff)
Add ppTorrent function
Diffstat (limited to 'src/Data/Torrent.hs')
-rw-r--r--src/Data/Torrent.hs44
1 files changed, 44 insertions, 0 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