diff options
-rw-r--r-- | app/Main.hs | 28 | ||||
-rw-r--r-- | servall.cabal | 2 | ||||
-rw-r--r-- | src/Servall/Types.hs | 37 |
3 files changed, 42 insertions, 25 deletions
diff --git a/app/Main.hs b/app/Main.hs index 82acb3c..005b63a 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -12,11 +12,13 @@ import Control.Lens ( (^.) ) import Control.Monad.IO.Class ( liftIO ) import Data.Aeson ( (.:) + , (.=) , FromJSON(..) - , ToJSON + , ToJSON(..) , Value(..) , decode , encode + , object ) import qualified Data.Aeson.KeyMap as KM ( KeyMap @@ -180,33 +182,11 @@ getInfobox name = do searchYt :: Text -> Handler [Video] searchYt query = do (_, Just hout, _, _) <- liftIO $ createProcess - (proc "yt-dlp" ["-j", "ytsearch3:" <> T.unpack query]) + (proc "yt-dlp" ["-j", "ytsearch10:" <> T.unpack query]) { std_out = CreatePipe } liftIO $ (catMaybes . map decode . BSL.lines) <$> BSL.hGetContents hout -data Video = Video - { vid :: Text - , vtitle :: Text - , vdesc :: Text - , vduration :: Int - } - deriving (Eq, Show, Generic) - -instance FromJSON Video where - parseJSON (Object o) = - Video - <$> o - .: "id" - <*> o - .: "title" - <*> o - .: "description" - <*> o - .: "duration" - -instance ToJSON Video - app :: Application app = serve api server diff --git a/servall.cabal b/servall.cabal index 05e1b2a..3792993 100644 --- a/servall.cabal +++ b/servall.cabal @@ -26,7 +26,7 @@ library default-language: Haskell2010 exposed-modules: Servall.Types, Servall.WikiParser hs-source-dirs: src - build-depends: base, attoparsec, text, unordered-containers + build-depends: aeson, base, attoparsec, text, unordered-containers executable servall main-is: Main.hs diff --git a/src/Servall/Types.hs b/src/Servall/Types.hs index e179e2c..69c2045 100644 --- a/src/Servall/Types.hs +++ b/src/Servall/Types.hs @@ -1,7 +1,16 @@ +{-# LANGUAGE OverloadedStrings #-} module Servall.Types ( WikiTemplate(..) + , Video(..) ) where +import Data.Aeson ( (.:) + , (.=) + , FromJSON(..) + , ToJSON(..) + , Value(..) + , object + ) import qualified Data.HashMap.Lazy as HM import Data.Text ( Text ) @@ -11,3 +20,31 @@ data WikiTemplate = WikiTemplate , wtFields :: HM.HashMap Text Text } deriving Show + +data Video = Video + { vid :: Text + , vtitle :: Text + , vdesc :: Text + , vduration :: Int + } + deriving (Eq, Show) + +instance FromJSON Video where + parseJSON (Object o) = + Video + <$> o + .: "id" + <*> o + .: "title" + <*> o + .: "description" + <*> o + .: "duration" + +instance ToJSON Video where + toJSON (Video vid title desc duration) = object + [ "id" .= vid + , "title" .= title + , "description" .= desc + , "duration" .= duration + ] |