Last.fm API

API Explanation

Last.fm is a service that allows users to track their music listening across various platforms like Spotify, Apple Music, or Tidal. It compiles your data and, along with the listening data of others, offers stats on your listening as well as recommendations. This API allows users to explore listening patterns using this compiled data. Specifically, you can use methods that fall into categories such as char.getTopArtists where you can retrieve all the top artists from a particular chart or artist.getSimilar where you can retrieve artists similar to the one you are interested in.

API Set-Up

To make use of the API, you need a regular Last.fm account. In addition to this, you need to apply for an API account, after which you will have access to an API key. You will be able to use this API key with a base URL of https://ws.audioscrobbler.com/2.0/ to make calls to the API. In R, you can add a method to these to retrieve specific information.

Example

In this example, I created a function to retrieve a URL, retrieve a page of data, and a final function to retrieve all data on the top artists on the charts.

lfm_api_URL <-
  function(api_key, method, page, limit) {
    lfm_api_endpoint <- "https://ws.audioscrobbler.com/2.0/?"
    
    method <- paste("method=", method, "&", sep="")
    key <- paste("api_key=", api_key, "&", sep="")
    limit <- paste("limit=", limit, "&", sep="")
    page <- paste("page=", page, "&", sep="")
    json <- "format=json"
    
    lfm_URL <- paste(lfm_api_endpoint, method, key, limit, page, json, sep="")
    
    return(lfm_URL)
  }


lfm_api_page <-
  function(api_key, method, page, limit) {
    url <- lfm_api_URL(api_key, method, page, limit)
    
    lfm_page_data <-
        url %>%
        GET() %>%
        content(as = "text",
                encoding = "UTF-8") %>% 
        fromJSON()
    
    return(lfm_page_data)
  }


lfm_api_GET <- 
  function(api_key, method, limit) {
    metadata_page <- lfm_api_page(api_key, method, page = 1, limit)
    pages <- as.numeric(metadata_page$artists$`@attr`$totalPages)

    lfm_df <- data.frame()

    for(i in 1:pages) {
      lfm_page <- lfm_api_page(api_key, method, i, 50)
  
      lfm_df <- 
        lfm_page$artists$artist %>%
        select(name, playcount, listeners, url) %>%
        mutate(page_id = i) %>%
        bind_rows(lfm_df)
  
      paste("Page",i,"of", pages,"collected", sep = " ") %>% 
        print()
  
      Sys.sleep(1)
    }
    
    return(lfm_df)
  }

In the following, I show how you would run the function using my API key and the method you want to run, in this case, chart.gettopartists.

my_api_key <- "insert key here"
chosen_method <- "chart.gettopartists"

lfm_topartists <- lfm_api_GET(my_api_key, chosen_method, 50)
# pre-collected data
top_artists <- read_csv("top_artists.csv")
Rows: 10000 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): name, url
dbl (3): playcount, listeners, page_id

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(top_artists)
# A tibble: 6 × 5
  name                   playcount listeners url                         page_id
  <chr>                      <dbl>     <dbl> <chr>                         <dbl>
1 Jonathan Bailey          2354750    249322 https://www.last.fm/music/…     200
2 HONESTAV                 1887000     77180 https://www.last.fm/music/…     200
3 Corey Kent               1561094    102213 https://www.last.fm/music/…     200
4 DIPIENS                   234534     35490 https://www.last.fm/music/…     200
5 Tulus                    6360052    102096 https://www.last.fm/music/…     200
6 Los Gemelos De Sinaloa   1637224     74490 https://www.last.fm/music/…     200