Commits

Sebastián Magrí committed 08cdcd5

Implement showInfo for apps

  • Participants
  • Parent commits cd4e675

Comments (0)

Files changed (1)

File src/Network/SoundCloud/App.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
-                             , permalink_url          :: String
-                             , external_url           :: String
-                             , creator                :: String
-                             } deriving (Show, Generic)
+import Network.SoundCloud.Util (scGet, scResolve)
 
-instance FromJSON JsonRecord
-instance ToJSON   JsonRecord
+data Json = Json { id                     :: Int
+                 , permalink_url          :: String
+                 , external_url           :: String
+                 , creator                :: String
+                 } deriving (Show, Generic)
 
-decodeJSON :: String -> Maybe JsonRecord
-decodeJSON dat = decode (BSL.pack dat) :: Maybe JsonRecord
+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 app information"
+         Just o  ->
+             do let tmp = "%s\n\t%s by %s\n"
+                printf
+                  tmp
+                  (permalink_url o)
+                  (external_url o)
+                  (creator o)