mpd-current-json 1.3.0.0 → 1.3.2.0
raw patch · 4 files changed
+49/−16 lines, 4 files
Files
- CHANGELOG.md +6/−0
- lib/Network/MPD/Parse.hs +21/−1
- mpd-current-json.cabal +1/−1
- src/Main.hs +21/−14
CHANGELOG.md view
@@ -1,3 +1,9 @@+# v1.3.2+- Add "`next_position`", "`id`" and "`next_id`" keys to `playlist`.++# v1.3.1+- Move helper function `objectJson` to lib+ # v1.3 - Add `filename` key. - Add `playlist` key and move existing keys to it.
lib/Network/MPD/Parse.hs view
@@ -6,12 +6,16 @@ , headMay , valueToStringMay , (.=?)+ , objectJson+ , getStatusIdInt ) where import qualified Network.MPD as MPD import Network.MPD ( Metadata(..), Song, PlaybackState(Stopped, Playing, Paused) )-import Data.Aeson ( Key, KeyValue(..), ToJSON )+import Data.Aeson ( object, Key, KeyValue(..), ToJSON, Value )+import Data.Aeson.Types ( Pair )+import Data.Maybe ( catMaybes, fromMaybe ) {- | Extract a field from the returned MPD.Status data record. @@ -104,3 +108,19 @@ (.=?) :: (KeyValue e a, ToJSON v) => Key -> Maybe v -> Maybe a key .=? Just value = Just (key .= value) _ .=? Nothing = Nothing++-- | Helper function for creating an JSON 'Data.Aeson.object' where+-- 'Data.Maybe.catMaybes' won't include items from the '[Maybe Pair]'+-- list that return 'Nothing'.+objectJson :: [Maybe Pair] -> Value+objectJson = object . catMaybes++-- | Extracts the 'Int' value from an 'MPD.Id' within 'MPD.Status', if+-- present and the 'Either' value is a 'Right'.+getStatusIdInt :: (MPD.Status -> Maybe MPD.Id) -> Either MPD.MPDError MPD.Status -> Maybe Int+getStatusIdInt item status =+ case m of+ Just (MPD.Id int) -> Just int+ Nothing -> Nothing+ where+ m = fromMaybe Nothing $ getStatusItem 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.3.0.0+version: 1.3.2.0 synopsis: Print current MPD song and status as JSON -- A longer description of the package.
src/Main.hs view
@@ -5,8 +5,7 @@ import qualified Network.MPD as MPD import Network.MPD ( Metadata(..), PlaybackState(Stopped, Playing, Paused) )-import Data.Maybe ( catMaybes )-import Data.Aeson ( object, KeyValue((.=)), Value )+import Data.Aeson ( object, KeyValue((.=)) ) import Data.Aeson.Encode.Pretty ( defConfig, encodePretty', keyOrder, Config(confCompare) ) import qualified Data.ByteString.Lazy.Char8 as C@@ -17,10 +16,13 @@ import Network.MPD.Parse ( getStatusItem , getTag , maybePath- , (.=?) )+ , (.=?)+ , objectJson+ , getStatusIdInt ) import Text.Read (readMaybe)-import Data.Aeson.Types (Pair)++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@@ -94,12 +96,17 @@ randomSt = getStatusItem st MPD.stRandom singleSt = getStatusItem st MPD.stSingle consumeSt = getStatusItem st MPD.stConsume- pos = getStatusItem st MPD.stSongPos- playlistLength = getStatusItem st MPD.stPlaylistLength bitrate = getStatusItem st MPD.stBitrate audioFormat = getStatusItem st MPD.stAudio errorSt = getStatusItem st MPD.stError + -- positon is an index starting from 0. Id starts from 1+ let pos = getStatusItem st MPD.stSongPos+ nextPos = fromMaybe Nothing $ getStatusItem st MPD.stNextSongPos+ songId = getStatusIdInt MPD.stSongID st+ nextId = getStatusIdInt MPD.stNextSongID st+ playlistLength = getStatusItem st MPD.stPlaylistLength+ let filename = maybePath cs -- sgTags@@ -150,8 +157,11 @@ -- let jFilename = objectJson [ "file" .=? filename ] let jPlaylist = objectJson- [ "position" .=? pos- , "length" .=? playlistLength+ [ "position" .=? pos -- current song position+ , "next_position" .=? nextPos+ , "id" .=? songId -- current song id+ , "next_id" .=? nextId+ , "length" .=? playlistLength ] let jObject = object [ "filename" .= filename@@ -183,11 +193,8 @@ , "duration", "elapsed", "elapsed_percent" , "audio_format", "bitrate" , "error"+ -- playlist+ , "position", "next_position", "id", "next_id"+ , "length" ] }---- | Helper function for creating an JSON 'Data.Aeson.object' where--- 'Data.Maybe.catMaybes' won't include items from the '[Maybe Pair]'--- list that return 'Nothing'.-objectJson :: [Maybe Pair] -> Value-objectJson = object . catMaybes