summaryrefslogtreecommitdiff
path: root/src/Data/Torrent/Magnet.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-06 18:11:03 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-06 18:11:03 +0400
commit5ffd1432dc947175787a6b616a673c9c3d49ffb8 (patch)
tree1547ae15db2feda6e6e6dfe81a3889e5f96ad68c /src/Data/Torrent/Magnet.hs
parentc2a8c8338a63001e03e65dec1a5b81fe33bdba37 (diff)
Add smart constructors for magnets
Diffstat (limited to 'src/Data/Torrent/Magnet.hs')
-rw-r--r--src/Data/Torrent/Magnet.hs29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/Data/Torrent/Magnet.hs b/src/Data/Torrent/Magnet.hs
index 34a7bbc5..01f28a76 100644
--- a/src/Data/Torrent/Magnet.hs
+++ b/src/Data/Torrent/Magnet.hs
@@ -13,10 +13,17 @@
13-- Bittorrent specific info: 13-- Bittorrent specific info:
14-- <http://www.bittorrent.org/beps/bep_0009.html> 14-- <http://www.bittorrent.org/beps/bep_0009.html>
15-- 15--
16{-# LANGUAGE NamedFieldPuns #-}
16module Data.Torrent.Magnet 17module Data.Torrent.Magnet
17 ( -- * Magnet 18 ( -- * Magnet
18 Magnet(..) 19 Magnet(..)
20
21 -- * Construction
19 , nullMagnet 22 , nullMagnet
23 , simpleMagnet
24 , detailedMagnet
25
26 -- * Conversion
20 , parseMagnet 27 , parseMagnet
21 , renderMagnet 28 , renderMagnet
22 29
@@ -33,11 +40,13 @@ import Data.List as L
33import Data.URLEncoded as URL 40import Data.URLEncoded as URL
34import Data.String 41import Data.String
35import Data.Text as T 42import Data.Text as T
43import Data.Text.Encoding as T
36import Network.URI 44import Network.URI
37import Text.Read 45import Text.Read
38 46
47import Data.Torrent
39import Data.Torrent.InfoHash 48import Data.Torrent.InfoHash
40 49import Data.Torrent.Layout
41 50
42{----------------------------------------------------------------------- 51{-----------------------------------------------------------------------
43-- URN 52-- URN
@@ -153,6 +162,24 @@ nullMagnet u = Magnet
153 , supplement = M.empty 162 , supplement = M.empty
154 } 163 }
155 164
165-- | A simple magnet link including infohash ('xt' param) and display
166-- name ('dn' param).
167--
168simpleMagnet :: Torrent -> Magnet
169simpleMagnet Torrent {tInfoDict = InfoDict {..}}
170 = (nullMagnet idInfoHash)
171 { displayName = Just $ T.decodeUtf8 $ suggestedName idLayoutInfo
172 }
173
174-- | Like 'simpleMagnet' but also include exactLength ('xl' param) and
175-- tracker ('tr' param).
176detailedMagnet :: Torrent -> Magnet
177detailedMagnet t @ Torrent {tInfoDict = InfoDict {..}, tAnnounce}
178 = (simpleMagnet t)
179 { exactLength = Just $ fromIntegral $ contentLength idLayoutInfo
180 , tracker = Just tAnnounce
181 }
182
156fromQuery :: URLEncoded -> Either String Magnet 183fromQuery :: URLEncoded -> Either String Magnet
157fromQuery q 184fromQuery q
158 | Just urnStr <- URL.lookup ("xt" :: String) q 185 | Just urnStr <- URL.lookup ("xt" :: String) q