HScD / src / Network / SoundCloud / Group.hs

{-# LANGUAGE DeriveGeneric #-}

{- |
   Module:      Network.SoundCloud.Group
   Copyright:   (c) 2012 Sebastián Ramírez Magrí <sebasmagri@gmail.com>
   License:     BSD3
   Maintainer:  Sebastián Ramírez Magrí <sebasmagri@gmail.com>
   Stability:   experimental

   Represents Groups as in http://soundcloud.com/groups
-}

module Network.SoundCloud.Group where

import Data.Aeson (FromJSON, ToJSON, decode)
import qualified Data.ByteString.Lazy.Char8 as BSL
import GHC.Generics (Generic)
import Text.Printf (printf)

import qualified Network.SoundCloud.MiniUser as User
import Network.SoundCloud.Util (scGet, scResolve)

-- | Represents group's JSON as a record
data JSON = JSON { id                     :: Int
                 , created_at             :: String
                 , permalink_url          :: String
                 , name                   :: String
                 , description            :: String
                 , short_description      :: String
                 , creator                :: User.JSON
                 } deriving (Show, Generic)

instance FromJSON JSON
instance ToJSON   JSON

-- | Decode a group's valid JSON string into a record
decodeJSON :: String -> Maybe JSON
decodeJSON dat = decode (BSL.pack dat) :: Maybe JSON

-- | Get a group record given a public URL
-- as in http://soundcloud.com/groups/group_name
getJSON :: String -> IO (Maybe JSON)
getJSON url =
    do tUrl <- scResolve url
       dat  <- scGet tUrl True
       case dat of
         Nothing -> return Nothing
         Just d  -> return $ decodeJSON d

-- | Show general information about a group in the
-- standard output
showInfo :: String -> IO ()
showInfo url =
    do obj <- getJSON url
       case obj of
         Nothing        -> putStrLn "Unable to get track information."
         Just o         ->
             do let tmp = "%s\n%s (Since %s)\n\t%s\nCreated by %s\n"
                printf
                  tmp
                  (permalink_url o)
                  (name o)
                  (created_at o)
                  (description o)
                  (User.username $ creator o)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.