r 您可以从last.fm的相似艺术家

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r 您可以从last.fm的相似艺术家相关的知识,希望对你有一定的参考价值。

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"))

以上是关于r 您可以从last.fm的相似艺术家的主要内容,如果未能解决你的问题,请参考以下文章

从数据集输出艺术家

从数据集输出艺术家

在 Python 中读取大量 json 文件?

从带有last.fm乱码的mysql表中查找给定年份的前100名[重复]

Last.fm java.lang.ClassCastException - 从文件中读取用户对象

从 Last.fm API 到 Python 中的 UTS 到 DateTime [重复]