import qualified Bencode as Benc
import qualified Metainfo as MInfo
import qualified Tracker as T
+import qualified Text.ParserCombinators.Parsec as Parsec
+import Data.Functor
-import Text.ParserCombinators.Parsec
-
-printError :: ParseError -> IO ()
-printError e = putStrLn "parse error"
+printError :: Parsec.ParseError -> IO ()
+printError e = putStrLn $ "parse error: " ++ show e
genPeerId :: String
genPeerId = "-HS0001-20150215"
Right d -> case (MInfo.mkMetaInfo d) of
Nothing -> putStrLn "parse error"
Just m -> do
- let (Benc.Bdict d') = d
--- putStrLn (show m)
--- putStrLn (T.urlEncode (T.infoHash d'))
- do body <- T.connect (MInfo.announce m) (T.prepareRequest d genPeerId)
- putStrLn body
+ body <- (Benc.decode . BC.pack) <$> T.connect (MInfo.announce m) (T.prepareRequest d genPeerId)
+ putStrLn (show body)
Left e -> printError e
putStrLn "done"
import qualified Data.List as List
import qualified Network.HTTP as HTTP
import qualified Bencode as Benc
-import qualified Crypto.Hash as H
import qualified Crypto.Hash.SHA1 as SHA1
import qualified Data.ByteString.Base16 as B16
+import qualified Utils as U
import Data.Char
-- import Network.HTTP
type Url = String
-splitN :: Int -> BC.ByteString -> [BC.ByteString]
-splitN n bs | BC.null bs = []
- | otherwise = (BC.take n bs) : splitN n (BC.drop n bs)
-- | urlEncode
--
-- >>> urlEncode $ BC.pack "123456789abcdef123456789abcdef123456789a"
-- "%124Vx%9a%bc%de%f1%23Eg%89%ab%cd%ef%124Vx%9a"
urlEncode :: BC.ByteString -> String
-urlEncode bs = concatMap (encode . BC.unpack) (splitN 2 bs)
+urlEncode bs = concatMap (encode . BC.unpack) (U.splitN 2 bs)
where encode b@(c1 : c2 : []) = let c = chr (read ("0x" ++ b))
in
escape c c1 c2
--- /dev/null
+module Utils where
+
+import qualified Data.ByteString.Char8 as BC
+
+splitN :: Int -> BC.ByteString -> [BC.ByteString]
+splitN n bs | BC.null bs = []
+ | otherwise = (BC.take n bs) : splitN n (BC.drop n bs)