No 1 at Christmas

This is my post for the 2015 F# advent calendar series for the 5th of December. I wanted to do a post using F# Type Providers and data. For this post I’ve used FSharp.Data’s HTML and JSON providers to read data about UK Christmas No 1 singles and show some details.

Using the HTML Type Provider I was able to easily get all the Christmas No 1s from a table on the Official Charts website:

type Number1s = HtmlProvider<"http://www.officialcharts.com/chart-news/every-official-christmas-number-1-ever-__3618/">
let number1s = Number1s.GetSample()
[for row in number1s.Tables.Table1.Rows ->
    row.YEAR, row.ARTIST, row.TITLE
]

Next I wanted to get pictures for all the Christmas singles. Last.fm has a handy API which lets you search for track information using the title and artist name and returns some JSON, which can be fed into the JSON Type Provider:

let [<Literal>] sample = "http://ws.audioscrobbler.com/2.0/?method=track.search&track=save%20your%20love&artist=REN%C3%89E%20AND%20RENATO&format=json&api_key=" + api_key
type TrackSearch = JsonProvider<sample>
let trackSearch = TrackSearch.GetSample()
let track = trackSearch.Results.Trackmatches.Track.[0]
let pic = track.Image.[1].Text

Finally I wanted to make a table in HTML. For this I got a little help from dad and his static HTML library:

table [
 thead [tr [th %"Play"; th %"Year"; th %"Artist"; th %"Title"]]
 tbody
    [for row in number1s.Tables.Table1.Rows |> Array.rev ->
        let artist = 
            match row.ARTIST with
            | "MICHAEL ANDREWS & GARY JULES" -> "MICHAEL ANDREWS"
            | "MILITARY WIVES WITH GARETH MALONE" -> "MILITARY WIVES"
            | name -> name      
        printfn "Album %d %s %s" row.YEAR row.ARTIST row.TITLE
        let trackSearchRequest =
            "http://ws.audioscrobbler.com/2.0/" +
            "?method=track.search" +
            "&track=" + HttpUtility.UrlPathEncode (row.TITLE.Replace("?","").Replace("&","AND")) +
            "&artist=" + HttpUtility.UrlPathEncode (artist.Replace("&","AND")) +
            "&api_key=" + api_key +
            "&format=json"        
        let search = TrackSearch.Load(trackSearchRequest)
        let track0 = search.Results.Trackmatches.Track.[0]       
        let mediumImage = track0.Image.[1].Text
        tr [
            td [a (("href"%=track0.Url)::[img ["src"%=mediumImage]])]
            td %(sprintf "%d" row.YEAR)
            td %(row.ARTIST)
            td %(row.TITLE)
        ]
    ]
]
|> Html.toString

Here’s all the UK Christmas No 1s from 2014 until 1952 (my favourite is Rage Against the Machine):

Play Year Artist Title
2014 BEN HAENOW SOMETHING I NEED
2013 SAM BAILEY SKYSCRAPER
2012 THE JUSTICE COLLECTIVE HE AIN’T HEAVY, HE’S MY BROTHER
2011 MILITARY WIVES WITH GARETH MALONE WHEREVER YOU ARE
2010 MATT CARDLE WHEN WE COLLIDE
2009 RAGE AGAINST THE MACHINE KILLING IN THE NAME
2008 ALEXANDRA BURKE HALLELUJAH
2007 LEON JACKSON WHEN YOU BELIEVE
2006 LEONA LEWIS A MOMENT LIKE THIS
2005 SHAYNE WARD THAT’S MY GOAL
2004 BAND AID 20 DO THEY KNOW IT’S CHRISTMAS?
2003 MICHAEL ANDREWS & GARY JULES MAD WORLD
2002 GIRLS ALOUD SOUND OF THE UNDERGROUND
2001 ROBBIE WILLIAMS & NICOLE KIDMAN SOMETHIN’ STUPID
2000 BOB THE BUILDER CAN WE FIX IT?
1999 WESTLIFE I HAVE A DREAM/SEASONS IN THE SUN
1998 SPICE GIRLS GOODBYE
1997 SPICE GIRLS TOO MUCH
1996 SPICE GIRLS 2 BECOME 1
1995 MICHAEL JACKSON EARTH SONG
1994 EAST 17 STAY ANOTHER DAY
1993 MR BLOBBY MR BLOBBY
1992 WHITNEY HOUSTON I WILL ALWAYS LOVE YOU
1991 QUEEN BOHEMIAN RHAPSODY/THESE ARE THE DAYS OF OUR LIVES
1990 CLIFF RICHARD SAVIOUR’S DAY
1989 BAND AID II DO THEY KNOW IT’S CHRISTMAS?
1988 CLIFF RICHARD MISTLETOE AND WINE
1987 PET SHOP BOYS ALWAYS ON MY MIND
1986 JACKIE WILSON REET PETITE
1985 SHAKIN’ STEVENS MERRY CHRISTMAS EVERYONE
1984 BAND AID DO THEY KNOW IT’S CHRISTMAS?
1983 THE FLYING PICKETS ONLY YOU
1982 RENÉE AND RENATO SAVE YOUR LOVE
1981 THE HUMAN LEAGUE DON’T YOU WANT ME
1980 ST WINIFRED’S SCHOOL CHOIR THERE’S NO ONE QUITE LIKE GRANDMA
1979 PINK FLOYD ANOTHER BRICK IN THE WALL (PART 2)
1978 BONEY M MARY’S BOY CHILD – OH MY LORD
1977 WINGS MULL OF KINTYRE/GIRLS’ SCHOOL
1976 JOHNNY MATHIS WHEN A CHILD IS BORN (SOLEADO)
1975 QUEEN BOHEMIAN RHAPSODY
1974 MUD LONELY THIS CHRISTMAS
1973 SLADE MERRY XMAS EVERYBODY
1972 JIMMY OSMOND LONG HAIRED LOVER FROM LIVERPOOL
1971 BENNY HILL ERNIE (THE FASTEST MILKMAN IN THE WEST)
1970 DAVE EDMUNDS I HEAR YOU KNOCKING
1969 ROLF HARRIS TWO LITTLE BOYS
1968 THE SCAFFOLD LILY THE PINK
1967 THE BEATLES HELLO, GOODBYE
1966 TOM JONES GREEN, GREEN GRASS OF HOME
1965 THE BEATLES DAY TRIPPER/WE CAN WORK IT OUT
1964 THE BEATLES I FEEL FINE
1963 THE BEATLES I WANT TO HOLD YOUR HAND
1962 ELVIS PRESLEY RETURN TO SENDER
1961 DANNY WILLIAMS MOON RIVER
1960 CLIFF RICHARD & THE SHADOWS I LOVE YOU
1959 EMILE FORD & THE CHECKMATES WHAT DO YOU WANT TO MAKE THOSE EYES AT ME FOR?
1958 CONWAY TWITTY IT’S ONLY MAKE BELIEVE
1957 HARRY BELAFONTE MARY’S BOY CHILD
1956 JOHNNIE RAY JUST WALKIN’ IN THE RAIN
1955 DICKIE VALENTINE CHRISTMAS ALPHABET
1954 WINIFRED ATWELL LET’S HAVE ANOTHER PARTY
1953 FRANKIE LAINE ANSWER ME
1952 AL MARTINO HERE IN MY HEART

Merry Christmas and a happy New Year! Smile

Advertisements