lord 2.20131201 → 2.20131203
raw patch · 7 files changed
+69/−59 lines, 7 files
Files
- CHANGELOG +12/−0
- Radio.hs +22/−1
- Radio/EightTracks.hs +5/−22
- Radio/Jing.hs +1/−16
- lord.cabal +3/−3
- main.hs +3/−2
- test/main.hs +23/−15
CHANGELOG view
@@ -1,4 +1,16 @@ CHANGELOG +2.20131201 -> 2.20131203+ - Bug fixes++1.20131130 -> 2.20131201+ - Fallback to mplayer when mpd is unavailable++1.20131124 -> 1.20131130+ - Added support to http://8tracks.com++1.20131115 -> 1.20131124+ - Updated "other-modules" field in cabalfile, installable now.+ 1.20130928 -> 1.20131115 - Added support to http://radioreddit.com
Radio.hs view
@@ -20,6 +20,7 @@ import Control.Concurrent.MVar import qualified Control.Exception as E import Control.Monad (liftM, when, void)+import Data.Aeson hiding (encode) import qualified Data.ByteString as B import qualified Data.ByteString.Char8 as C import Data.Conduit (runResourceT, ($$+-))@@ -245,7 +246,27 @@ B.writeFile yml $ B.append bs (encode config) putStrLn $ "Your token has been saved to " ++ yml - readToken :: String -> IO (Maybe (Param a))+ mkParam :: Param a -> String -> Param a++ readToken :: FromJSON (Config a)+ => (Config a -> Param a) -> String -> IO (Maybe (Param a))+ readToken selector keywords = do+ home <- Radio.getLordDir+ let yml = home ++ "/lord.yml"+ exist <- doesFileExist yml+ if exist+ then do+ conf <- decodeFile yml+ case conf of+ Nothing -> error $ "Invalid YAML file: " ++ show conf+ Just c -> + case fromJSON c of+ Success tok -> return $ Just $ + mkParam (selector tok) keywords+ Error err -> do+ print $ "Parse token failed: " ++ show err+ return Nothing+ else return Nothing getLordDir :: IO FilePath getLordDir = (++ "/.lord") <$> getHomeDirectory
Radio/EightTracks.hs view
@@ -13,8 +13,7 @@ import Data.Aeson import Data.Aeson.Types (defaultOptions, Options(..)) import qualified Data.ByteString.Char8 as C-import Data.Maybe (fromJust)-import Data.Yaml hiding (decode)+import Data.Maybe (fromMaybe, fromJust) import Data.CaseInsensitive (mk) import Data.Char (isDigit) import Data.Conduit (($$+-))@@ -25,7 +24,6 @@ import Network.HTTP.Conduit import Prelude hiding (id) import System.Console.ANSI-import System.Directory (doesFileExist) import System.IO.Unsafe (unsafePerformIO) import Radio@@ -79,7 +77,7 @@ , track_file_stream_url :: String , name :: String , performer :: String- , release_name :: String+ , release_name :: Maybe String , url :: String } deriving (Show, Generic) instance FromJSON EightTracks@@ -139,7 +137,8 @@ songUrl _ x = return $ track_file_stream_url x - songMeta x = Radio.SongMeta (performer x) (release_name x) (name x)+ songMeta x = Radio.SongMeta (performer x) + (fromMaybe "" $ release_name x) (name x) tagged _ = False @@ -186,23 +185,7 @@ mkConfig = Config - readToken strMixId = do- mId <- getMixId strMixId- home <- Radio.getLordDir- let yml = home ++ "/lord.yml"- exist <- doesFileExist yml- if exist- then do- conf <- decodeFile yml- case conf of- Nothing -> error $ "Invalid YAML file: " ++ show conf- Just c -> - case fromJSON c of- Success tok -> return $ Just $ (eight tok) { mixId = mId }- Error err -> do- print $ "Parse token failed: " ++ show err- return Nothing- else return Nothing+ mkParam param key = param { mixId = read key } instance FromJSON (Radio.Config EightTracks) instance ToJSON (Radio.Config EightTracks)
Radio/Jing.hs view
@@ -17,14 +17,12 @@ import qualified Data.HashMap.Strict as HM import Data.Maybe (fromJust, fromMaybe) import qualified Data.Text as T-import Data.Yaml import Data.CaseInsensitive (mk) import Data.Conduit (runResourceT, ($$+-)) import Data.Conduit.Attoparsec (sinkParser) import GHC.Generics (Generic) import Network.HTTP.Types import Network.HTTP.Conduit-import System.Directory (doesFileExist) import Radio @@ -159,20 +157,7 @@ mkConfig = Config - readToken keywords = do- home <- Radio.getLordDir- let yml = home ++ "/lord.yml"- exist <- doesFileExist yml- if exist- then do- conf <- decodeFile yml- case conf of- Nothing -> error $ "Invalid YAML file: " ++ show conf- Just c -> - case fromJSON c of- Success tok -> return $ Just $ (jing tok) { cmbt = keywords }- Error err -> error $ "Parse token failed: " ++ show err- else return Nothing+ mkParam param key = param { cmbt = key } instance FromJSON (Radio.Config Jing) instance ToJSON (Radio.Config Jing)
lord.cabal view
@@ -1,8 +1,8 @@ name: lord-version: 2.20131201+version: 2.20131203 synopsis: A command line interface to online radios. description: - A unified command line interface to several online radios, use @mpd@ (<http://musicpd.org>) as backend by default. Will fallback to mplayer (http://www.mplayerhq.hu) when mpd is unavailable.+ A unified command line interface to several online radios, use mpd (<http://musicpd.org>) as backend by default. Will fallback to mplayer (<http://www.mplayerhq.hu>) when mpd is unavailable. . Supported radios: .@@ -53,7 +53,7 @@ source-repository head type: git location: git://github.com/rnons/lord.git- + executable lord main-is: main.hs other-modules: Radio,
main.hs view
@@ -210,7 +210,8 @@ etListen :: Bool -> Keywords -> IO () etListen nodaemon k = do- tok <- readToken k+ mId <- ET.getMixId k+ tok <- readToken ET.eight $ show mId case tok of Just tok' -> do putStrLn $ "Welcome back, " ++ ET.userName tok'@@ -224,7 +225,7 @@ jingListen :: Bool -> Keywords -> IO () jingListen nodaemon k = do- tok <- readToken k+ tok <- readToken jing k case tok of Just tok' -> do putStrLn $ "Welcome back, " ++ C.unpack (nick tok')
test/main.hs view
@@ -5,7 +5,7 @@ import Radio import qualified Radio.Cmd as Cmd-import qualified Radio.EightTracks as ET+import Radio.EightTracks import Radio.Douban import Radio.Jing import qualified Radio.Reddit as Reddit@@ -17,27 +17,35 @@ spec :: Spec spec = describe "getPlaylist" $ do it "cmd: given genre" $ do- ss <- Radio.getPlaylist (Cmd.Genre "Dream Pop")- assert $ not $ null ss+ ss <- getPlaylist (Cmd.Genre "Dream Pop")+ assertNotNull ss it "douban: given channel id" $ do- ss <- Radio.getPlaylist (Cid 6)- assert $ not $ null ss+ ss <- getPlaylist (Cid 6)+ assertNotNull ss it "douban: given musician name" $ do- ss <- Radio.getPlaylist (Musician "Sigur RóS")- assert $ not $ null ss+ ss <- getPlaylist (Musician "Sigur RóS")+ assertNotNull ss it "8tracks: given mix id" $ do- tok <- readToken "14" :: IO (Maybe (Radio.Param ET.EightTracks))- ss <- Radio.getPlaylist $ fromJust tok- assert $ not $ null ss+ tok <- readToken eight "14"+ ss <- getPlaylist $ fromJust tok+ assertNotNull ss + it "8tracks: given mix url" $ do+ mId <- getMixId "http://8tracks.com/an-nie/oblitus"+ tok <- readToken eight $ show mId+ ss <- getPlaylist $ fromJust tok+ assertNotNull ss+ it "jing: given keywords" $ do- tok <- readToken "postrock" :: IO (Maybe (Radio.Param Jing))- ss <- Radio.getPlaylist $ fromJust tok- assert $ not $ null ss+ tok <- readToken jing "postrock"+ ss <- getPlaylist $ fromJust tok+ assertNotNull ss it "reddit: given genre" $ do- ss <- Radio.getPlaylist (Reddit.Genre "indie")- assert $ not $ null ss+ ss <- getPlaylist (Reddit.Genre "indie")+ assertNotNull ss+ where+ assertNotNull = assert . not .null