successfully assemble magnet links

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

View File

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

View File

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

View File

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

31
src/Torrent.hs 100644
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 100644
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