2013-08-22 22:25:21 +00:00
|
|
|
{- querying quvi (import qualified)
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
2013-08-22 22:25:21 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2013-08-22 22:25:21 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-05-10 19:37:55 +00:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2013-08-22 22:25:21 +00:00
|
|
|
|
|
|
|
module Utility.Quvi where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Utility.Url
|
|
|
|
|
|
|
|
import Data.Aeson
|
2015-11-09 16:19:10 +00:00
|
|
|
import qualified Data.ByteString as B
|
|
|
|
import qualified Data.ByteString.Lazy as BL
|
2013-11-25 03:44:30 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
import Network.URI (uriAuthority, uriRegName)
|
|
|
|
import Data.Char
|
2013-08-22 22:25:21 +00:00
|
|
|
|
2014-02-28 18:54:02 +00:00
|
|
|
data QuviVersion
|
|
|
|
= Quvi04
|
|
|
|
| Quvi09
|
|
|
|
| NoQuvi
|
2015-02-10 15:59:55 +00:00
|
|
|
deriving (Show)
|
2014-02-28 18:54:02 +00:00
|
|
|
|
2013-08-22 22:25:21 +00:00
|
|
|
data Page = Page
|
|
|
|
{ pageTitle :: String
|
|
|
|
, pageLinks :: [Link]
|
|
|
|
} deriving (Show)
|
|
|
|
|
|
|
|
data Link = Link
|
2015-05-08 17:39:00 +00:00
|
|
|
{ linkSuffix :: Maybe String
|
2013-08-22 22:25:21 +00:00
|
|
|
, linkUrl :: URLString
|
|
|
|
} deriving (Show)
|
|
|
|
|
2013-11-25 03:44:30 +00:00
|
|
|
{- JSON instances for quvi 0.4. -}
|
2013-08-22 22:25:21 +00:00
|
|
|
instance FromJSON Page where
|
|
|
|
parseJSON (Object v) = Page
|
|
|
|
<$> v .: "page_title"
|
|
|
|
<*> v .: "link"
|
|
|
|
parseJSON _ = mzero
|
|
|
|
|
|
|
|
instance FromJSON Link where
|
|
|
|
parseJSON (Object v) = Link
|
2015-05-08 17:39:00 +00:00
|
|
|
<$> v .:? "file_suffix"
|
2013-08-22 22:25:21 +00:00
|
|
|
<*> v .: "url"
|
|
|
|
parseJSON _ = mzero
|
|
|
|
|
2013-11-25 03:44:30 +00:00
|
|
|
{- "enum" format used by quvi 0.9 -}
|
|
|
|
parseEnum :: String -> Maybe Page
|
|
|
|
parseEnum s = Page
|
|
|
|
<$> get "QUVI_MEDIA_PROPERTY_TITLE"
|
2013-11-25 04:02:48 +00:00
|
|
|
<*> ((:[]) <$>
|
|
|
|
( Link
|
2015-05-08 17:39:00 +00:00
|
|
|
<$> Just <$> (get "QUVI_MEDIA_STREAM_PROPERTY_CONTAINER")
|
2013-11-25 04:02:48 +00:00
|
|
|
<*> get "QUVI_MEDIA_STREAM_PROPERTY_URL"
|
|
|
|
)
|
|
|
|
)
|
2013-11-25 03:44:30 +00:00
|
|
|
where
|
|
|
|
get = flip M.lookup m
|
|
|
|
m = M.fromList $ map (separate (== '=')) $ lines s
|
|
|
|
|
2014-02-28 18:54:02 +00:00
|
|
|
probeVersion :: IO QuviVersion
|
2015-02-10 15:59:55 +00:00
|
|
|
probeVersion = catchDefaultIO NoQuvi $
|
2015-02-19 18:31:04 +00:00
|
|
|
examine <$> processTranscript "quvi" ["--version"] Nothing
|
2014-02-28 18:54:02 +00:00
|
|
|
where
|
|
|
|
examine (s, True)
|
|
|
|
| "quvi v0.4" `isInfixOf` s = Quvi04
|
|
|
|
| otherwise = Quvi09
|
|
|
|
examine _ = NoQuvi
|
|
|
|
|
|
|
|
type Query a = QuviVersion -> [CommandParam] -> URLString -> IO a
|
2013-08-22 22:25:21 +00:00
|
|
|
|
|
|
|
{- Throws an error when quvi is not installed. -}
|
|
|
|
forceQuery :: Query (Maybe Page)
|
2014-02-28 18:54:02 +00:00
|
|
|
forceQuery v ps url = query' v ps url `catchNonAsync` onerr
|
2013-08-22 22:25:21 +00:00
|
|
|
where
|
2015-11-09 16:19:10 +00:00
|
|
|
onerr e = ifM (inPath "quvi")
|
|
|
|
( error ("quvi failed: " ++ show e)
|
2013-08-23 01:09:04 +00:00
|
|
|
, error "quvi is not installed"
|
|
|
|
)
|
2013-08-22 22:25:21 +00:00
|
|
|
|
|
|
|
{- Returns Nothing if the page is not a video page, or quvi is not
|
|
|
|
- installed. -}
|
|
|
|
query :: Query (Maybe Page)
|
2014-02-28 18:54:02 +00:00
|
|
|
query v ps url = flip catchNonAsync (const $ return Nothing) (query' v ps url)
|
2013-08-22 22:25:21 +00:00
|
|
|
|
|
|
|
query' :: Query (Maybe Page)
|
2014-02-28 18:54:02 +00:00
|
|
|
query' Quvi09 ps url = parseEnum
|
2015-11-09 16:19:10 +00:00
|
|
|
<$> readQuvi (toCommand $ [Param "dump", Param "-p", Param "enum"] ++ ps ++ [Param url])
|
|
|
|
query' Quvi04 ps url = do
|
|
|
|
let p = proc "quvi" (toCommand $ ps ++ [Param url])
|
|
|
|
decode . BL.fromStrict
|
|
|
|
<$> withHandle StdoutHandle createProcessSuccess p B.hGetContents
|
2014-02-28 18:54:02 +00:00
|
|
|
query' NoQuvi _ _ = return Nothing
|
2013-08-22 22:25:21 +00:00
|
|
|
|
|
|
|
queryLinks :: Query [URLString]
|
2014-02-28 18:54:02 +00:00
|
|
|
queryLinks v ps url = maybe [] (map linkUrl . pageLinks) <$> query v ps url
|
2013-08-22 22:25:21 +00:00
|
|
|
|
|
|
|
{- Checks if quvi can still find a download link for an url.
|
|
|
|
- If quvi is not installed, returns False. -}
|
|
|
|
check :: Query Bool
|
2014-02-28 18:54:02 +00:00
|
|
|
check v ps url = maybe False (not . null . pageLinks) <$> query v ps url
|
2013-08-22 22:25:21 +00:00
|
|
|
|
2013-11-25 03:44:30 +00:00
|
|
|
{- Checks if an url is supported by quvi, as quickly as possible
|
|
|
|
- (without hitting it if possible), and without outputting
|
2013-08-22 22:25:21 +00:00
|
|
|
- anything. Also returns False if quvi is not installed. -}
|
2014-02-28 18:54:02 +00:00
|
|
|
supported :: QuviVersion -> URLString -> IO Bool
|
|
|
|
supported NoQuvi _ = return False
|
|
|
|
supported Quvi04 url = boolSystem "quvi"
|
2015-06-02 20:28:05 +00:00
|
|
|
[ Param "--verbosity", Param "mute"
|
2015-06-01 17:52:23 +00:00
|
|
|
, Param "--support"
|
2014-02-28 18:54:02 +00:00
|
|
|
, Param url
|
|
|
|
]
|
|
|
|
{- Use quvi-info to see if the url's domain is supported.
|
|
|
|
- If so, have to do a online verification of the url. -}
|
|
|
|
supported Quvi09 url = (firstlevel <&&> secondlevel)
|
2013-11-25 03:44:30 +00:00
|
|
|
`catchNonAsync` (\_ -> return False)
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
firstlevel = case uriAuthority =<< parseURIRelaxed url of
|
2013-11-25 03:44:30 +00:00
|
|
|
Nothing -> return False
|
|
|
|
Just auth -> do
|
|
|
|
let domain = map toLower $ uriRegName auth
|
|
|
|
let basedomain = intercalate "." $ reverse $ take 2 $ reverse $ split "." domain
|
|
|
|
any (\h -> domain `isSuffixOf` h || basedomain `isSuffixOf` h)
|
2014-02-28 18:54:02 +00:00
|
|
|
. map (map toLower) <$> listdomains Quvi09
|
2013-11-25 03:44:30 +00:00
|
|
|
secondlevel = snd <$> processTranscript "quvi"
|
|
|
|
(toCommand [Param "dump", Param "-o", Param url]) Nothing
|
|
|
|
|
2014-02-28 18:54:02 +00:00
|
|
|
listdomains :: QuviVersion -> IO [String]
|
|
|
|
listdomains Quvi09 = concatMap (split ",")
|
|
|
|
. concatMap (drop 1 . words)
|
|
|
|
. filter ("domains: " `isPrefixOf`) . lines
|
2015-11-09 16:19:10 +00:00
|
|
|
<$> readQuvi (toCommand [Param "info", Param "-p", Param "domains"])
|
2014-02-28 18:54:02 +00:00
|
|
|
listdomains _ = return []
|
|
|
|
|
2015-06-01 17:52:23 +00:00
|
|
|
type QuviParams = QuviVersion -> [CommandParam]
|
2013-11-25 03:44:30 +00:00
|
|
|
|
|
|
|
{- Disables progress, but not information output. -}
|
2015-06-01 17:52:23 +00:00
|
|
|
quiet :: QuviParams
|
2014-02-28 18:54:02 +00:00
|
|
|
-- Cannot use quiet as it now disables informational output.
|
|
|
|
-- No way to disable progress.
|
2015-06-01 17:52:23 +00:00
|
|
|
quiet Quvi09 = [Param "--verbosity", Param "verbose"]
|
|
|
|
quiet Quvi04 = [Param "--verbosity", Param "quiet"]
|
|
|
|
quiet NoQuvi = []
|
2013-08-22 22:25:21 +00:00
|
|
|
|
|
|
|
{- Only return http results, not streaming protocols. -}
|
2015-06-01 17:52:23 +00:00
|
|
|
httponly :: QuviParams
|
2014-02-28 18:54:02 +00:00
|
|
|
-- No way to do it with 0.9?
|
2015-06-01 17:52:23 +00:00
|
|
|
httponly Quvi04 = [Param "-c", Param "http"]
|
|
|
|
httponly _ = [] -- No way to do it with 0.9?
|
2015-11-09 16:19:10 +00:00
|
|
|
|
|
|
|
{- Both versions of quvi will output utf-8 encoded data even when
|
|
|
|
- the locale doesn't support it. -}
|
|
|
|
readQuvi :: [String] -> IO String
|
|
|
|
readQuvi ps = withHandle StdoutHandle createProcessSuccess p $ \h -> do
|
|
|
|
fileEncoding h
|
|
|
|
r <- hGetContentsStrict h
|
|
|
|
hClose h
|
|
|
|
return r
|
|
|
|
where
|
|
|
|
p = proc "quvi" ps
|