diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-06 18:11:03 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-06 18:11:03 +0400 |
commit | 5ffd1432dc947175787a6b616a673c9c3d49ffb8 (patch) | |
tree | 1547ae15db2feda6e6e6dfe81a3889e5f96ad68c /src/Data/Torrent/Magnet.hs | |
parent | c2a8c8338a63001e03e65dec1a5b81fe33bdba37 (diff) |
Add smart constructors for magnets
Diffstat (limited to 'src/Data/Torrent/Magnet.hs')
-rw-r--r-- | src/Data/Torrent/Magnet.hs | 29 |
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 #-} | ||
16 | module Data.Torrent.Magnet | 17 | module 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 | |||
33 | import Data.URLEncoded as URL | 40 | import Data.URLEncoded as URL |
34 | import Data.String | 41 | import Data.String |
35 | import Data.Text as T | 42 | import Data.Text as T |
43 | import Data.Text.Encoding as T | ||
36 | import Network.URI | 44 | import Network.URI |
37 | import Text.Read | 45 | import Text.Read |
38 | 46 | ||
47 | import Data.Torrent | ||
39 | import Data.Torrent.InfoHash | 48 | import Data.Torrent.InfoHash |
40 | 49 | import 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 | -- | ||
168 | simpleMagnet :: Torrent -> Magnet | ||
169 | simpleMagnet 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). | ||
176 | detailedMagnet :: Torrent -> Magnet | ||
177 | detailedMagnet t @ Torrent {tInfoDict = InfoDict {..}, tAnnounce} | ||
178 | = (simpleMagnet t) | ||
179 | { exactLength = Just $ fromIntegral $ contentLength idLayoutInfo | ||
180 | , tracker = Just tAnnounce | ||
181 | } | ||
182 | |||
156 | fromQuery :: URLEncoded -> Either String Magnet | 183 | fromQuery :: URLEncoded -> Either String Magnet |
157 | fromQuery q | 184 | fromQuery q |
158 | | Just urnStr <- URL.lookup ("xt" :: String) q | 185 | | Just urnStr <- URL.lookup ("xt" :: String) q |