lord 1.20130928 → 1.20131115
raw patch · 3 files changed
+38/−17 lines, 3 files
Files
- lord.cabal +3/−2
- main.hs +28/−13
- test/main.hs +7/−2
lord.cabal view
@@ -1,10 +1,11 @@ name: lord-version: 1.20130928-synopsis: Lord of radio daemon+version: 1.20131115+synopsis: A command line interface to online radios. description: A unified interface to online radio service providers, use mpd as backend. . Supported radios:+ <http://radioreddit.com> <http://cmd.fm> <http://douban.fm> <http://jing.fm>
main.hs view
@@ -4,13 +4,13 @@ import Data.Default (def) import Network.MPD (withMPD, clear, status, stState) import Options.Applicative-import Options.Applicative.Types (ParserPrefs, ParserInfo)+import Options.Applicative.Types (ParserPrefs) import System.Directory (createDirectoryIfMissing) import System.Environment (getArgs, getProgName) import System.Exit (exitWith, exitSuccess, ExitCode(..)) import System.IO ( openFile, IOMode(AppendMode) , hPutStr, stdout, stderr, SeekMode(..) )-import System.Log.FastLogger (mkLogger, Logger)+import System.Log.FastLogger (mkLogger) import System.Posix.Daemon import System.Posix.Files (stdFileMode) import System.Posix.IO ( fdWrite, createFile, setLock@@ -18,9 +18,10 @@ import System.Posix.Process (getProcessID) import Radio-import Radio.Cmd+import qualified Radio.Cmd as Cmd import Radio.Douban-import Radio.Jing+import Radio.Jing +import qualified Radio.Reddit as Reddit data Options = Options { optCommand :: Command@@ -30,6 +31,7 @@ data Command = CmdFM CmdSubCommand | DoubanFM DoubanSubCommand | JingFM JingSubCommand+ | RedditFM RedditSubCommand | Status | Kill deriving (Eq, Show)@@ -47,6 +49,10 @@ data JingSubCommand = JingListen String deriving (Eq, Show) +data RedditSubCommand = RedditListen String+ | RedditGenreList+ deriving (Eq, Show)+ type Keywords = String optParser :: Parser Options@@ -57,6 +63,8 @@ (progDesc "douban.fm commander")) <> command "jing" (info (helper <*> jingOptions) (progDesc "jing.fm commander"))+ <> command "reddit" (info (helper <*> redditOptions)+ (progDesc "radioreddit.com commander")) <> command "status" (info (pure Status) (progDesc "show current status")) <> command "kill" (info (pure Kill)@@ -72,8 +80,8 @@ case optCommand o of CmdFM subCommand -> case subCommand of- CmdListen genre -> listen nodaemon (cmd genre)- CmdGenreList -> cmdGenres+ CmdListen genre -> listen nodaemon (Cmd.Genre genre)+ CmdGenreList -> Cmd.genres >>= Cmd.pprGenres DoubanFM subCommand -> case subCommand of DoubanListen key -> listen nodaemon (douban key)@@ -81,6 +89,10 @@ DoubanTrending -> doubanTrending DoubanSearch key -> doubanSearch key JingFM (JingListen key) -> jingListen nodaemon key+ RedditFM subCommand ->+ case subCommand of+ RedditListen genre -> listen nodaemon (Reddit.Genre genre)+ RedditGenreList -> Reddit.genres >>= Reddit.pprGenres Status -> lordStatus Kill -> killLord @@ -137,8 +149,14 @@ (progDesc "Provide keywords to listen to jing.fm")) ) -cmdGenres :: IO ()-cmdGenres = genres >>= pprGenres+redditOptions :: Parser Command+redditOptions = RedditFM <$> subparser+ ( command "listen" + (info (helper <*> (RedditListen <$> argument str (metavar "GENRE")))+ (progDesc "Provide genre to listen to radioreddit.com"))+ <> command "genres" + (info (pure RedditGenreList) (progDesc "List available genres"))+ ) doubanHot :: IO () doubanHot = hot >>= pprChannels@@ -149,9 +167,6 @@ doubanSearch :: String -> IO () doubanSearch key = search key >>= pprChannels -cmd :: Keywords -> Radio.Param Cmd-cmd = Genre- douban :: Keywords -> Radio.Param Douban douban k | isChId k = Cid $ read k@@ -197,7 +212,7 @@ lordStatus :: IO () lordStatus = do running <- getPidFile >>= isRunning- status <- + myStatus <- if running then do st <- fmap stState <$> withMPD status let state = case st of@@ -206,4 +221,4 @@ song <- getStateFile >>= readFile return $ "[" ++ state ++ "] " ++ song else return "Not running!"- putStrLn status+ putStrLn myStatus
test/main.hs view
@@ -4,9 +4,10 @@ import Test.HUnit import Radio-import Radio.Cmd+import qualified Radio.Cmd as Cmd import Radio.Jing import Radio.Douban+import qualified Radio.Reddit as Reddit main :: IO ()@@ -17,7 +18,7 @@ spec = do describe "getPlaylist" $ do it "cmd: given genre" $ do- ss <- Radio.getPlaylist (Genre "Dream Pop")+ ss <- Radio.getPlaylist (Cmd.Genre "Dream Pop") assert $ length ss > 0 it "douban: given channel id" $ do@@ -31,4 +32,8 @@ it "jing" $ do tok <- readToken "postrock" ss <- Radio.getPlaylist $ fromJust tok+ assert $ length ss > 0++ it "reddit: given genre" $ do+ ss <- Radio.getPlaylist (Reddit.Genre "indie") assert $ length ss > 0