diff --git a/app/Main.hs b/app/Main.hs index 16698ea..2202cc0 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -13,6 +13,7 @@ import Data.Text (pack, splitOn, unpack) import Data.Time import GHC.Generics import Text.Read (readMaybe) +import Control.Applicative (asum) data ContentType = Movie @@ -73,8 +74,8 @@ newtype Language = Language String deriving (Show, Eq, Generic, FromJSON, ToJSON data Trailer = Trailer String | Clip String deriving (Show, Eq, Generic, ToJSON) -instance FromJSON Trailer where - parseJSON (Object v) = do +instance FromJSON Trailer where + parseJSON (Object v) = do t <- v .: "type" :: Parser String src <- v .: "source" case t of @@ -107,7 +108,7 @@ data StreamDetails = StreamDetails data StreamSource = UrlSource Url | YoutubeSource String - | TorrentSource + | TorrentSource String Int | ExternalSource MetaLink deriving (Show, Generic, FromJSON, ToJSON) @@ -115,7 +116,31 @@ data Stream = Stream { details :: StreamDetails, source :: StreamSource } - deriving (Show, Generic, FromJSON, ToJSON) + deriving (Show, Generic, ToJSON) + +instance FromJSON Stream where + parseJSON (Object v) = + Stream + <$> streamDetails + <*> streamSource + where + behaviorHints = v .: "behaviorHints" + behaviorHints :: Parser Object + streamDetails = StreamDetails + <$> v .: "name" + <*> v .: "title" + <*> v .: "description" + <*> v .: "subtitles" + <*> (behaviorHints >>= (.: "bingeGroup")) + <*> (behaviorHints >>= (.: "videoHash")) + <*> (behaviorHints >>= (.: "videoSize")) + <*> (behaviorHints >>= (.: "filename")) + streamSource = asum [ + UrlSource <$> v .: "url", + YoutubeSource <$> v.: "ytId", + ExternalSource <$> v .: "externalUrl", + TorrentSource <$> v .: "infoHash" <*> v .: "fileIdx"] + parseJSON _ = fail "expected an object" data Video = Video { id :: ContentID,