successfully assemble magnet links

This commit is contained in:
Rachel Lambda Samuelsson 2021-09-06 21:02:39 +02:00
parent 6f1e9df2af
commit 6446f54c20
6 changed files with 92 additions and 14 deletions

View File

@ -1,6 +1,20 @@
module Main where module Main where
import Request import Request
import qualified JSONTypes as J
import Torrent
import qualified Data.Text as T
import System.Environment
main :: IO () 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)

View File

@ -16,6 +16,8 @@ extra-source-files: CHANGELOG.md
library library
exposed-modules: Request exposed-modules: Request
, JSONTypes , JSONTypes
, UI
, Torrent
other-modules: other-modules:
-- other-extensions: -- other-extensions:
build-depends: base ^>=4.14.1.0 build-depends: base ^>=4.14.1.0
@ -25,6 +27,8 @@ library
, lens , lens
, bytestring , bytestring
, text , text
, brick
, HTTP
hs-source-dirs: src hs-source-dirs: src
default-language: Haskell2010 default-language: Haskell2010
@ -32,9 +36,10 @@ executable kino
main-is: Main.hs main-is: Main.hs
-- other-modules: -- other-modules:
-- other-extensions: -- other-extensions:
build-depends: build-depends: base ^>=4.14.1.0
base ^>=4.14.1.0, , kino
kino , brick
, text
hs-source-dirs: app hs-source-dirs: app
default-language: Haskell2010 default-language: Haskell2010

View File

@ -4,7 +4,7 @@ module JSONTypes where
import Data.Aeson import Data.Aeson
import Data.Aeson.Types import Data.Aeson.Types
import Data.Text as T import qualified Data.Text as T
import Data.Aeson.Lens (key, nth) import Data.Aeson.Lens (key, nth)
import qualified Data.ByteString.Lazy.Internal as BL import qualified Data.ByteString.Lazy.Internal as BL
@ -45,6 +45,7 @@ data JSONMovie = JSONMovie
, movie_url :: T.Text , movie_url :: T.Text
, imdb_code :: T.Text , imdb_code :: T.Text
, movie_title :: T.Text , movie_title :: T.Text
, movie_title_long :: T.Text
, movie_year :: Int , movie_year :: Int
, movie_rating :: Double , movie_rating :: Double
, movie_runtime :: Int , movie_runtime :: Int
@ -61,6 +62,7 @@ instance FromJSON JSONMovie where
<*> v .: "url" <*> v .: "url"
<*> v .: "imdb_code" <*> v .: "imdb_code"
<*> v .: "title" <*> v .: "title"
<*> v .: "title_long"
<*> v .: "year" <*> v .: "year"
<*> v .: "rating" <*> v .: "rating"
<*> v .: "runtime" <*> v .: "runtime"

View File

@ -5,20 +5,22 @@ module Request where
import JSONTypes import JSONTypes
import Network.Wreq import Network.Wreq
import qualified Network.Wreq as WR (Options)
import Control.Lens import Control.Lens
import Data.Aeson import Data.Aeson
import Data.Text as T import Data.Text as T
import Data.Aeson.Lens (key, nth) 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) makeRequest :: (FromJSON a) => String -> WR.Options -> IO (Either T.Text a)
testFunc = do makeRequest url opts = do
r <- asJSON =<< get "https://yts.mx/api/v2/list_movies.json" r <- asJSON =<< getWith opts url
pure $ r ^. responseBody
getMovies :: IO (Either T.Text JSONListMovies)
getMovies = do
r <- asJSON =<< get "https://yts.mx/api/v2/list_movies.json"
pure $ case (r ^. responseBody) of pure $ case (r ^. responseBody) of
(JSONResponse "ok" _ d) -> Right d (JSONResponse "ok" _ d) -> Right d
(JSONResponse _ m _) -> Left m (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])

31
src/Torrent.hs Normal file
View File

@ -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)

24
src/UI.hs Normal file
View File

@ -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