function get_stats() { var results; var functions; var loading_icon = document.getElementById("loading_icon"); var p_identity = document.getElementById("p_identity").value; //var p_identity = p_identity.replace(/[&\/\\#,+()$~%:*?<>{}]/g, ''); stats_form = { "p_identity" : p_identity.trim() }; var stats_data = JSON.stringify(stats_form); var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { console.log(this.responseText); var result = JSON.parse(this.responseText); if(result.error) { loading_icon.style.display = "none"; search_button("SEARCH"); document.getElementById('results_error').innerHTML = '

' + result.message + '

'; } else { load_page(this.responseText); } } }; xhttp.withCredentials = true; xhttp.open("post", "api/get_stats.php"); xhttp.send(stats_data); loading_icon.style.display = "inline"; } function load_page(data){ results = JSON.parse(data); if(results.error) { $('#results_error').html(results.message); loading_icon.style.display = "none"; search_button("SEARCH"); return } var search_box = document.getElementById("search_input"); var login_content = document.getElementById("login_content"); var footer = document.getElementById("footer"); search_box.style.display = "none"; login_content.style.display = "none"; footer.style.display = "none"; load_introduction(); if(!results.user.user_movies.error && functions.get_user_movie_stats) { load_movies(); } if(!results.user.user_shows.error && functions.get_user_show_stats) { load_shows(); } if(!results.user.user_music.error && functions.get_user_music_stats) { load_music(); } if(!results.year_stats.error && (functions.get_year_stats_movies || functions.get_year_stats_shows || functions.get_year_stats_music)) { load_users(); } load_outro(); } //INTRODUCTION function load_introduction() { var text = ""; text += "
"; text += "
"; text += "
"; text += ''; text += "
"; text += "
"; text += "
"; text += "

Hey there, " + results.user.name + "!

"; text += "



"; text += "

New year, new page of statistics...

"; text += "
"; text += "
"; text += "
"; document.getElementById("search_results").innerHTML += text; } //MOVIES function load_movies() { var text = ""; if(results.user.user_movies.data.movies.length > 1) { text += "
"; text += "
"; text += "

Movies!

"; text += "


"; text += "

You watched " + results.user.user_movies.data.movies.length + " movies. That's a lot of movies!

(or not, I am pre-programmed to say that)

" text += "

"; text += "
"; text += "
"; text += "
"; text += top_list(results.user.user_movies.data.movies, "Your top movies", false, true); text += "
"; text += "
"; text += "
"; text += completion_movie(results.user.user_movies.data.user_movie_finishing_percent, false); text += "
"; text += "
"; text += paused_movie(results.user.user_movies.data.user_movie_most_paused, false); text += "
"; text += "
"; text += oldest_movie(results.user.user_movies.data.user_movie_oldest); text += "
"; var sum = 0; for(i = 0; (i < results.user.user_movies.data.movies.length); i++) { sum += results.user.user_movies.data.movies[i].duration; } text += "
"; text += you_spent(sum, 'movies', 'watching'); text += "
"; text += "
"; text += "
"; text += "
"; } else if(results.user.user_movies.data.movies.length == 1) { text += "
"; text += "
"; text += "

Movies!

"; text += "


"; text += "

You watched " + results.user.user_movies.data.movies.length + " movie. You know what you like!

(at least you tried it out)

"; text += "

"; text += "
"; text += "
"; text += "
"; text += top_list(results.user.user_movies.data.movies, "Your movie", false, true); text += "
"; text += "
"; text += "
"; text += completion_movie(results.user.user_movies.data.user_movie_finishing_percent, true); text += "
"; text += "
"; text += paused_movie(results.user.user_movies.data.user_movie_most_paused, true); text += "
"; text += "
"; text += "
"; text += "
"; } else { text += "
"; text += "
"; text += "
"; text += "

Movies!

"; text += "


"; text += "

You watched " + results.user.user_movies.data.movies.length + " movies. That's impressive in itself!

(might wanna try it)

" text += ''; text += "
"; text += "
"; text += "
"; } document.getElementById("search_results").innerHTML += text; } //SHOWS function load_shows() { var text = ""; if(results.user.user_shows.data.shows.length > 1) { text += "
"; text += "
"; text += "

Shows!

"; text += "


You watched " + results.user.user_shows.data.shows.length + " different shows.

(No, watching The Office twice in a year doesn't count as two shows)

" text += "
"; text += "
"; text += "
"; text += top_list(results.user.user_shows.data.shows, "Your top shows", false, true); text += "
"; text += "
"; if(results.user.user_shows.data.shows.length > 0 && !results.user.user_shows.data.show_buddy.error && functions.get_user_show_buddy) { text += "
"; text += load_showbuddy(); text += "
"; } var sum = 0; for(i = 0; (i < results.user.user_shows.data.shows.length); i++) { sum += results.user.user_shows.data.shows[i].duration; } text += "
"; text += you_spent(sum, 'shows', 'watching'); text += "
"; text += "
"; text += "
"; text += "
"; } else if(results.user.user_shows.data.shows.length == 1) { text += "
"; text += "
"; text += "

Shows!

"; text += "


You watched " + results.user.user_shows.data.shows.length + " show.

(Better not be that same one again...)

" text += "
"; text += "
"; text += "
"; text += top_list(results.user.user_shows.data.shows, "Your show", false, true); text += "
"; if(results.user.user_shows.data.shows.length > 0 && !results.user.user_shows.data.show_buddy.error && functions.get_user_show_buddy) { text += "
"; text += load_showbuddy(); text += "
"; } text += "
"; text += "
"; } else { text += "
"; text += "
"; text += "
"; text += "

Shows!

"; text += "


"; text += "

You watched " + results.user.user_shows.data.shows.length + " shows. I get it, it's not for everyone!

(might wanna try it)

" text += ''; text += "
"; text += "
"; text += "
"; } document.getElementById("search_results").innerHTML += text; } //MUSIC function load_music() { var text = ""; var albums = []; var artists = []; for(var i = 0; i < results.user.user_music.data.music.length; i++) { var found = false; for(var j = 0; j < albums.length; j++) { if(albums[j].title == results.user.user_music.data.music[i].parent_title && albums[j].grandparent_title == results.user.user_music.data.music[i].grandparent_title) { albums[j]["plays"] = albums[j].plays + 1; albums[j]["duration"] = results.user.user_music.data.music[i].duration + albums[j].duration; found = true; break; } } if(!found && results.user.user_music.data.music[i].parent_title != "" && results.user.user_music.data.music[i].grandparent_title != "") { albums.push({"title" : results.user.user_music.data.music[i].parent_title, "parent_rating_key" : results.user.user_music.data.music[i].parent_rating_key,"grandparent_title" : results.user.user_music.data.music[i].grandparent_title, "plays" : 1, "duration" : results.user.user_music.data.music[i].duration}); } } albums.sort(function(a, b) { return parseFloat(b.duration) - parseFloat(a.duration); }); for(var i = 0; i < results.user.user_music.data.music.length; i++) { var found = false; for(var j = 0; j < artists.length; j++) { if(artists[j].title == results.user.user_music.data.music[i].grandparent_title) { artists[j]["plays"] = artists[j].plays + 1; artists[j]["duration"] = results.user.user_music.data.music[i].duration + artists[j].duration; found = true; break; } } if(!found && results.user.user_music.data.music[i].grandparent_title != "") { artists.push({"title" : results.user.user_music.data.music[i].grandparent_title, "grandparent_rating_key" : results.user.user_music.data.music[i].grandparent_rating_key, "plays" : 1, "duration" : results.user.user_music.data.music[i].duration}); } } artists.sort(function(a, b) { return parseFloat(b.duration) - parseFloat(a.duration); }); if(results.user.user_music.data.music.length > 1) { text += "
"; text += "
"; text += "

Music!

"; text += "


You listened to " + results.user.user_music.data.music.length + ' different tracks.

(If you can call your taste "music"...)

' text += "
"; text += "
"; text += "
"; text += top_list(results.user.user_music.data.music, "Your top tracks", true, false); text += "
"; text += "
"; text += top_list(albums, "Your top albums", true, false); text += "
"; text += "
"; text += top_list(artists, "Your top artists", false, false); text += "
"; var sum = 0; for(i = 0; (i < results.user.user_music.data.music.length); i++) { sum += results.user.user_music.data.music[i].duration; } text += "
"; text += you_spent(sum, 'music', 'listening'); text += "
"; text += "
"; text += "
"; } else if(results.user.user_shows.data.shows.length == 1) { text += "
"; text += "
"; text += "

Music!

"; text += "


You listened to " + results.user.user_music.data.music.length + " track.

(Whatever floats your boat...)

" text += "
"; text += "
"; text += "
"; text += top_list(results.user.user_music.data.music, "Your track", true, false); text += "
"; text += "
"; text += "
"; } else { text += "
"; text += "
"; text += "
"; text += "

Shows!

"; text += "


"; text += "

You listened to " + results.user.user_music.data.music.length + " tracks. I get it, it's not for everyone!

(might wanna try it)

" text += ''; text += "
"; text += "
"; text += "
"; } document.getElementById("search_results").innerHTML += text; } function oldest_movie(array) { var html = ""; html += "
"; html += "
"; html += "The oldest movie you watched was
" + array.title + " (" + array.year + ")
"; if(array.year < 1950) { html += "
I didn't even know they made movies back then."; html += '

'; } else if(array.year < 1975) { html += "
Did it even have color?"; html += '

'; } else if(array.year < 2000) { html += "
Was it a 4K, UHD, 3D, Dolby Atmos remaster?"; html += '

'; } else { html += "
Enjoying the classics, huh?"; html += '

'; } html += "
"; html += "
"; return html; } function completion_movie(user_movie_finishing_percent, single) { var html = ""; html += "
"; html += "
"; var str = JSON.stringify(user_movie_finishing_percent); var percent = str.split('.'); if(!single) { html += "Your average movie finishing percentage was " + percent[0] + "%"; } else { html += "Your saw " + percent[0] + "%"; } if(percent[0] > 89) { html += '

'; } html += "

You're not watching the credits like a nerd, are you?"; html += "
"; html += "
"; return html; } function paused_movie(array, single) { var html = ""; html += "
"; if(array.paused_counter > 0) { var pause_time = seconds_to_time(array.paused_counter, false); if(!single) { html += "
"; html += "Your longest movie pause was watching
" + array.title + " (" + array.year + ")"; html += "

It was paused for " + pause_time + "..."; html += "
"; } else { html += "
"; html += "One movie, but you still paused it
"; html += "

It was paused for " + pause_time + "..."; html += "
"; } } else { html += "
"; html += "Bladder of steel"; html += '

'; html += "
You never paused a single movie."; html += "
"; } html += "
"; return html; } function load_showbuddy() { var html = ""; html += "
"; html += "
"; html += "Your top show was " + results.user.user_shows.data.shows[0].title + "
"; if(!results.user.user_shows.data.show_buddy.error) { if(!results.user.user_shows.data.show_buddy.user.found) { html += '
'; html += "
That means you dared to explore where no one else would, because you are the only viewer of that show"; } else { html += "And you're not alone! Your " + results.user.user_shows.data.shows[0].title + "-buddy is "; html += "" + results.user.user_shows.data.show_buddy.user.user + "!

"; var combined = results.user.user_shows.data.show_buddy.user.duration + parseInt(results.user.user_shows.data.shows[0].duration); var combined_2 = seconds_to_time(combined); html += ''; html += "
Your combined efforts resulted in " + combined_2 + " of " + results.user.user_shows.data.shows[0].title + "!"; } } html += "
"; html += "
"; return html; } function you_spent(time, category, verb) { var html = ""; var time = seconds_to_time(time, false); html += "
"; html += "
"; html += "You spent " + time + ""; html += " " + verb + " "; html += category; if(category == 'music') { html += '
'; } else { html += '
'; } html += "
"; html += "
"; return html; } function top_list(array, title, music, year) { var html = ""; html += "
"; html += "
"; html += "
" + title + "
"; html += "
"; for(i = 0; (i < array.length && i < 10); i++) { html += "
"; html += "
"; html += i+1 + ". "; html += "
"; html += "
"; if(music) { html+= array[i].grandparent_title + "
"; } html += ""; html += array[i].title; html += ""; var movie_hour = seconds_to_time(array[i].duration, true); if(typeof(array[i].year) != "undefined" && year) { html += " (" + array[i].year + ")"; } html += "
" + movie_hour + "
" + play_plays(array[i].plays); html += "
"; html += "
"; } html += "
"; html += "
"; html += "
"; return html; } function top_list_names(array, title) { var html = ""; html += "
"; html += "
"; html += "
" + title + "
"; html += "
"; for(i = 0; i < 10 && i < array.length; i++) { if(i == 0) { html += "
"; } else if(i == 1) { html += "
"; } else if(i == 2) { html += "
"; } else { html += "
"; } html += "
"; html += i+1 + ". "; html += "
"; if(array[i].user == results.user.name) { html += "
"; html += array[i].user; html += "
"; }else { html += "
"; html += array[i].user; html += "
"; } html += "
"; } html += "
"; html += "
"; html += "
"; return html; } //TOP USERS function load_users() { var text = ""; text += "
"; text += "

Server-wide statistics!

"; text += "



It's okay to feel shame if you are on the list.

(or missing from it...)

" text += "

"; text += "
"; if(functions.get_year_stats_leaderboard) { text += "
"; text += top_list_names(results.year_stats.year_users.data, 'Top users'); text += "
"; var sum_movies = 0; var sum_shows = 0; var sum_artists = 0; if(functions.get_year_stats_movies) { for(i = 0; (i < results.year_stats.year_movies.data.length); i++) { sum_movies += results.year_stats.year_movies.data[i].duration; } } if(functions.get_year_stats_shows) { for(i = 0; (i < results.year_stats.year_shows.data.length); i++) { sum_shows += results.year_stats.year_shows.data[i].duration; } } if(functions.get_year_stats_music) { for(i = 0; (i < results.year_stats.year_music.data.length); i++) { sum_artists += results.year_stats.year_music.data[i].duration; } } var time_movies = seconds_to_time(sum_movies, false); var time_shows = seconds_to_time(sum_shows, false); var time_artists = seconds_to_time(sum_artists, false); var time_all = seconds_to_time(Math.floor(sum_movies + sum_shows + sum_artists), false); text += "
"; text += "
"; text += "
"; if(functions.get_year_stats_movies) { text += "All the different users combined spent " + time_movies + ""; text += " watching movies."; text += "

"; } if(functions.get_year_stats_shows) { text += "All the different users combined spent " + time_shows + ""; text += " watching shows."; text += "

"; } if(functions.get_year_stats_music) { text += "All the different users combined spent " + time_artists + ""; text += " listening to artists."; text += "

"; } if(functions.get_year_stats_movies && (functions.get_year_stats_shows || functions.get_year_stats_music) || (functions.get_year_stats_shows && functions.get_year_stats_music)) { text += "That is " + time_all + "
of content!"; } text += ''; text += "
"; text += "
"; text += "
"; } text += "
"; text += "
"; if(functions.get_year_stats_movies) { text += "
"; text += top_list(results.year_stats.year_movies.data, "Top movies", false, true); text += "
"; } if(functions.get_year_stats_shows) { text += "
"; text += top_list(results.year_stats.year_shows.data, "Top shows", false, false); text += "
"; } if(functions.get_year_stats_music) { var artists = []; for(var i = 0; i < results.year_stats.year_music.data.length; i++) { var found = false; for(var j = 0; j < artists.length; j++) { if(artists[j].title == results.year_stats.year_music.data[i].grandparent_title) { artists[j]["plays"] = artists[j].plays + 1; artists[j]["duration"] = results.year_stats.year_music.data[i].duration + artists[j].duration; found = true; break; } } if(!found && results.year_stats.year_music.data[i].grandparent_title != "") { artists.push({"title" : results.year_stats.year_music.data[i].grandparent_title, "grandparent_rating_key" : results.year_stats.year_music.data[i].grandparent_rating_key, "plays" : 1, "duration" : results.year_stats.year_music.data[i].duration}); } } artists.sort(function(a, b) { return parseFloat(b.duration) - parseFloat(a.duration); }); text += "
"; text += top_list(artists, "Top artists", false, false); text += "
"; } text += "
"; text += "
"; document.getElementById("search_results").innerHTML += text; } //Outro function load_outro() { var text = ""; text += "
"; text += "
"; text += "
"; text += ''; text += "
"; text += "
"; text += "

Hope you are staying safe!



Goodybye.

"; text += "
"; text += "
"; text += "
"; document.getElementById("search_results").innerHTML += text; } function play_plays(plays) { plays = parseInt(plays); if(plays == 1) { var play_string = plays + ' play'; } else { var play_string = plays + ' plays'; } return play_string; } //Converting seconds to time in string var seconds_in_day = 86400; var seconds_in_hour = 3600; var seconds_in_minute = 60; function seconds_to_time(seconds, comma) { if(seconds >= seconds_in_day) { var time = seconds_to_days(seconds, comma); } else if(seconds >= seconds_in_hour) { var time = seconds_to_hours(seconds, comma); } else if(seconds >= seconds_in_minute) { var time = seconds_to_minutes(seconds, comma); } else { var time = seconds_to_seconds(seconds); } return time; } function seconds_to_days(seconds, comma) { var day = Math.floor(seconds / seconds_in_day); var rest = Math.floor(seconds % seconds_in_day); var hour = Math.floor(rest / seconds_in_hour); rest = Math.floor(rest % seconds_in_hour); var minute = Math.floor(rest / seconds_in_minute); rest = Math.floor(rest % seconds_in_minute); var day_string = ''; var hour_string = ''; var minute_string = ''; if(day < 2) { day_string += day + ' day'; } else { day_string += day + ' days'; } if(hour < 2) { hour_string += hour + ' hour'; } else { hour_string += hour + ' hours'; } if(minute < 2) { minute_string += minute + ' minute'; } else { minute_string += minute + ' minutes'; } if(!hour == 0) { if(!minute == 0) { if(comma) { return day_string + ', ' + hour_string + ', ' + minute_string; } else { return day_string + ', ' + hour_string + ' and ' + minute_string; } } else { if(comma) { return day_string + ', ' + hour_string; } else { return day_string + ' and ' + hour_string; } } } else { return day_string; } } function seconds_to_hours(seconds, comma) { var hour = Math.floor(seconds / seconds_in_hour); var rest = Math.floor(seconds % seconds_in_hour); var minute = Math.floor(rest / seconds_in_minute); rest = Math.floor(rest % seconds_in_minute); var hour_string = ''; var minute_string = ''; if(hour < 2) { hour_string += hour + ' hour'; } else { hour_string += hour + ' hours'; } if(minute < 2) { minute_string += minute + ' minute'; } else { minute_string += minute + ' minutes'; } if(!minute == 0) { if(comma) { return hour_string + ', ' + minute_string; } else { return hour_string + ' and ' + minute_string; } } else { return hour_string; } } function seconds_to_minutes(seconds, comma) { seconds = parseInt(seconds); var minute = Math.floor(seconds / seconds_in_minute); var rest = Math.floor(seconds % seconds_in_minute); var minute_string = ''; var second_string = ''; if(minute < 2) { minute_string += minute + ' minute'; } else { minute_string += minute + ' minutes'; } if(seconds < 2) { second_string += rest + ' second'; } else { second_string += rest + ' seconds'; } if(!seconds == 0) { if(comma) { return minute_string + ', ' + second_string; } else { return minute_string + ' and ' + second_string; } } else { return minute_string; } } function seconds_to_seconds(seconds) { var second_string = ''; if(seconds == 1) { second_string += seconds + ' second'; } else { second_string += seconds + ' seconds'; } return second_string; }