From 538c88728fa5400454f158e2c72561be95d7867a Mon Sep 17 00:00:00 2001 From: depsterr Date: Tue, 7 Sep 2021 18:37:21 +0200 Subject: [PATCH] better formatting of magnet links, unspaghettied some code --- src/JSONTypes.hs | 2 +- src/Request.hs | 2 +- src/Torrent.hs | 21 ++++++++++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/JSONTypes.hs b/src/JSONTypes.hs index cce7cf1..4d784c3 100644 --- a/src/JSONTypes.hs +++ b/src/JSONTypes.hs @@ -11,7 +11,7 @@ import qualified Data.ByteString.Lazy.Internal as BL data JSONResponse d = JSONResponse { resp_status :: T.Text , resp_message :: T.Text - , response_data :: d + , response_data :: Maybe d } deriving (Show) instance (FromJSON d) => FromJSON (JSONResponse d) where diff --git a/src/Request.hs b/src/Request.hs index 7d7d0be..b33b387 100644 --- a/src/Request.hs +++ b/src/Request.hs @@ -16,7 +16,7 @@ makeRequest :: (FromJSON a) => String -> WR.Options -> IO (Either T.Text a) makeRequest url opts = do r <- asJSON =<< getWith opts url pure $ case (r ^. responseBody) of - (JSONResponse "ok" _ d) -> Right d + (JSONResponse "ok" _ (Just d)) -> Right d (JSONResponse _ m _) -> Left m getMovies :: IO (Either T.Text JSONListMovies) diff --git a/src/Torrent.hs b/src/Torrent.hs index 53d9e9e..b0ba7c8 100644 --- a/src/Torrent.hs +++ b/src/Torrent.hs @@ -5,6 +5,8 @@ import Network.HTTP.Base import qualified Data.Text as T import Data.List (intercalate) +type Quality = String + trackerList :: [String] trackerList = [ "udp://open.demonii.com:1337/announce" , "udp://tracker.openbittorrent.com:80" @@ -19,13 +21,22 @@ trackerList = [ "udp://open.demonii.com:1337/announce" trackerString :: String trackerString = "&tr=" <> intercalate "&tr=" trackerList -toMagnets :: JSONMovie -> [String] -toMagnets m = map (toMagnet $ T.unpack (movie_title_long m)) (map (T.unpack . torrent_hash) (movie_torrents m)) +toMagnets :: JSONMovie -> [(Quality, String)] +toMagnets m = map (\t -> (quality t, toMagnet name (hash t))) torrents + where + name = T.unpack (movie_title_long m) + quality = T.unpack . torrent_quality + hash = T.unpack . torrent_hash + torrents = movie_torrents m + toMagnet :: String -> String -> String -toMagnet long_name hash = "magnet:?xt=urn:btih:" <> hash <> "&dn" <> (urlEncode long_name) <> trackerString +toMagnet long_name hash = "magnet:?xt=urn:btih:" <> hash <> "&dn" + <> (urlEncode long_name) <> trackerString printMagnets :: JSONMovie -> IO () printMagnets movie = do - putStrLn . T.unpack $ movie_title movie - mapM_ (putStrLn . ("\t" ++)) (toMagnets movie) + putStrLn $ '\n' : title ++ '\n' : replicate (length title) '=' + mapM_ ( \(q, m) -> putStrLn (q ++ '\t' : m)) (toMagnets movie) + where + title = T.unpack (movie_title movie)