Commits

Sebastián Magrí committed d18d03f

Implement showInfo for users. JSON must be uppercase. Use MiniUser for user data embeded in other resource data. Use otherwise in scResourceType guards. Improve Track.showComments.

Comments (0)

Files changed (9)

src/Network/SoundCloud.hs

 
 scResourceShowInfo :: String -> IO ()
 scResourceShowInfo url | scResourceType url == "track"   = Track.showInfo url
-                       | scResourceType url == "user"    = User.showInfo
+                       | scResourceType url == "user"    = User.showInfo url
                        | scResourceType url == "set"     = Set.showInfo
                        | scResourceType url == "group"   = Group.showInfo
                        | scResourceType url == "app"     = App.showInfo url

src/Network/SoundCloud/App.hs

 
 import Network.SoundCloud.Util (scGet, scResolve)
 
-data Json = Json { id                     :: Int
+data JSON = JSON { id                     :: Int
                  , permalink_url          :: String
                  , external_url           :: String
                  , creator                :: String
                  } deriving (Show, Generic)
 
-instance FromJSON Json
-instance ToJSON   Json
+instance FromJSON JSON
+instance ToJSON   JSON
 
-decodeJson :: String -> Maybe Json
-decodeJson dat = decode (BSL.pack dat) :: Maybe Json
+decodeJSON :: String -> Maybe JSON
+decodeJSON dat = decode (BSL.pack dat) :: Maybe JSON
 
-getJson :: String -> IO (Maybe Json)
-getJson url =
+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
+         Just d  -> return $ decodeJSON d
 
 showInfo :: String -> IO ()
 showInfo url =
-    do obj <- getJson url
+    do obj <- getJSON url
        case obj of
          Nothing -> putStrLn "Unable to get app information"
          Just o  ->

src/Network/SoundCloud/Comment.hs

 
 import qualified Network.SoundCloud.MiniUser as User
 
-data Json = Json { id                     :: Int
+data JSON = JSON { id                     :: Int
                  , created_at             :: String
                  , body                   :: String
                  , timestamp              :: Maybe Int
-                 , user                   :: User.JsonRecord
+                 , user                   :: User.JSON
                  , track_id               :: Int
                  } deriving (Show, Generic)
 
-instance FromJSON Json
-instance ToJSON   Json
+instance FromJSON JSON
+instance ToJSON   JSON
 
-decodeJson :: String -> Maybe Json
-decodeJson dat = decode (BSL.pack dat) :: Maybe Json
+decodeJSON :: String -> Maybe JSON
+decodeJSON dat = decode (BSL.pack dat) :: Maybe JSON

src/Network/SoundCloud/Group.hs

 import qualified Data.ByteString.Lazy.Char8 as BSL
 import GHC.Generics (Generic)
 
-import qualified Network.SoundCloud.User as User
+import qualified Network.SoundCloud.MiniUser as User
 
-data JsonRecord = JsonRecord { id                     :: Int
-                             , created_at             :: String
-                             , permalink_url          :: String
-                             , name                   :: String
-                             , description            :: String
-                             , short_description      :: String
-                             , creator                :: User.JsonRecord
-                             } deriving (Show, Generic)
+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 JsonRecord
-instance ToJSON   JsonRecord
+instance FromJSON JSON
+instance ToJSON   JSON
 
-decodeJSON :: String -> Maybe JsonRecord
-decodeJSON dat = decode (BSL.pack dat) :: Maybe JsonRecord
+decodeJSON :: String -> Maybe JSON
+decodeJSON dat = decode (BSL.pack dat) :: Maybe JSON
 
-showInfo :: String -> IO ()
-showInfo trackUrl = putStrLn "Not Implemented"
+showInfo :: IO ()
+showInfo = putStrLn "Not Implemented for groups"

src/Network/SoundCloud/MiniUser.hs

 import qualified Data.ByteString.Lazy.Char8 as BSL
 import GHC.Generics (Generic)
 
-data JsonRecord = JsonRecord { id               :: Int
-                             , username         :: String
-                             , uri              :: String
-                             , permalink_url    :: String
-                             , avatar_url       :: Maybe String
-                             } deriving (Show, Generic)
+data JSON = JSON { id               :: Int
+                 , username         :: String
+                 , uri              :: String
+                 , permalink_url    :: String
+                 , avatar_url       :: Maybe String
+                 } deriving (Show, Generic)
 
-instance FromJSON JsonRecord
-instance ToJSON   JsonRecord
+instance FromJSON JSON
+instance ToJSON   JSON
 
-decodeJSON :: String -> Maybe JsonRecord
-decodeJSON dat = decode (BSL.pack dat) :: Maybe JsonRecord
+decodeJSON :: String -> Maybe JSON
+decodeJSON dat = decode (BSL.pack dat) :: Maybe JSON

src/Network/SoundCloud/Set.hs

 import qualified Data.ByteString.Lazy.Char8 as BSL
 import GHC.Generics (Generic)
 
-import qualified Network.SoundCloud.User as User
+import qualified Network.SoundCloud.MiniUser as User
 
-data JsonRecord = JsonRecord { id                     :: Int
-                             , created_at             :: String
-                             , user                   :: User.JsonRecord
-                             , title                  :: String
-                             , permalink_url          :: String
-                             , sharing                :: String
-                             , description            :: String
-                             , label                  :: User.JsonRecord
-                             , duration               :: Int
-                             , genre                  :: String
-                             , shared_to_count        :: Int
-                             , license                :: String
-                             , release_day            :: Int
-                             , release_month          :: Int
-                             , release_year           :: Int
-                             , streamable             :: Bool
-                             , downloadable           :: Bool
-                             , playlist_type          :: String
-                             } deriving (Show, Generic)
+data JSON = JSON { id                     :: Int
+                 , created_at             :: String
+                 , user                   :: User.JSON
+                 , title                  :: String
+                 , permalink_url          :: String
+                 , sharing                :: String
+                 , description            :: String
+                 , label                  :: User.JSON
+                 , duration               :: Int
+                 , genre                  :: String
+                 , shared_to_count        :: Int
+                 , license                :: String
+                 , release_day            :: Int
+                 , release_month          :: Int
+                 , release_year           :: Int
+                 , streamable             :: Bool
+                 , downloadable           :: Bool
+                 , playlist_type          :: String
+                 } deriving (Show, Generic)
 
-instance FromJSON JsonRecord
-instance ToJSON   JsonRecord
+instance FromJSON JSON
+instance ToJSON   JSON
 
-decodeJSON :: String -> Maybe JsonRecord
-decodeJSON dat = decode (BSL.pack dat) :: Maybe JsonRecord
+decodeJSON :: String -> Maybe JSON
+decodeJSON dat = decode (BSL.pack dat) :: Maybe JSON
 
-showInfo :: String -> IO ()
-showInfo trackUrl = putStrLn "Not Implemented"
+showInfo :: IO ()
+showInfo = putStrLn "Not Implemented for sets"

src/Network/SoundCloud/Track.hs

 import qualified Network.SoundCloud.MiniUser as User
 import qualified Network.SoundCloud.Comment as Comment
 
-data Json = Json { id                     :: Int
+data JSON = JSON { id                     :: Int
                  , created_at             :: String
-                 , user                   :: User.JsonRecord
+                 , user                   :: User.JSON
                  , title                  :: String
                  , permalink_url          :: String
                  , sharing                :: String
                  , tag_list               :: String
                  } deriving (Show, Generic)
 
-instance FromJSON Json
-instance ToJSON   Json
+instance FromJSON JSON
+instance ToJSON   JSON
 
-data DownloadJson = DownloadJson { download_url     :: String
+data DownloadJSON = DownloadJSON { download_url     :: String
                                  } deriving (Show, Generic)
 
-instance FromJSON DownloadJson
-instance ToJSON   DownloadJson
+instance FromJSON DownloadJSON
+instance ToJSON   DownloadJSON
 
-decodeJson :: String -> Maybe Json
-decodeJson dat = decode (BSL.pack dat) :: Maybe Json
+decodeJSON :: String -> Maybe JSON
+decodeJSON dat = decode (BSL.pack dat) :: Maybe JSON
 
-getJson :: String -> IO (Maybe Json)
-getJson url =
+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
+         Just d  -> return $ decodeJSON d
 
-decodeDownloadJson :: String -> Maybe DownloadJson
-decodeDownloadJson dat = decode (BSL.pack dat) :: Maybe DownloadJson
+decodeDownloadJSON :: String -> Maybe DownloadJSON
+decodeDownloadJSON dat = decode (BSL.pack dat) :: Maybe DownloadJSON
 
-decodeComments :: String -> Maybe [Comment.Json]
-decodeComments dat = decode (BSL.pack dat) :: Maybe [Comment.Json]
+decodeComments :: String -> Maybe [Comment.JSON]
+decodeComments dat = decode (BSL.pack dat) :: Maybe [Comment.JSON]
 
-getComments :: Int -> IO (Maybe [Comment.Json])
+getComments :: Int -> IO (Maybe [Comment.JSON])
 getComments trackId =
     do let url = tracksURL ++ "/" ++ show trackId ++ "/comments.json?client_id=" ++ clientId
        dat <- scGet url True
          Nothing -> return Nothing
          Just d  -> return $ decodeComments d
 
-showComment :: Comment.Json -> String
+showComment :: Comment.JSON -> String
 showComment c = concat ["\nAt ", Comment.created_at c, ", ", User.username $ Comment.user c, " said:\n", Comment.body c, "\n"]
 
-showComments :: [Comment.Json] -> String
-showComments comments = if null comments then "No comments" else concatMap showComment comments
+showComments :: [Comment.JSON] -> String
+showComments [] = "No comments"
+showComments xs = concatMap showComment xs
 
 fetch :: String -> String -> IO ()
 fetch trackUrl output =
        case dat of
          Nothing -> putStrLn "Unable to connect"
          Just d  ->
-             do let o = decodeJson d
+             do let o = decodeJSON d
                 case o of
                   Nothing        -> putStrLn "Unable to get track information."
                   Just obj       ->
                       if downloadable obj then
-                          do let obj0 = decodeDownloadJson d
+                          do let obj0 = decodeDownloadJSON d
                              let dUrlStr = concat [download_url $ fromJust obj0, "?client_id=", clientId]
                              let filename = if null output then
                                                "./" ++ title obj ++ "." ++ original_format obj
                              scFetch dUrlStr filename
                       else putStrLn "Track is not downloadable"
 
-trackComments :: Int -> IO [Comment.Json]
+trackComments :: Int -> IO [Comment.JSON]
 trackComments trackId =
     do obj <- getComments trackId
        case obj of
 
 showInfo :: String -> IO ()
 showInfo trackUrl =
-    do obj <- getJson trackUrl
+    do obj <- getJSON trackUrl
        case obj of
          Nothing        -> putStrLn "Unable to get track information."
          Just o         ->

src/Network/SoundCloud/User.hs

 import Data.Aeson (FromJSON, ToJSON, decode)
 import qualified Data.ByteString.Lazy.Char8 as BSL
 import GHC.Generics (Generic)
+import Text.Printf (printf)
 
-data JsonRecord = JsonRecord { id                     :: Int
-                             , uri                    :: String
-                             , permalink_url          :: String
-                             , country                :: String
-                             , full_name              :: String
-                             , city                   :: String
-                             , description            :: String
-                             , website                :: String
-                             , website_title          :: String
-                             , online                 :: Bool
-                             , track_count            :: Int
-                             , playlist_count         :: Int
-                             , followers_count        :: Int
-                             , followings_count       :: Int
-                             , public_favorites_count :: Int
-                             } deriving (Show, Generic)
+import Network.SoundCloud.Util (scGet, scResolve)
 
-instance FromJSON JsonRecord
-instance ToJSON   JsonRecord
 
-decodeJSON :: String -> Maybe JsonRecord
-decodeJSON dat = decode (BSL.pack dat) :: Maybe JsonRecord
+data JSON = JSON { id                     :: Int
+                 , uri                    :: String
+                 , permalink_url          :: String
+                 , country                :: String
+                 , full_name              :: String
+                 , city                   :: String
+                 , description            :: String
+                 , website                :: String
+                 , website_title          :: String
+                 , online                 :: Bool
+                 , track_count            :: Int
+                 , playlist_count         :: Int
+                 , followers_count        :: Int
+                 , followings_count       :: Int
+                 , public_favorites_count :: Int
+                 } deriving (Show, Generic)
+
+instance FromJSON JSON
+instance ToJSON   JSON
+
+decodeJSON :: String -> Maybe JSON
+decodeJSON dat = decode (BSL.pack dat) :: Maybe JSON
+
+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
 
 showInfo :: String -> IO ()
-showInfo trackUrl = putStrLn "Not Implemented"
+showInfo url =
+    do obj <- getJSON url
+       case obj of
+         Nothing        -> putStrLn "Unable to get track information."
+         Just o         ->
+             do let tmp = "%s\n%s (%s)\n\t%s\n%s, %s\n%d tracks, %d sets.\nFollowing: %d\nFollowed by: %d\n"
+                printf
+                  tmp
+                  (permalink_url o)
+                  (full_name o)
+                  (website o)
+                  (description o)
+                  (city o)
+                  (country o)
+                  (track_count o)
+                  (playlist_count o)
+                  (followings_count o)
+                  (followers_count o)
+

src/Network/SoundCloud/Util.hs

                    | groupsURL    `isPrefixOf` url      = "group"
                    | commentsURL  `isPrefixOf` url      = "comment"
                    | appsURLS     `isPrefixOf` url      = "app"
-scResourceType _                                        = ""
+                   | otherwise                          = "app"
 
 {-
 This function's request will always return a (3,_,_) status,