2013-08-22 22:25:21 +00:00
|
|
|
{- querying quvi (import qualified)
|
|
|
|
-
|
|
|
|
- Copyright 2013 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Utility.Quvi where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Utility.Url
|
2013-11-25 03:44:30 +00:00
|
|
|
import Build.SysConfig (newquvi)
|
2013-08-22 22:25:21 +00:00
|
|
|
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.ByteString.Lazy.UTF8 (fromString)
|
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
|
|
|
|
|
|
|
data Page = Page
|
|
|
|
{ pageTitle :: String
|
|
|
|
, pageLinks :: [Link]
|
|
|
|
} deriving (Show)
|
|
|
|
|
|
|
|
data Link = Link
|
|
|
|
{ linkSuffix :: String
|
|
|
|
, 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
|
|
|
|
<$> v .: "file_suffix"
|
|
|
|
<*> 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
|
|
|
|
<$> get "QUVI_MEDIA_STREAM_PROPERTY_CONTAINER"
|
|
|
|
<*> 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
|
|
|
|
|
2013-08-22 22:25:21 +00:00
|
|
|
type Query a = [CommandParam] -> URLString -> IO a
|
|
|
|
|
|
|
|
{- Throws an error when quvi is not installed. -}
|
|
|
|
forceQuery :: Query (Maybe Page)
|
2013-08-23 01:09:04 +00:00
|
|
|
forceQuery ps url = query' ps url `catchNonAsync` onerr
|
2013-08-22 22:25:21 +00:00
|
|
|
where
|
2013-08-23 01:09:04 +00:00
|
|
|
onerr _ = ifM (inPath "quvi")
|
|
|
|
( error "quvi failed"
|
|
|
|
, 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)
|
|
|
|
query ps url = flip catchNonAsync (const $ return Nothing) (query' ps url)
|
|
|
|
|
|
|
|
query' :: Query (Maybe Page)
|
2013-11-25 03:44:30 +00:00
|
|
|
query' ps url
|
|
|
|
| newquvi = parseEnum
|
|
|
|
<$> readProcess "quvi" (toCommand $ [Param "dump", Param "-p", Param "enum"] ++ ps ++ [Param url])
|
|
|
|
| otherwise = decode . fromString
|
|
|
|
<$> readProcess "quvi" (toCommand $ ps ++ [Param url])
|
2013-08-22 22:25:21 +00:00
|
|
|
|
|
|
|
queryLinks :: Query [URLString]
|
|
|
|
queryLinks ps url = maybe [] (map linkUrl . pageLinks) <$> query ps url
|
|
|
|
|
|
|
|
{- Checks if quvi can still find a download link for an url.
|
|
|
|
- If quvi is not installed, returns False. -}
|
|
|
|
check :: Query Bool
|
|
|
|
check ps url = maybe False (not . null . pageLinks) <$> query ps url
|
|
|
|
|
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. -}
|
|
|
|
supported :: URLString -> IO Bool
|
2013-11-25 03:44:30 +00:00
|
|
|
supported url
|
|
|
|
{- Use quvi-info to see if the url's domain is supported.
|
|
|
|
- If so, have to do a online verification of the url. -}
|
|
|
|
| newquvi = (firstlevel <&&> secondlevel)
|
|
|
|
`catchNonAsync` (\_ -> return False)
|
|
|
|
| otherwise = boolSystem "quvi" [Params "--verbosity mute --support", Param url]
|
|
|
|
where
|
|
|
|
firstlevel = case uriAuthority =<< parseURIRelaxed url of
|
|
|
|
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)
|
|
|
|
. map (map toLower) <$> listdomains
|
|
|
|
secondlevel = snd <$> processTranscript "quvi"
|
|
|
|
(toCommand [Param "dump", Param "-o", Param url]) Nothing
|
|
|
|
|
|
|
|
listdomains :: IO [String]
|
|
|
|
listdomains
|
|
|
|
| newquvi = concatMap (split ",")
|
|
|
|
. concatMap (drop 1 . words)
|
|
|
|
. filter ("domains: " `isPrefixOf`) . lines
|
|
|
|
<$> readProcess "quvi"
|
|
|
|
(toCommand [Param "info", Param "-p", Param "domains"])
|
|
|
|
| otherwise = return []
|
|
|
|
|
|
|
|
{- Disables progress, but not information output. -}
|
2013-08-22 22:25:21 +00:00
|
|
|
quiet :: CommandParam
|
2013-11-25 03:44:30 +00:00
|
|
|
quiet
|
|
|
|
-- Cannot use quiet as it now disables informational output.
|
|
|
|
-- No way to disable progress.
|
2013-11-25 03:51:25 +00:00
|
|
|
| newquvi = Params "--verbosity verbose"
|
2013-11-25 03:44:30 +00:00
|
|
|
| otherwise = Params "--verbosity quiet"
|
2013-08-22 22:25:21 +00:00
|
|
|
|
|
|
|
{- Only return http results, not streaming protocols. -}
|
|
|
|
httponly :: CommandParam
|
2013-11-25 03:44:30 +00:00
|
|
|
httponly
|
|
|
|
-- No way to do it with 0.9?
|
|
|
|
| newquvi = Params ""
|
|
|
|
| otherwise = Params "-c http"
|