kino/src/Kino/Torrent.hs

58 lines
2.0 KiB
Haskell

{-|
Module : Kino.Torrent
Description : Contains code for formatting torrent info and retrieving magnet links
Contains code for formatting torrent info and retrieving magnet links
-}
module Kino.Torrent (listTorrents, toMagnets) where
import Data.List (intercalate)
import Network.HTTP.Base
import qualified Data.Text as T
import Kino.Types
-- | A list of recommended trackers
trackerList :: [String]
trackerList = [ "udp://open.demonii.com:1337/announce"
, "udp://tracker.openbittorrent.com:80"
, "udp://tracker.coppersurfer.tk:6969"
, "udp://glotorrents.pw:6969/announce"
, "udp://tracker.opentrackr.org:1337/announce"
, "udp://torrent.gresille.org:80/announce"
, "udp://p4p.arenabg.com:1337"
, "udp://tracker.leechers-paradise.org:6969"
]
-- | A string to be embedded into the magnet link
trackerString :: String
trackerString = "&tr=" <> intercalate "&tr=" trackerList
-- | Creates a String enumerating and listing the different torrents
-- quality and amount of peers
listTorrents :: JSONMovie -> String
listTorrents m = intercalate ", " (zipWith3 (\x y z -> x ++ y ++ z) numbers qualities seeders)
where
qualities = map quality (movieTorrents m)
seeders = map (\x -> ' ':'(':seeds x ++ ")") (movieTorrents m)
numbers = map (\x -> '[':show x ++ "] ") ([1..] :: [Int])
seeds = show . torrentSeeds
quality = T.unpack . torrentQuality
-- | Creates a list of magnet names given a movie
toMagnets :: JSONMovie -> [String]
toMagnets m = map (toMagnet name . hash) torrents
where
name = T.unpack (movieTitleLong m)
hash = T.unpack . torrentHash
torrents = movieTorrents m
-- | Takes the longName of a movie and a torrent hash and returns
-- a valid magnet link
toMagnet :: String -> String -> String
toMagnet longName hash = "magnet:?xt=urn:btih:" <> hash <> "&dn"
<> urlEncode longName <> trackerString