diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-06 22:09:20 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-06 22:09:20 +0400 |
commit | c45c87c587046fcc7f2656bc1eb7302286c0ef96 (patch) | |
tree | 3bc4317db109c4c887a87de49b52b0331470b5d5 /src/Data/Torrent.hs | |
parent | 068751854cc6c111bf4bec14802fb2552c0a26bf (diff) |
Add ppTorrent function
Diffstat (limited to 'src/Data/Torrent.hs')
-rw-r--r-- | src/Data/Torrent.hs | 44 |
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 @@ | |||
26 | module Data.Torrent | 26 | module 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 | |||
85 | import Data.Time.Clock.POSIX | 87 | import Data.Time.Clock.POSIX |
86 | import Data.Typeable | 88 | import Data.Typeable |
87 | import Network.URI | 89 | import Network.URI |
90 | import Text.PrettyPrint as PP | ||
88 | import System.FilePath | 91 | import System.FilePath |
89 | 92 | ||
90 | import Data.Torrent.InfoHash as IH | 93 | import 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 | ||
158 | ppPrivacy :: Bool -> Doc | ||
159 | ppPrivacy privacy = | ||
160 | "Privacy: " <> if privacy then "private" else "public" | ||
161 | |||
162 | -- | Format info dictionary in human-readable form. | ||
163 | ppInfoDict :: InfoDict -> Doc | ||
164 | ppInfoDict 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 | ||
284 | name <:> v = name <> ":" <+> v | ||
285 | |||
286 | (<:>?) :: Doc -> Maybe Doc -> Doc | ||
287 | _ <:>? Nothing = PP.empty | ||
288 | name <:>? (Just d) = name <:> d | ||
289 | |||
290 | ppTorrent :: Torrent -> Doc | ||
291 | ppTorrent 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. |
270 | nullTorrent :: URI -> InfoDict -> Torrent | 314 | nullTorrent :: URI -> InfoDict -> Torrent |
271 | nullTorrent ann info = Torrent | 315 | nullTorrent ann info = Torrent |