better formatting of magnet links, unspaghettied some code

master
Rachel Lambda Samuelsson 2021-09-07 18:37:21 +02:00
parent 6446f54c20
commit 538c88728f
3 changed files with 18 additions and 7 deletions

View File

@ -11,7 +11,7 @@ import qualified Data.ByteString.Lazy.Internal as BL
data JSONResponse d = JSONResponse data JSONResponse d = JSONResponse
{ resp_status :: T.Text { resp_status :: T.Text
, resp_message :: T.Text , resp_message :: T.Text
, response_data :: d , response_data :: Maybe d
} deriving (Show) } deriving (Show)
instance (FromJSON d) => FromJSON (JSONResponse d) where instance (FromJSON d) => FromJSON (JSONResponse d) where

View File

@ -16,7 +16,7 @@ makeRequest :: (FromJSON a) => String -> WR.Options -> IO (Either T.Text a)
makeRequest url opts = do makeRequest url opts = do
r <- asJSON =<< getWith opts url r <- asJSON =<< getWith opts url
pure $ case (r ^. responseBody) of pure $ case (r ^. responseBody) of
(JSONResponse "ok" _ d) -> Right d (JSONResponse "ok" _ (Just d)) -> Right d
(JSONResponse _ m _) -> Left m (JSONResponse _ m _) -> Left m
getMovies :: IO (Either T.Text JSONListMovies) getMovies :: IO (Either T.Text JSONListMovies)

View File

@ -5,6 +5,8 @@ import Network.HTTP.Base
import qualified Data.Text as T import qualified Data.Text as T
import Data.List (intercalate) import Data.List (intercalate)
type Quality = String
trackerList :: [String] trackerList :: [String]
trackerList = [ "udp://open.demonii.com:1337/announce" trackerList = [ "udp://open.demonii.com:1337/announce"
, "udp://tracker.openbittorrent.com:80" , "udp://tracker.openbittorrent.com:80"
@ -19,13 +21,22 @@ trackerList = [ "udp://open.demonii.com:1337/announce"
trackerString :: String trackerString :: String
trackerString = "&tr=" <> intercalate "&tr=" trackerList trackerString = "&tr=" <> intercalate "&tr=" trackerList
toMagnets :: JSONMovie -> [String] toMagnets :: JSONMovie -> [(Quality, String)]
toMagnets m = map (toMagnet $ T.unpack (movie_title_long m)) (map (T.unpack . torrent_hash) (movie_torrents m)) 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 :: 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 :: JSONMovie -> IO ()
printMagnets movie = do printMagnets movie = do
putStrLn . T.unpack $ movie_title movie putStrLn $ '\n' : title ++ '\n' : replicate (length title) '='
mapM_ (putStrLn . ("\t" ++)) (toMagnets movie) mapM_ ( \(q, m) -> putStrLn (q ++ '\t' : m)) (toMagnets movie)
where
title = T.unpack (movie_title movie)