Created
May 23, 2017 08:47
-
-
Save tilmanschweitzer/5abb6457275a84ecd525e22970db5f66 to your computer and use it in GitHub Desktop.
Shows diagram for a movie in a movie pilot page.
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
var moviePilotRatingAnalyser = { | |
movieId: function () { | |
return jQuery("[data-type='Movie']").data('id'); | |
}, | |
ratings: function () { | |
return this.ratingData.map(function (rating) { | |
return rating.value || 0; | |
}); | |
}, | |
average: function () { | |
var ratings = this.ratings(); | |
return ratings.reduce(function (a, b) { | |
return a + b; | |
}) / ratings.length / 10; | |
}, | |
median: function () { | |
var ratings = this.ratings(); | |
ratings.sort(); | |
return ratings[Math.round(ratings.length / 2)] / 10; | |
}, | |
countRatings: function () { | |
var countedRatings = { | |
max: 0 | |
}; | |
this.ratings().forEach(function (rating) { | |
if (!countedRatings[rating]) { | |
countedRatings[rating] = 0; | |
} | |
countedRatings[rating] += 1; | |
countedRatings.max = Math.max(countedRatings.max, countedRatings[rating]) | |
}); | |
return countedRatings; | |
}, | |
printReport: function () { | |
var ratingReportLines = ['', '', '###########', 'RATING REPORT', '##########', '']; | |
ratingReportLines.push('Total Ratings: '+ this.ratings().length); | |
ratingReportLines.push('Average Rating: '+ Math.round(this.average() * 100)/100); | |
ratingReportLines.push('Median Rating: ' + this.median()); | |
var countedRatings = this.countRatings(); | |
_.range(0,101,5).forEach(function (rating) { | |
var infoString = (rating / 10) + ""; | |
if (rating % 10 === 0) { | |
infoString += '.0' | |
} | |
infoString += " " | |
if (rating !== 100) { | |
infoString = " " + infoString; | |
} | |
var i; | |
var ratingCount = countedRatings[rating] || 0 | |
var spacePadding = ' '.slice(Math.floor(Math.log10(ratingCount)), Math.floor(Math.log10(countedRatings.max))); | |
infoString += spacePadding + ' (' + ratingCount + ') '; | |
for (i = 0; i < ratingCount; i++) { | |
infoString += "#" | |
} | |
ratingReportLines.push(infoString); | |
}); | |
console.log(ratingReportLines.join('\n')); | |
}, | |
loadRatings: function () { | |
var self = this; | |
self.ratingData = []; | |
var url = 'https://www.moviepilot.de/api/movies/MOVIE_ID/comments?page=PAGE&per_page=PER_PAGE'; | |
var movieUrl = url.replace('MOVIE_ID', this.movieId()); | |
loadRatingChunk(1, 200); | |
function loadRatingChunk(page, perPage) { | |
console.log('load page ' + page); | |
var nextUrl = movieUrl.replace('PAGE', page).replace('PER_PAGE', perPage); | |
jQuery.getJSON(nextUrl, function (data) { | |
self.ratingData = self.ratingData.concat(data.results); | |
console.log(self.ratingData.length); | |
if (data.hasMorePages) { | |
loadRatingChunk(page + 1, perPage); | |
} else { | |
if (data.totalCommentsCount !== self.ratingData.length) { | |
throw new Error('Expected ' + data.totalCommentsCount + ' ratings, but only received ' + self.ratingData.length); | |
} | |
console.log('Found ' + self.ratingData.length + ' elements'); | |
self.printReport(); | |
} | |
}); | |
} | |
} | |
} | |
moviePilotRatingAnalyser.loadRatings(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment