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) { 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.year_stats.error && functions.get_year_stats) { 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"); 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'); 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"); 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 out)

" 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"); 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'); 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"); 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 out)

" 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?"; } else { html += "
Enjoying the classics, huh?"; } 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 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're 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) { var html = ""; var time = seconds_to_time(time, false); html += "
"; html += "
"; html += "You spent " + time + ""; html += " watching " + category; html += '
'; html += "
"; html += "
"; return html; } function top_list(array, title) { var html = ""; html += "
"; html += "
"; html += "
" + title + "
"; html += "
"; for(i = 0; (i < array.length && i < 10); i++) { html += "
"; html += "
"; html += i+1 + ". "; html += "
"; html += "
"; html += array[i].title; html += ""; var movie_hour = seconds_to_time(array[i].duration, true); if(typeof(array[i].year) != "undefined") { 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 += "
"; text += "
"; text += top_list_names(results.year_stats.data.users, 'Top users'); text += "
"; var sum_movies = 0; for(i = 0; (i < results.year_stats.data.top_movies.length); i++) { sum_movies += results.year_stats.data.top_movies[i].duration; } var sum_shows = 0; for(i = 0; (i < results.year_stats.data.top_shows.length); i++) { sum_shows += results.year_stats.data.top_shows[i].duration; } var time_movies = seconds_to_time(sum_movies, false); var time_shows = seconds_to_time(sum_shows, false); var time_all = seconds_to_time(Math.floor(sum_movies + sum_shows), false); text += "
"; text += "
"; text += "
"; text += "All the different users combined spent " + time_movies + ""; text += " watching movies."; text += "

And, the users spent " + time_shows + ""; text += " watching shows."; text += "

That is " + time_all + "
of content!"; text += ''; text += "
"; text += "
"; text += "
"; text += "
"; text += "
"; text += "
"; text += top_list(results.year_stats.data.top_movies, "Top movies"); text += "
"; text += "
"; text += top_list(results.year_stats.data.top_shows, "Top shows"); 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; }