Skip to content

Instantly share code, notes, and snippets.

@ccagrawal
Last active August 29, 2015 13:59
Show Gist options
  • Save ccagrawal/10443514 to your computer and use it in GitHub Desktop.
Save ccagrawal/10443514 to your computer and use it in GitHub Desktop.
Youtube_IMDb_Rater
library(rjson)
library(reshape)
user <- 'TheLaughFactory'
videos <- data.frame(matrix(data = 0, nrow = 1000, ncol = 8))
colnames(videos) <- c('id', 'link', 'title', 'date', 'duration', 'view.count', 'avg.rating', 'num.raters')
found.all <- FALSE
index <- 1
while (found.all == FALSE) {
url = paste('http://gdata.youtube.com/feeds/api/users/', user, '/uploads?start-index=', index, '&max-results=50&alt=json', sep = '')
json <- fromJSON(file=url)$feed$entry
for (v in 1:length(json)) {
temp <- json[v][[1]]
videos[index + v - 1, 'id'] <- gsub('http://gdata.youtube.com/feeds/api/videos/', '', temp$id$'$t')
videos[index + v - 1, 'link'] <- temp$link[[1]]$href
videos[index + v - 1, 'title'] <- temp$title$'$t'
videos[index + v - 1, 'date'] <- as.character(as.Date(temp$published$'$t'))
videos[index + v - 1, 'duration'] <- as.numeric(temp$'media$group'$`yt$duration`$seconds) / 60
videos[index + v - 1, 'view.count'] <- as.numeric(nulldefault(temp$`yt$statistics`$viewCount, NA))
videos[index + v - 1, 'avg.rating'] <- nulldefault(temp$`gd$rating`$average, NA)
videos[index + v - 1, 'num.raters'] <- nulldefault(temp$`gd$rating`$numRaters, NA)
cat(index + v - 1, '\n')
}
index <- index + 50
if (length(json) < 50) {
found.all <- TRUE
}
}
videos <- videos[which(videos$id != 0), ]
videos <- videos[complete.cases(videos), ]
videos$total.rating <- videos$num.raters * videos$avg.rating
min.votes <- median(videos$num.raters)
top.videos <- videos[which(videos$num.raters > min.votes), ]
mean.rating <- sum(top.videos$total.rating) / sum(top.videos$num.raters)
videos$weighted.rating <- (videos$total.rating + mean.rating * min.votes) / (videos$num.raters + min.votes)
videos$total.rating <- NULL
videos <- videos[order(videos$weighted.rating, decreasing = TRUE), ]
write.csv(videos, paste(user, '.csv', sep = ''), row.names = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment