Skip to content

Instantly share code, notes, and snippets.

@ercas
Last active July 21, 2021 15:58
Show Gist options
  • Select an option

  • Save ercas/f969bf8854ec8721c3283d64f228f040 to your computer and use it in GitHub Desktop.

Select an option

Save ercas/f969bf8854ec8721c3283d64f228f040 to your computer and use it in GitHub Desktop.

Revisions

  1. ercas revised this gist Jul 21, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion listenbrainz.R
    Original file line number Diff line number Diff line change
    @@ -109,7 +109,7 @@ data %>%
    y = "Hour",
    title = "Most active listening times (note log scale)"
    ) +
    scale_x_continuous(expand = c(0, 0)) +
    scale_x_datetime(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0)) +
    theme(
    # viridis: https://github.com/BIDS/colormap/blob/master/colormaps.py#L788
  2. ercas revised this gist Jul 21, 2021. 1 changed file with 21 additions and 9 deletions.
    30 changes: 21 additions & 9 deletions listenbrainz.R
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ library(stringr)

    # load data ---------------------------------------------------------------

    data <- fromJSON("ercas_lb-2020-12-23.json") %>%
    data <- fromJSON("ercas_lb-2021-07-21.json") %>%
    flatten() %>%
    mutate(
    listened_at = with_tz(
    @@ -86,21 +86,33 @@ data %>%

    # top listening times -----------------------------------------------------

    weeks_since_epoch <- function(dt) {
    return(floor(as.numeric(dt) / (60*60*24*7)))
    }

    data %>%
    transmute(
    week = week(listened_at),
    mutate(
    week = weeks_since_epoch(listened_at),
    hour = hour(listened_at)
    ) %>%
    table() %>%
    as.data.frame() %>%
    group_by(week, hour) %>%
    summarize(
    week_of = floor_date(min(listened_at) - 1, "weeks") + 1,
    listens = n()
    ) %>%
    ggplot() +
    aes(week, hour, fill = log(Freq)) +
    aes(week_of, hour, fill = log(listens)) +
    geom_tile() +
    scale_fill_viridis_c(na.value = "black") +
    scale_fill_viridis_c() +
    labs(
    x = "Week",
    y = "Hour",
    title = "Most active listening times (note log scale)"
    ) +
    theme_minimal() +
    theme(axis.text.x = element_text(angle=90))
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0)) +
    theme(
    # viridis: https://github.com/BIDS/colormap/blob/master/colormaps.py#L788
    panel.background = element_rect(fill = rgb(0.267004, 0.004874, 0.329415)),
    panel.grid = element_blank()
    )
  3. ercas revised this gist Dec 23, 2020. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions listenbrainz.R
    Original file line number Diff line number Diff line change
    @@ -73,16 +73,16 @@ data %>%
    )
    ) %>%
    ggplot() +
    aes(x = listened_at, fill = Album) +
    geom_density(position = "stack", color = NA) +
    labs(
    x = "Date",
    y = "Density",
    title = sprintf("Top %d most listened-to albums over time", n_albums)
    ) +
    scale_fill_brewer(palette = "Spectral") +
    theme_minimal() +
    scale_y_continuous(breaks = NULL)
    aes(x = listened_at, fill = Album) +
    geom_density(position = "stack", color = NA) +
    labs(
    x = "Date",
    y = "Density",
    title = sprintf("Top %d most listened-to albums over time", n_albums)
    ) +
    scale_fill_brewer(palette = "Spectral") +
    theme_minimal() +
    scale_y_continuous(breaks = NULL)

    # top listening times -----------------------------------------------------

  4. ercas revised this gist Dec 23, 2020. 1 changed file with 0 additions and 32 deletions.
    32 changes: 0 additions & 32 deletions listenbrainz.R
    Original file line number Diff line number Diff line change
    @@ -84,38 +84,6 @@ data %>%
    theme_minimal() +
    scale_y_continuous(breaks = NULL)


    # top songs ---------------------------------------------------------------

    n_songs <- 10

    top_songs <- data$track_metadata.track_name %>%
    table() %>%
    as.data.frame() %>%
    arrange(desc(Freq)) %>%
    .$. %>%
    head(n_songs)

    data %>%
    mutate(
    Track = ifelse(
    track_metadata.track_name %in% top_songs,
    track_metadata.track_name,
    "All others"
    )
    ) %>%
    ggplot() +
    aes(x = listened_at, fill = Track) +
    geom_density(position = "stack", color = NA) +
    labs(
    x = "Date",
    y = "Density",
    title = sprintf("Top %d most listened-to tracks over time", n_songs)
    ) +
    scale_fill_brewer(palette = "Spectral") +
    theme_minimal() +
    scale_y_continuous(breaks = NULL)

    # top listening times -----------------------------------------------------

    data %>%
  5. ercas created this gist Dec 23, 2020.
    138 changes: 138 additions & 0 deletions listenbrainz.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,138 @@
    library(dplyr)
    library(ggplot2)
    library(jsonlite)
    library(lubridate)
    library(stringr)

    # load data ---------------------------------------------------------------

    data <- fromJSON("ercas_lb-2020-12-23.json") %>%
    flatten() %>%
    mutate(
    listened_at = with_tz(
    as_datetime(listened_at),
    system("readlink -f /etc/localtime | grep -o '[^/]*/[^/]*$'", intern = TRUE)
    )
    ) %>%
    filter(listened_at >= as_datetime("2020-01-01"))

    # top artists -------------------------------------------------------------

    n_artists <- 10

    top_artists <- data$track_metadata.artist_name %>%
    table() %>%
    as.data.frame() %>%
    arrange(desc(Freq)) %>%
    .$. %>%
    head(n_artists)

    data %>%
    mutate(
    Artist = ifelse(
    #track_metadata.artist_name %in% top_artists,
    #track_metadata.artist_name,
    str_detect(track_metadata.artist_name, paste(top_artists, collapse = "|")),
    str_extract(track_metadata.artist_name, paste(top_artists, collapse = "|")),
    "All others"
    )
    ) %>%
    ggplot() +
    aes(x = listened_at, fill = Artist) +
    geom_density(position = "stack", color = NA) +
    labs(
    x = "Date",
    y = "Density",
    title = sprintf("Top %d most listened-to artists over time", n_artists)
    ) +
    scale_fill_brewer(palette = "Spectral") +
    theme_minimal() +
    scale_y_continuous(breaks = NULL)

    # top albums --------------------------------------------------------------

    n_albums <- 10

    top_albums <- data$track_metadata.release_name %>%
    table() %>%
    as.data.frame() %>%
    arrange(desc(Freq)) %>%
    .$. %>%
    head(n_albums)

    data %>%
    mutate(
    Album = ifelse(
    track_metadata.release_name %in% top_albums,
    ifelse(
    str_starts(track_metadata.release_name, "The Idler Wheel"),
    "The Idler Wheel (...)",
    track_metadata.release_name
    ),
    "All others"
    )
    ) %>%
    ggplot() +
    aes(x = listened_at, fill = Album) +
    geom_density(position = "stack", color = NA) +
    labs(
    x = "Date",
    y = "Density",
    title = sprintf("Top %d most listened-to albums over time", n_albums)
    ) +
    scale_fill_brewer(palette = "Spectral") +
    theme_minimal() +
    scale_y_continuous(breaks = NULL)


    # top songs ---------------------------------------------------------------

    n_songs <- 10

    top_songs <- data$track_metadata.track_name %>%
    table() %>%
    as.data.frame() %>%
    arrange(desc(Freq)) %>%
    .$. %>%
    head(n_songs)

    data %>%
    mutate(
    Track = ifelse(
    track_metadata.track_name %in% top_songs,
    track_metadata.track_name,
    "All others"
    )
    ) %>%
    ggplot() +
    aes(x = listened_at, fill = Track) +
    geom_density(position = "stack", color = NA) +
    labs(
    x = "Date",
    y = "Density",
    title = sprintf("Top %d most listened-to tracks over time", n_songs)
    ) +
    scale_fill_brewer(palette = "Spectral") +
    theme_minimal() +
    scale_y_continuous(breaks = NULL)

    # top listening times -----------------------------------------------------

    data %>%
    transmute(
    week = week(listened_at),
    hour = hour(listened_at)
    ) %>%
    table() %>%
    as.data.frame() %>%
    ggplot() +
    aes(week, hour, fill = log(Freq)) +
    geom_tile() +
    scale_fill_viridis_c(na.value = "black") +
    labs(
    x = "Week",
    y = "Hour",
    title = "Most active listening times (note log scale)"
    ) +
    theme_minimal() +
    theme(axis.text.x = element_text(angle=90))