Created
March 28, 2018 18:36
-
-
Save MattSandy/5c7fd2a7b4a6aba16ee717b26457c5c4 to your computer and use it in GitHub Desktop.
Get similar artists from last.fm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("igraph") | |
library("networkD3") | |
library("data.table") | |
library("jsonlite") | |
library("data.table") | |
library("htmlwidgets") | |
AS.API <- "YOUR_API_KEY" | |
AS.URL <- paste0("http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&api_key=",AS_API,"&format=json&artist=") | |
AS.ARTISTS <- list() | |
get_similar <- function(artist,degree) { | |
print(paste(artist,degree)) | |
loc <- paste0(AS.URL, URLencode(artist)) | |
if(degree<3 & (!artist %in% names(AS.ARTISTS))) { | |
similar <- jsonlite::fromJSON(url(loc))$similarartists$artist | |
#check if properties on json exist | |
if(!is.null(similar)) { | |
#check the length of similar artists | |
if(length(similar)>0) { | |
similar$artist <- artist | |
similar <- similar[,c("artist","name","match")] | |
names(similar) <- c("Artist","Related","Match") | |
#keep matches somewhat relevant | |
similar <- similar[which(similar$Match>.2),] | |
#add to global list | |
AS.ARTISTS[[artist]] <<- similar | |
result <- sapply(similar$Related,get_similar,degree+1) | |
} | |
} | |
} | |
} | |
#set starting artist | |
artist <- "ajj" | |
get_similar(artist,0) | |
#bind list | |
df <- rbindlist(AS.ARTISTS) | |
#tmp for creating nodes | |
tmp <- df[,c("Related","Match")] | |
names(tmp) <- c("Artist","Match") | |
nodes <- rbind(df[,c("Artist","Match")],tmp) | |
#nodes made, unset tmp | |
rm(tmp) | |
#aggregate for unique nodes | |
nodes <- aggregate(as.numeric(as.character(nodes$Match)),by = list(nodes$Artist),FUN = sum) | |
names(nodes) <- c("id","size") | |
#ignore Ghost Mice | |
ignored <- "Ghost Mice" | |
df <- df[which(!df$Artist %in% ignored),] | |
df <- df[which(!df$Related %in% ignored),] | |
nodes <- nodes[which(!nodes$id %in% ignored),] | |
edge <- df | |
nodes <- nodes | |
#set max size if any artists skew sizing | |
nodes$size <- sapply(nodes$size,function(size) { | |
if(size>15) { | |
return(15) | |
} else { | |
return(size) | |
} | |
}) | |
names(edge) <- c("Source","Target","Color") | |
edge$Source <- as.character(edge$Source) | |
edge$Target <- as.character(edge$Target) | |
nodes$id <- as.character(nodes$id) | |
nodes <- nodes[!duplicated(nodes$id),] | |
#set up igraph data to convert to networkd3 | |
net <- graph_from_data_frame(d=edge, vertices=nodes, directed=T) | |
sj_list <- igraph_to_networkD3(net, group = round(nodes$size)) | |
#the feeling lucky URL for google | |
clicked <- 'window.open("https://www.google.com/search?btnI=1&q=bandcamp%20" + d.name, "_blank");' | |
p <- forceNetwork(Links = sj_list$links, Nodes = sj_list$nodes, Source = 'source', | |
Target = 'target', NodeID = 'name', Group = 'group', | |
zoom = TRUE, opacityNoHover = .1, fontSize = 14, opacity = 1,clickAction = clicked, | |
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory10);")) | |
saveWidget(p, file=paste0(artist,".html")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment