summaryrefslogtreecommitdiff
path: root/src/Network/Torrent
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-08 06:55:58 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-08 06:55:58 +0400
commit2934fbff3159288cbb51a11f42f1a135223f93d0 (patch)
treeb4f38e5295756591ffdf4b26d158db66ce153452 /src/Network/Torrent
parentd2bfd1064b08678c161a5d2d0f9dbcdcd0ab2142 (diff)
add more documentation
Diffstat (limited to 'src/Network/Torrent')
-rw-r--r--src/Network/Torrent/Tracker/Scrape.hs47
1 files changed, 43 insertions, 4 deletions
diff --git a/src/Network/Torrent/Tracker/Scrape.hs b/src/Network/Torrent/Tracker/Scrape.hs
index 6bfb488f..ae800230 100644
--- a/src/Network/Torrent/Tracker/Scrape.hs
+++ b/src/Network/Torrent/Tracker/Scrape.hs
@@ -1,7 +1,16 @@
1{-# LANGUAGE OverloadedStrings #-} 1{-# LANGUAGE OverloadedStrings #-}
2-- | By convention most trackers support anouther form of request,
3-- which queries the state of a given torrent (or all torrents) that the
4-- tracker is managing. This module provides a way to easily request
5-- scrape info for a particular torrent list.
6--
2module Network.Torrent.Tracker.Scrape 7module Network.Torrent.Tracker.Scrape
3 ( ScrapeInfo(..), Scrape 8 ( ScrapeInfo(..), Scrape
4 , scrapeURL 9 , scrapeURL
10
11 -- * Requests
12 , scrape
13 , scrapeOne
5 ) where 14 ) where
6 15
7import Control.Applicative 16import Control.Applicative
@@ -14,15 +23,22 @@ import qualified Data.Map as M
14import Data.Monoid 23import Data.Monoid
15import Data.Torrent.InfoHash 24import Data.Torrent.InfoHash
16import Network.URI 25import Network.URI
26import Network.HTTP
17 27
28-- | Information about particular torrent.
18data ScrapeInfo = ScrapeInfo { 29data ScrapeInfo = ScrapeInfo {
19 siComplete :: Int -- ^ Number of seeders. 30 siComplete :: Int
31 -- ^ Number of seeders - peers with the entire file.
20 , siDownloaded :: Int 32 , siDownloaded :: Int
21 -- ^ Total number of times the tracker has registered a completion. 33 -- ^ Total number of times the tracker has registered a completion.
22 , siIncomplete :: Int -- ^ Number of leechers 34 , siIncomplete :: Int
23 , siName :: Maybe ByteString -- ^ 35 -- ^ Number of leechers.
36 , siName :: Maybe ByteString
37 -- | Name of the torrent file, as specified by the "name"
38 -- file in the info section of the .torrent file.
24 } deriving (Show, Eq) 39 } deriving (Show, Eq)
25 40
41-- | Scrape info about a set of torrents.
26type Scrape = Map InfoHash ScrapeInfo 42type Scrape = Map InfoHash ScrapeInfo
27 43
28instance BEncodable ScrapeInfo where 44instance BEncodable ScrapeInfo where
@@ -40,7 +56,6 @@ instance BEncodable ScrapeInfo where
40 <*> d >--? "name" 56 <*> d >--? "name"
41 fromBEncode _ = decodingError "ScrapeInfo" 57 fromBEncode _ = decodingError "ScrapeInfo"
42 58
43-- TODO: encode info hash
44-- | Trying to convert /announce/ URL to /scrape/ URL. If 'scrapeURL' 59-- | Trying to convert /announce/ URL to /scrape/ URL. If 'scrapeURL'
45-- gives 'Nothing' then tracker do not support scraping. The info hash 60-- gives 'Nothing' then tracker do not support scraping. The info hash
46-- list is used to restrict the tracker's report to that particular 61-- list is used to restrict the tracker's report to that particular
@@ -60,3 +75,27 @@ scrapeURL uri ihs = do
60 = let newSuff = "scrape" <> B.drop (B.length "announce") (last ps) 75 = let newSuff = "scrape" <> B.drop (B.length "announce") (last ps)
61 in Just (B.intercalate "/" (init ps ++ [newSuff])) 76 in Just (B.intercalate "/" (init ps ++ [newSuff]))
62 | otherwise = Nothing 77 | otherwise = Nothing
78
79
80-- | For each 'InfoHash' of torrents request scrape info from the tracker.
81-- However if the info hash list is 'null', the tracker should list
82-- all available torrents.
83-- Note that the 'URI' should be /announce/ URI, not /scrape/ URI.
84--
85scrape :: URI -- ^ Announce 'URI'.
86 -> [InfoHash] -- ^ Torrents to be scrapped.
87 -> IO (Result Scrape) -- ^ 'ScrapeInfo' for each torrent.
88scrape announce ihs = undefined
89
90
91-- | More particular version of 'scrape', just for one torrent.
92--
93scrapeOne :: URI -- ^ Announce 'URI'
94 -> InfoHash -- ^ Hash of the torrent info.
95 -> IO (Result ScrapeInfo) -- ^ 'ScrapeInfo' for the torrent.
96scrapeOne uri ih = extract <$> scrape uri [ih]
97 where
98 extract (Right m)
99 | Just s <- M.lookup ih m = Right s
100 | otherwise = Left "unable to find info hash in response dict"
101 extract (Left e) = Left e