diff --git a/app/Main.hs b/app/Main.hs index c3f6bb6..83e0688 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,6 +1,20 @@ module Main where import Request +import qualified JSONTypes as J +import Torrent +import qualified Data.Text as T + +import System.Environment main :: IO () -main = print =<< testFunc +main = do + args <- getArgs + case args of + ([]) -> print =<< getMovies + (x:xs) -> do + movies <- queryMovies (T.pack x) + + case movies of + (Left m) -> print m + (Right d) -> mapM_ printMagnets (J.movies d) diff --git a/kino.cabal b/kino.cabal index e74d374..515c99d 100644 --- a/kino.cabal +++ b/kino.cabal @@ -16,6 +16,8 @@ extra-source-files: CHANGELOG.md library exposed-modules: Request , JSONTypes + , UI + , Torrent other-modules: -- other-extensions: build-depends: base ^>=4.14.1.0 @@ -25,6 +27,8 @@ library , lens , bytestring , text + , brick + , HTTP hs-source-dirs: src default-language: Haskell2010 @@ -32,9 +36,10 @@ executable kino main-is: Main.hs -- other-modules: -- other-extensions: - build-depends: - base ^>=4.14.1.0, - kino + build-depends: base ^>=4.14.1.0 + , kino + , brick + , text hs-source-dirs: app default-language: Haskell2010 diff --git a/src/JSONTypes.hs b/src/JSONTypes.hs index daf2f08..cce7cf1 100644 --- a/src/JSONTypes.hs +++ b/src/JSONTypes.hs @@ -4,7 +4,7 @@ module JSONTypes where import Data.Aeson import Data.Aeson.Types -import Data.Text as T +import qualified Data.Text as T import Data.Aeson.Lens (key, nth) import qualified Data.ByteString.Lazy.Internal as BL @@ -45,6 +45,7 @@ data JSONMovie = JSONMovie , movie_url :: T.Text , imdb_code :: T.Text , movie_title :: T.Text + , movie_title_long :: T.Text , movie_year :: Int , movie_rating :: Double , movie_runtime :: Int @@ -61,6 +62,7 @@ instance FromJSON JSONMovie where <*> v .: "url" <*> v .: "imdb_code" <*> v .: "title" + <*> v .: "title_long" <*> v .: "year" <*> v .: "rating" <*> v .: "runtime" diff --git a/src/Request.hs b/src/Request.hs index 57341ae..7d7d0be 100644 --- a/src/Request.hs +++ b/src/Request.hs @@ -5,20 +5,22 @@ module Request where import JSONTypes import Network.Wreq +import qualified Network.Wreq as WR (Options) import Control.Lens import Data.Aeson import Data.Text as T import Data.Aeson.Lens (key, nth) -import qualified Data.ByteString.Lazy.Internal as BL +import qualified Data.ByteString.Lazy as BL -testFunc :: IO (JSONResponse JSONListMovies) -testFunc = do - r <- asJSON =<< get "https://yts.mx/api/v2/list_movies.json" - pure $ r ^. responseBody - -getMovies :: IO (Either T.Text JSONListMovies) -getMovies = do - r <- asJSON =<< get "https://yts.mx/api/v2/list_movies.json" +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 _ m _) -> Left m + +getMovies :: IO (Either T.Text JSONListMovies) +getMovies = makeRequest "https://yts.mx/api/v2/list_movies.json" defaults + +queryMovies :: T.Text -> IO (Either T.Text JSONListMovies) +queryMovies q = makeRequest "https://yts.mx/api/v2/list_movies.json" (defaults & param "query_term" .~ [q]) diff --git a/src/Torrent.hs b/src/Torrent.hs new file mode 100644 index 0000000..53d9e9e --- /dev/null +++ b/src/Torrent.hs @@ -0,0 +1,31 @@ +module Torrent where + +import JSONTypes +import Network.HTTP.Base +import qualified Data.Text as T +import Data.List (intercalate) + +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" + ] + +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)) + +toMagnet :: String -> String -> String +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) diff --git a/src/UI.hs b/src/UI.hs new file mode 100644 index 0000000..169f5c6 --- /dev/null +++ b/src/UI.hs @@ -0,0 +1,24 @@ +module UI where + +import Brick +import Brick.Main + +data Identifiers = Nil + +data AppState = Null + +app :: App AppState () Identifiers +app = App + { appDraw = draw + , appChooseCursor = chooseCursor + , appHandleEvent = eventHandler + } + + +draw = undefined + +chooseCursor = undefined + +eventHandler = undefined + +attributeMap = undefined