mpd-current-json 1.4.0.0 → 1.5.0.0
raw patch · 4 files changed
+87/−47 lines, 4 filesdep ~mpd-current-json
Dependency ranges changed: mpd-current-json
Files
- CHANGELOG.md +9/−0
- lib/Network/MPD/Parse.hs +35/−13
- mpd-current-json.cabal +2/−2
- src/Main.hs +41/−32
CHANGELOG.md view
@@ -1,3 +1,12 @@+# v1.5+- Add json keys+ - `volume`: Integer for volume percentage+ - `crossfade`: Integer seconds of crossfase+ - `mixramp_db`: Decibels for MixRamp, can use float (decimals) number+ - `mixramp_delay`: Seconds of delay for MixRamp, can use float number+ - `updating_db`: Returns `true` when updading, not present otherwise+- More code refactoring, prepping for v2 for more abstractions :p+ # v1.4.0 - Add "`next_filename`" for getting next song file URI relative to the music library.
lib/Network/MPD/Parse.hs view
@@ -1,5 +1,6 @@ module Network.MPD.Parse- ( getStatusItem+ ( getStatusField+ , getStatusFieldElement , getTag , processSong , maybePathCurrentSong@@ -20,19 +21,40 @@ {- | Extract a field from the returned MPD.Status data record. -This takes an @Either@ 'Network.MPD.MPDError' 'Network.MPD.Status'-value and a field label function @f@ as arguments. It returns @Just-(f st)@ if the input status is @Right st@, where @st@ is the-'Network.MPD.Status' value. This function helps to extract a-specific field from the @MPD.Status@ data record by providing the-corresponding field label function. If the input status "@st@" is-not @Right st@, indicating an error, or the field label function is-not applicable, it returns @Nothing@.+Helper to extract a specific field from the+[Network.MPD.Status](Network.MPD#Status) data record by providing the+corresponding field label. If the input status "@st@" is /not/ @Right a@,+indicating an error, or the field label function is not applicable, it+returns @Nothing@.++==== __Example__:++@+ghci> import qualified Network.MPD as MPD+ghci> st <- MPD.withMPD MPD.status+ghci> getStatusField st MPD.stVolume+@+Just (Just 100) -}-getStatusItem :: Either MPD.MPDError MPD.Status -> (MPD.Status -> a) -> Maybe a-getStatusItem (Right st) f = Just (f st)-getStatusItem _ _ = Nothing+getStatusField :: MPD.Response MPD.Status -> (MPD.Status -> a) -> Maybe a+getStatusField (Right st) f = Just (f st)+getStatusField _ _ = Nothing +{- | Go a level deeper than `getStatusField'. For nested @Maybe a@+fields from 'Network.MPD.Status'.++==== __Example__:++@+ghci> import qualified Network.MPD as MPD+ghci> st <- MPD.withMPD MPD.status+ghci> getStatusFieldElement st MPD.stVolume+@+Just 100+-}+getStatusFieldElement :: MPD.Response MPD.Status -> (MPD.Status -> Maybe a) -> Maybe a+getStatusFieldElement status item = fromMaybe Nothing $ getStatusField status item+ {- | @Either@ check for the returned value of 'Network.MPD.currentSong', then call 'processSong' or return @Nothing@. -}@@ -136,4 +158,4 @@ Just (MPD.Id int) -> Just int Nothing -> Nothing where- m = fromMaybe Nothing $ getStatusItem status item+ m = fromMaybe Nothing $ getStatusField status item
mpd-current-json.cabal view
@@ -7,7 +7,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 1.4.0.0+version: 1.5.0.0 synopsis: Print current MPD song and status as JSON -- A longer description of the package.@@ -60,7 +60,7 @@ , aeson , bytestring >=0.11 && <0.13 , aeson-pretty == 0.8.*- , mpd-current-json == 1.4.*+ , mpd-current-json == 1.5.* -- Directories containing source files. hs-source-dirs: src
src/Main.hs view
@@ -13,7 +13,8 @@ import Options ( optsParserInfo, execParser, Opts(optPass, optHost, optPort) ) -import Network.MPD.Parse ( getStatusItem+import Network.MPD.Parse ( getStatusField+ , getStatusFieldElement , getTag , maybePathCurrentSong , maybePathNextPlaylistSong@@ -22,8 +23,6 @@ , getStatusIdInt ) import Text.Read (readMaybe)--import Data.Maybe (fromMaybe) {- | Where the program connects to MPD and uses the helper functions to extract values, organize them into a list of key/value pairs, make them a 'Data.Aeson.Value' using 'Data.Aeson.object', then encode it to@@ -66,48 +65,51 @@ musicbrainz_Workid = getTag MUSICBRAINZ_WORKID cs let state :: Maybe String- state = case getStatusItem st MPD.stState of+ state = case getStatusField st MPD.stState of Just ps -> case ps of Playing -> Just "playing" Paused -> Just "paused" Stopped -> Just "stopped" Nothing -> Nothing - time = getStatusItem st MPD.stTime-- elapsed = case time of- Just t -> case t of- Just (e, _) -> Just e- _noTag -> Nothing- Nothing -> Nothing-- duration = case time of- Just t -> case t of- Just (_, d) -> Just d- _noTag -> Nothing- Nothing -> Nothing+ time = getStatusFieldElement st MPD.stTime+ elapsed = fst <$> time+ duration = snd <$> time elapsedPercent :: Maybe Double elapsedPercent = case time of- Just t -> case t of- Just t1 -> readMaybe $ printf "%.2f" (uncurry (/) t1 * 100)- Nothing -> Just 0- Nothing -> Nothing+ Just t1 -> readMaybe $ printf "%.2f" (uncurry (/) t1 * 100)+ Nothing -> Just 0.0 - repeatSt = getStatusItem st MPD.stRepeat- randomSt = getStatusItem st MPD.stRandom- singleSt = getStatusItem st MPD.stSingle- consumeSt = getStatusItem st MPD.stConsume- bitrate = getStatusItem st MPD.stBitrate- audioFormat = getStatusItem st MPD.stAudio- errorSt = getStatusItem st MPD.stError+ volumeSt :: Maybe Int+ volumeSt = fromIntegral <$> getStatusFieldElement st MPD.stVolume + repeatSt = getStatusField st MPD.stRepeat+ randomSt = getStatusField st MPD.stRandom+ singleSt = getStatusField st MPD.stSingle+ consumeSt = getStatusField st MPD.stConsume+ bitrate = getStatusField st MPD.stBitrate+ audioFormat = getStatusField st MPD.stAudio+ errorSt = getStatusField st MPD.stError++ updatingDbSt = isJust updatingDbMaybe+ where+ updatingDbMaybe = getStatusFieldElement st MPD.stUpdatingDb -- "Nothing" or "Just 1"+ isJust Nothing = Nothing+ isJust _ = Just True++ crossfadeSt :: Maybe Int+ crossfadeSt = fromIntegral <$> getStatusField st MPD.stXFadeWidth++ mixRampDbSt = getStatusField st MPD.stMixRampdB+ mixRampDelay = getStatusField st MPD.stMixRampDelay+ -- positon is an index starting from 0. Id starts from 1- let pos = getStatusItem st MPD.stSongPos- nextPos = fromMaybe Nothing $ getStatusItem st MPD.stNextSongPos+ let pos = getStatusField st MPD.stSongPos+ nextPos = getStatusFieldElement st MPD.stNextSongPos songId = getStatusIdInt MPD.stSongID st nextId = getStatusIdInt MPD.stNextSongID st- playlistLength = getStatusItem st MPD.stPlaylistLength+ playlistLength = getStatusField st MPD.stPlaylistLength nextPlaylistSong <- withMpdOpts $ MPD.playlistInfo nextId let filename = maybePathCurrentSong cs@@ -153,8 +155,13 @@ , "duration" .=? duration , "elapsed" .=? elapsed , "elapsed_percent" .=? elapsedPercent+ , "volume" .=? volumeSt , "audio_format" .=? audioFormat , "bitrate" .=? bitrate+ , "crossfade" .=? crossfadeSt+ , "mixramp_db" .=? mixRampDbSt+ , "mixramp_delay" .=? mixRampDelay+ , "updating_db" .=? updatingDbSt , "error" .=? errorSt ] @@ -196,7 +203,9 @@ -- status , "state", "repeat", "random", "single", "consume" , "duration", "elapsed", "elapsed_percent"- , "audio_format", "bitrate"+ , "volume", "audio_format", "bitrate"+ , "crossfade", "mixramp_db", "mixramp_delay"+ , "updating_db" , "error" -- playlist , "position", "next_position", "id", "next_id"