Skip to content

Instantly share code, notes, and snippets.

@dmalikov
Created March 28, 2012 22:01
Show Gist options
  • Save dmalikov/2230945 to your computer and use it in GitHub Desktop.
Save dmalikov/2230945 to your computer and use it in GitHub Desktop.
Find some unlistenable artists in user library (via liblastfm)
import Control.Arrow ((***), (&&&))
import Control.Monad ((<=<), forM_, join, liftM, liftM2, when)
import Data.Maybe (fromMaybe)
import Kludges
import Network.Lastfm.API.Artist (getInfo)
import Network.Lastfm.Types
import Text.Printf
import Text.XML.Light
import qualified Network.Lastfm.API.Library as L
newtype KludgeResponse = KludgeResponse {unwrap :: Element}
apiKey = APIKey "b25b959554ed76058ac220b7b2e0a026"
user = User "smpcln"
{- main stuff -}
main = do
putStrLn $ show user ++ "'s artists with less than 1000 listeners:"
artists <- fromMaybe [] `liftM` getArtists user
forM_ artists $ \a -> do
(l,p) <- getListenersAndPlaycount $ Artist a
when (l < 1000) $ printf "%s: %d listeners, %d plays\n" a l p
{- kludges kludges kludges -}
getArtists :: User -> IO (Maybe [String])
getArtists u = do
response <- L.getArtists u Nothing (Just $ Limit 10000) apiKey
case response of
Left _ -> return Nothing
Right r -> return $ allArtists r
where allArtists = mapM (content <=< tag "name") <=< tags "artist" <=< tag "artists" <=< wrap
getListenersAndPlaycount :: Artist -> IO (Int,Int)
getListenersAndPlaycount a = do
response <- getInfo (Left a) Nothing Nothing Nothing apiKey
return $ case response of
Left _ -> (0, 0)
Right r -> join (***) read . fromMaybe ("0","0") . getStats $ r
where
getStats = (collapseM . (getListeners &&& getPlaycount)) <=< tag "stats" <=< tag "artist" <=< wrap
getListeners = content <=< tag "listeners"
getPlaycount = content <=< tag "playcount"
collapseM :: (Maybe a, Maybe a) -> Maybe (a,a)
collapseM = uncurry $ liftM2 (,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment