Poniżej konfiguracja skryptu

lastkey <- "xxxxxxxxxxxxxxxxxxxxxxxxxx" # get your key from http://www.last.fm/api/account/create
user_name <- "LeMUr1978KCrq"
ile_polecanych <- 10
ile_scrobli <- 1000

Potrzebny jest pakiet, który został wycofany z CRANa, ale dostępny jest w archiwum. Trzeba zainstalować ręcznie paczkę pobraną z https://cran.r-project.org/src/contrib/Archive/RLastFM/RLastFM_0.1-5.tar.gz

## Warning: package 'XML' was built under R version 3.2.5
## Warning: package 'RCurl' was built under R version 3.2.3
## Warning: package 'bitops' was built under R version 3.2.3
## Warning: package 'dplyr' was built under R version 3.2.5
## Warning: package 'ggplot2' was built under R version 3.2.5
## Warning: package 'reshape2' was built under R version 3.2.5

Załadowanie potrzebnych bibliotek

library(RLastFM)
library(dplyr)
library(ggplot2)
library(reshape2)

Pobranie najnowszych danych z Last.FM

ile_scrobli <- ifelse(ile_scrobli>=1000, 1000, ile_scrobli)
recent <- as.data.frame(user.getRecentTracks(user_name, limit = ile_scrobli))
lastfm <- select(recent, Artist=artist, Album=album, Song=track, DateOrg=date)

Uporządkowanie danych…

lastfm$Artist <- as.character(lastfm$Artist)
lastfm$Album <- as.character(lastfm$Album)
lastfm$Song <- as.character(lastfm$Song)

…potrzebni nam są tylko artyści

top_artists <- data.frame(Artist=unique(lastfm$Artist), stringsAsFactors = FALSE)

Szukamy podobnych artysów (wg LastFm)

GetSimilar <- function(x) {
   sims <- rep(NA, 5)
   try(sims <- artist.getInfo(x)$similar)
   sims <- as.character(sims)
   return(data.frame(Artist=x, sim1=sims[1], sim2=sims[2], sim3=sims[3], sim4=sims[4], sim5=sims[5]))
}

sim_df <- data.frame()
for(i in 1:nrow(top_artists)) {
   sim_df <- rbind(sim_df, GetSimilar(top_artists[i,1]))
}
sim_df$Artist <- as.character(sim_df$Artist)
sim_df$sim1 <- as.character(sim_df$sim1)
sim_df$sim2 <- as.character(sim_df$sim2)
sim_df$sim3 <- as.character(sim_df$sim3)
sim_df$sim4 <- as.character(sim_df$sim4)
sim_df$sim5 <- as.character(sim_df$sim5)

t <- melt(sim_df, id.vars = 1, measure.vars = 2:6) %>% filter(!is.na(value))

Czy podobny jest w scroblowanych, czyli czy go słuchałem? Jeśli tak - nie ma sensu go polecać.
polecany == FALSE - oznacza, że nie było scrobla tego artysty, bierzemy tylko nieznanych.

t$polecany <- t$value %in% top_artists$Artist

Kto jest najczęściej polecany? Liczymy i rysujemy wykres.

polecani <- t %>%
   filter(polecany==FALSE) %>%
   group_by(value) %>%
   mutate(n=n()) %>%
   ungroup() %>%
   arrange(desc(n)) %>%
   select(Artist, Polecany=value, n)
   
polecani_plot <- polecani %>%
   top_n(ile_polecanych, n) %>%
   ggplot() +
   geom_bar(aes(Polecany, n, fill=Polecany), stat="identity") +
   theme_minimal() +
   theme(axis.text.x=element_text(angle=90, hjust=1), legend.position = "none") + coord_flip()

polecani_plot

A teraz to samo w tabeli - kto jest polecany i na podstawie jakich przesłuchanych artystów?

polecani_lista <- polecani %>%
   top_n(ile_polecanych, n) %>%
   arrange(desc(n), Polecany) %>%
   select(Polecany, Bo_sluchany=Artist)
polecani_lista
Polecany Bo_sluchany
Kazik Kult
Kazik Kazik na Żywo
Kazik Świetliki i Linda
Kazik Lao Che
Alice in Chains Soundgarden
Alice in Chains Nirvana
Alice in Chains Pearl Jam
Bob Dylan Leonard Cohen
Bob Dylan Bruce Springsteen
Bob Dylan Van Morrison
Charles Mingus Miles Davis
Charles Mingus John Coltrane
Charles Mingus Thelonious Monk
Fake Dead Pare’s Band
Fake Dead Stephunk T. & Tonya Take
Fake Dead The Tchendos meets Gringo
Homo Twist Apteka
Homo Twist Świetliki
Homo Twist Maciej Maleńczuk
Illusion Acid Drinkers
Illusion FlapJack
Illusion Sweet Noise
Led Zeppelin Pink Floyd
Led Zeppelin The Jimi Hendrix Experience
Led Zeppelin The Doors
Megadeth Metallica
Megadeth Pantera
Megadeth Iron Maiden
Roe-Deer Colorfactory
Roe-Deer Here
Roe-Deer The Loners
Turbo Kat & Roman Kostrzewski
Turbo Kat
Turbo Acid Drinkers