Reading Top Songs With Haskell

Author: Kat Chuang @katychuang on Twitter

First import the modules we'll need for this exercise.


In [3]:
{-# LANGUAGE OverloadedStrings #-}

-- Define the module
--module TopSongs where

import Data.Aeson
import Data.Aeson.TH (deriveJSON)
import Control.Applicative
import qualified Data.ByteString.Lazy as B
import qualified Data.Text as T
import Text.Show

In [4]:
data Song = Song {
      mediaid :: String,
      artist :: String,
      title :: String,
      dateposted :: Int,
      siteid :: Int,
      sitename :: String,
      posturl :: String,
      postid :: Int,
      loved_count :: Int,
      posted_count :: Int,
      thumb_url :: String, 
      thumb_url_medium :: String,
      thumb_url_large :: String,
      thumb_url_artist :: Maybe String,
      time :: Int,
      description :: String,
      itunes_link :: String} deriving (Eq, Show, Read)

instance FromJSON Song where
  parseJSON (Object v) = Song 
                         <$> (v .: "mediaid")
                         <*> (v .: "artist")
                         <*> (v .: "title")
                         <*> (v .: "dateposted")
                         <*> (v .: "siteid")
                         <*> (v .: "sitename")
                         <*> (v .: "posturl")
                         <*> (v .: "postid")
                         <*> (v .: "loved_count")
                         <*> (v .: "posted_count")
                         <*> (v .: "thumb_url")
                         <*> (v .: "thumb_url_medium")
                         <*> (v .: "thumb_url_large")
                         <*> (v .: "thumb_url_artist")
                         <*> (v .: "time")
                         <*> (v .: "description")
                         <*> (v .: "itunes_link")

In [5]:
data Songs = Songs { 
    song0 :: Song,
    song1 :: Song,
    song2 :: Song,
    song3 :: Song,
    song4 :: Song,
    song5 :: Song,
    song6 :: Song,
    song7 :: Song,
    song8 :: Song,
    song9 :: Song,
    song10 :: Song,
    song11 :: Song,
    song12 :: Song,
    song13 :: Song,
    song14 :: Song,
    song15 :: Song,
    song16 :: Song,
    song17 :: Song,
    song18 :: Song,
    song19 :: Song} deriving (Eq, Show, Read)

    
instance FromJSON Songs where
  parseJSON (Object v) = Songs
                         <$> (v .: "0")
                         <*> (v .: "1")
                         <*> (v .: "2")
                         <*> (v .: "3")
                         <*> (v .: "4")
                         <*> (v .: "5")
                         <*> (v .: "6")
                         <*> (v .: "7")
                         <*> (v .: "8")
                         <*> (v .: "9")
                         <*> (v .: "10")
                         <*> (v .: "11")
                         <*> (v .: "12")
                         <*> (v .: "13")
                         <*> (v .: "14")
                         <*> (v .: "15")
                         <*> (v .: "16")
                         <*> (v .: "17")
                         <*> (v .: "18")
                         <*> (v .: "19")
  parseJSON _          = empty

Define the data types


In [ ]:
-- Define the module

In [ ]:


In [6]:
main :: IO ()
main = do
  input <- B.readFile "_data/songlist.json"
  let (Just song) = decode input :: Maybe Songs

  Prelude.putStrLn $ myFunction song0 song
  Prelude.putStrLn $ myFunction song1 song
  Prelude.putStrLn $ myFunction song2 song
  Prelude.putStrLn $ myFunction song3 song
  Prelude.putStrLn $ myFunction song4 song
  Prelude.putStrLn $ myFunction song5 song
  Prelude.putStrLn $ myFunction song6 song
  Prelude.putStrLn $ myFunction song7 song
  Prelude.putStrLn $ myFunction song8 song
  Prelude.putStrLn $ myFunction song9 song
  Prelude.putStrLn $ myFunction song10 song
  Prelude.putStrLn $ myFunction song11 song
  Prelude.putStrLn $ myFunction song12 song
  Prelude.putStrLn $ myFunction song13 song
  Prelude.putStrLn $ myFunction song14 song
  Prelude.putStrLn $ myFunction song15 song
  Prelude.putStrLn $ myFunction song16 song
  Prelude.putStrLn $ myFunction song17 song
  Prelude.putStrLn $ myFunction song18 song
  Prelude.putStrLn $ myFunction song19 song

myFunction y xs = "\"" ++ title (y xs) ++ "\" by " ++ artist (y xs) ++ " [" ++ ( show (loved_count (y xs))) ++ "] "

In [7]:
main


"Gemini feat. George Maple" by What So Not [6155] 
"Borderline" by Vanic X Tove Styrke [3610] 
"Burn With Me" by Whilk and Misky [1981] 
"Promises (feat. Fetty Wap & Dubbel Dutch)" by Baauer [2375] 
"Skeletons" by Trails and Ways [1754] 
"Adam's Song (Kasum Remix)" by Blink 182 [2147] 
"Circles" by Vanic X machineheart [15131] 
"Talking Body (Panic City Remix)" by Tove Lo [935] 
"Throw Some D's (LUCA LUSH & Dirty Chocolate Remix)" by Rich Boy [1330] 
"Outside (Anevo Remix)" by Calvin Harris feat. Ellie Goulding [743] 
"Born Free (SoySauce Edition)" by Stalking Gia [1448] 
"Helios Feat. Wayne Snow" by Darius [1092] 
"Open Up" by Gallant [1377] 
"HIGH" by SIVIK [468] 
"Shout (PINEO Remix)" by Tears For Fears [716] 
"Body (Stint Remix)" by Niia [1123] 
"Timeless (Tchami Remix)" by Caroline Koch [654] 
"VIII feat. Alexa Harley" by Mt. Wolf [493] 
"Zenith" by Ben Khan [925] 
"Finally (Original Mix)" by Mr. Belt & Wezol [681]