Streamlined JSON response & privacy

All data calculations moved to PHP side
Response arrays limited to needed data (10)
Fixed bug where users artist and album plays just counted unique track plays
JSON response more streamlined with consistent build
This commit is contained in:
aunefyren 2021-10-24 18:08:22 +02:00
parent f060ea1ab0
commit c9f6d5764a
3 changed files with 207 additions and 272 deletions

View file

@ -37,7 +37,7 @@ if(!empty($data)){
exit(0);
}
if(!empty($data)){
if(!empty($data) && !empty($data->caching)) {
$caching = filter_var(htmlspecialchars(trim($data->caching)), FILTER_VALIDATE_BOOLEAN);
} else if(isset($_GET["caching"])) {
$caching = filter_var(htmlspecialchars(trim($_GET["caching"])), FILTER_VALIDATE_BOOLEAN);
@ -573,7 +573,7 @@ function data_get_user_stats_loop($id, $array) {
}
for ($j = 0; $j < count($episodes); $j++) {
if(strtolower($episodes[$j]["grandparent_title"]) == strtolower($title) && strtolower($episodes[$j]["parent_title"]) == strtolower($season_title) && strtolower($episodes[$j]["title"]) == strtolower$episode_title)) {
if(strtolower($episodes[$j]["grandparent_title"]) == strtolower($title) && strtolower($episodes[$j]["parent_title"]) == strtolower($season_title) && strtolower($episodes[$j]["title"]) == strtolower($episode_title)) {
$episodes[$j]["duration"] = intval($episodes[$j]["duration"]) + intval($duration);
$episodes[$j]["plays"] = intval($episodes[$j]["plays"]) + 1;
$episode_found = True;
@ -604,9 +604,6 @@ function data_get_user_stats_loop($id, $array) {
$grandparent_title = $array[$i]["data"][$d]["grandparent_title"];
$duration = $array[$i]["data"][$d]["duration"];
$year = $array[$i]["data"][$d]["year"];
$rating_key = $array[$i]["data"][$d]["rating_key"];
$parent_rating_key = $array[$i]["data"][$d]["parent_rating_key"];
$grandparent_rating_key = $array[$i]["data"][$d]["grandparent_rating_key"];
// Skip entry if title, album or artist is empty/missing
if($title == "" || $grandparent_title == "" || $parent_title == "") {
@ -620,12 +617,12 @@ function data_get_user_stats_loop($id, $array) {
$tracks[$j]["plays"] = intval($tracks[$j]["plays"]) + 1;
$tracks[$j]["duration"] = intval($tracks[$j]["duration"]) + intval($duration);
$found = True;
break;
break;
}
}
if(!$found) {
array_push($tracks, array("title" => $title, "parent_title" => $parent_title, "grandparent_title" => $grandparent_title, "plays" => 1, "duration" => $duration, "year" => $year, "rating_key" => $rating_key, "parent_rating_key" => $parent_rating_key, "grandparent_rating_key" => $grandparent_rating_key));
array_push($tracks, array("title" => $title, "parent_title" => $parent_title, "grandparent_title" => $grandparent_title, "plays" => 1, "duration" => $duration, "year" => $year));
}
}
@ -642,11 +639,6 @@ function data_get_user_stats_loop($id, $array) {
$user = $array[$i]["data"][$d]["friendly_name"];
$user_id = $array[$i]["data"][$d]["user_id"];
$year = $array[$i]["data"][$d]["year"];
// Skip movie if year is empty/missing
if($year == "") {
continue;
}
$user_found = False;
$movie_found = False;
@ -740,9 +732,6 @@ function data_get_user_stats_loop($id, $array) {
$user_id = $array[$i]["data"][$d]["user_id"];
$friendly_name = $array[$i]["data"][$d]["friendly_name"];
$year = $array[$i]["data"][$d]["year"];
$rating_key = $array[$i]["data"][$d]["rating_key"];
$parent_rating_key = $array[$i]["data"][$d]["parent_rating_key"];
$grandparent_rating_key = $array[$i]["data"][$d]["grandparent_rating_key"];
// Skip entry if title, album or artist is empty/missing
if($title == "" || $grandparent_title == "" || $parent_title == "") {
@ -767,16 +756,16 @@ function data_get_user_stats_loop($id, $array) {
}
for ($j = 0; $j < count($year_music); $j++) {
if(strtolower($title) == strtolower($year_music[$j]["title"]) && strtolower($parent_title) == strtolower($year_music[$j]["parent_title"]) && strtolower($grandparent_title) == strtolower($year_music[$j]["grandparent_title"])) {
if(strtolower($grandparent_title) == strtolower($year_music[$j]["grandparent_title"])) {
$year_music[$j]["plays"] = intval($year_music[$j]["plays"]) + 1;
$year_music[$j]["duration"] = intval($year_music[$j]["duration"]) + $duration;
$artist_found = True;
$artist_found = True;
break;
}
}
if(!$artist_found) {
array_push($year_music, array("title" => $title, "parent_title" => $parent_title, "grandparent_title" => $grandparent_title, "plays" => 1, "duration" => $duration, "year" => $year, "rating_key" => $rating_key, "parent_rating_key" => $parent_rating_key, "grandparent_rating_key" => $grandparent_rating_key));
array_push($year_music, array("grandparent_title" => $grandparent_title, "plays" => 1, "duration" => $duration));
}
}
@ -792,45 +781,123 @@ function data_get_user_stats_loop($id, $array) {
$movie_most_paused = array("title" => "No movies watched", "year" => 0, "plays" => 0, "duration" => 0, "paused_counter" => 0);
}
// Sort $movies for oldest movie
// Sort $movies for oldest movie and choose the oldest, verifiable movie
$year = array_column($movies, 'year');
array_multisort($year, SORT_ASC, $movies);
if(count($movies) > 0) {
$movie_oldest = array("title" => $movies[0]["title"], "year" => $movies[0]["year"], "plays" => $movies[0]["plays"], "duration" => $movies[0]["duration"], "paused_counter" => $movies[0]["paused_counter"]);
$oldest_found = false;
for($i = 0; $i < count($movies); $i++) {
if($movies[$i]["year"] !== "" && $movies[$i]["year"] > 1000) {
$movie_oldest = array("title" => $movies[$i]["title"], "year" => $movies[$i]["year"], "plays" => $movies[$i]["plays"], "duration" => $movies[$i]["duration"], "paused_counter" => $movies[$i]["paused_counter"], "error" => false);
$oldest_found = true;
break;
}
}
if(!$oldest_found) {
$movie_oldest = array("title" => "No movies watched", "year" => 0, "plays" => 0, "duration" => 0, "paused_counter" => 0, "error" => true);
}
} else {
$movie_oldest = array("title" => "No movies watched", "year" => 0, "plays" => 0, "duration" => 0, "paused_counter" => 0);
$movie_oldest = array("title" => "No movies watched", "year" => 0, "plays" => 0, "duration" => 0, "paused_counter" => 0, "error" => true);
}
// Sort $movies by longest duration
$duration = array_column($movies, 'duration');
array_multisort($duration, SORT_DESC, $movies);
//Sort shows by duration
//Sort $shows by longest duration
$duration = array_column($shows, 'duration');
array_multisort($duration, SORT_DESC, $shows);
//Sort episodes by duration
//Sort episodes by duration and find longest episode duration
$duration = array_column($episodes, 'duration');
array_multisort($duration, SORT_DESC, $episodes);
if(count($episodes) > 0) {
$episode_duration_longest = $episodes[0];
$episode_duration_longest["error"] = false;
} else {
$episode_duration_longest = array("title" => "None", "parent_title" => "None", "grandparent_title" => "None", "duration" => 0, "plays" => 0);
$episode_duration_longest = array("title" => "None", "parent_title" => "None", "grandparent_title" => "None", "duration" => 0, "plays" => 0, "error" => true);
}
// Sort $tracks for longest pause
// Declare artist and album variables
$artists = array();
$albums = array();
// Look for artists in track-list
for($i = 0; $i < count($tracks); $i++) {
$artist_found = false;
for($j = 0; $j < count($artists); $j++) {
if(strtolower($tracks[$i]["grandparent_title"]) == strtolower($artists[$j]["grandparent_title"])) {
$artists[$j]["plays"] += $tracks[$i]["plays"];
$artists[$j]["duration"] += $tracks[$i]["duration"];
$artist_found = true;
break;
}
}
if(!$artist_found) {
array_push($artists, array("grandparent_title" => $tracks[$i]["grandparent_title"], "duration" => $tracks[$i]["duration"], "plays" => $tracks[$i]["plays"]));
}
}
// Look for albums in track-list
for($i = 0; $i < count($tracks); $i++) {
$album_found = false;
for($j = 0; $j < count($albums); $j++) {
if(strtolower($tracks[$i]["grandparent_title"]) == strtolower($albums[$j]["grandparent_title"]) && strtolower($tracks[$i]["parent_title"]) == strtolower($albums[$j]["parent_title"])) {
$albums[$j]["plays"] += $tracks[$i]["plays"];
$albums[$j]["duration"] += $tracks[$i]["duration"];
$album_found = true;
break;
}
}
if(!$album_found) {
array_push($albums, array("parent_title" => $tracks[$i]["parent_title"], "grandparent_title" => $tracks[$i]["grandparent_title"], "duration" => $tracks[$i]["duration"], "plays" => 1, "year" => $tracks[$i]["year"]));
}
}
// Sort $albums for oldest album and choose the oldest, verifiable album
$year = array_column($albums, 'year');
array_multisort($year, SORT_ASC, $albums);
if(count($albums) > 0) {
$oldest_found = false;
for($i = 0; $i < count($albums); $i++) {
if($albums[$i]["year"] !== "" && $albums[$i]["year"] > 1000) {
$album_oldest = array("parent_title" => $albums[$i]["parent_title"], "grandparent_title" => $albums[$i]["grandparent_title"], "year" => $albums[$i]["year"], "plays" => $albums[$i]["plays"], "duration" => $albums[$i]["duration"], "error" => false);
$oldest_found = true;
break;
}
}
if(!$oldest_found) {
$album_oldest = array("parent_title" => "No albums played", "grandparent_title" => "No albums played", "year" => 0, "plays" => 0, "duration" => 0, "error" => true);
}
} else {
$album_oldest = array("parent_title" => "No albums played", "grandparent_title" => "No albums played", "year" => 0, "plays" => 0, "duration" => 0, "error" => true);
}
// Sort $tracks by longest duration
$duration = array_column($tracks, 'duration');
array_multisort($duration, SORT_DESC, $tracks);
// Sort year_movies by duration
// Sort $albums by longest duration
$duration = array_column($albums, 'duration');
array_multisort($duration, SORT_DESC, $albums);
// Sort $artists by longest duration
$duration = array_column($artists, 'duration');
array_multisort($duration, SORT_DESC, $artists);
// Sort $year_movies by duration
$duration = array_column($year_movies, 'duration');
array_multisort($duration, SORT_DESC, $year_movies);
// Sort year_shows by duration
// Sort $year_shows by duration
$duration = array_column($year_shows, 'duration');
array_multisort($duration, SORT_DESC, $year_shows);
// Sort tracks by duration
// Sort $year_music by duration
$duration = array_column($year_music, 'duration');
array_multisort($duration, SORT_DESC, $year_music);
@ -851,49 +918,85 @@ function data_get_user_stats_loop($id, $array) {
// Choose return value based on if function is enabled
if($config->get_user_movie_stats) {
$return_movies = array("movies" => $movies, "user_movie_most_paused" => $movie_most_paused, "user_movie_finishing_percent" => $movie_percent_average, "user_movie_oldest" => $movie_oldest);
$return_movies = array("movies" => array_slice($movies, 0, 10), "user_movie_most_paused" => $movie_most_paused, "user_movie_finishing_percent" => $movie_percent_average, "user_movie_oldest" => $movie_oldest);
$duration = 0;
for($i = 0; $i < count($movies); $i++) {
$duration += $movies[$i]["duration"];
}
$return_movies["movie_duration"] = $duration;
$return_movies["movie_plays"] = count($movies);
} else {
$return_movies = array();
}
// Choose return value based on if function is enabled
if($config->get_user_show_stats) {
$return_shows = array("shows" => $shows, "episode_duration_longest" => $episode_duration_longest);
$return_shows = array("shows" => array_slice($shows, 0, 10), "episode_duration_longest" => $episode_duration_longest);
$duration = 0;
for($i = 0; $i < count($shows); $i++) {
$duration += $shows[$i]["duration"];
}
$return_shows["show_duration"] = $duration;
$return_shows["show_plays"] = count($shows);
} else {
$return_shows = array();
}
// Choose return value based on if function is enabled
if($config->get_user_music_stats) {
$return_music = array("music" => $tracks);
$return_music = array("tracks" => array_slice($tracks, 0, 10), "albums" => array_slice($albums, 0, 10), "user_album_oldest" => $album_oldest, "artists" => array_slice($artists, 0, 10));
$duration = 0;
for($i = 0; $i < count($tracks); $i++) {
$duration += $tracks[$i]["duration"];
}
$return_music["track_duration"] = $duration;
$return_music["track_plays"] = count($tracks);
}else {
$return_music = array();
}
// Choose return value based on if function is enabled
if($config->get_year_stats_movies) {
$return_year_movies = $year_movies;
$return_year_movies["movies"] = array_slice($year_movies, 0, 10);
$duration = 0;
for($i = 0; $i < count($year_movies); $i++) {
$duration += $year_movies[$i]["duration"];
}
$return_year_movies["movie_duration"] = $duration;
$return_year_movies["movie_plays"] = count($year_movies);
}else {
$return_year_movies = array();
}
// Choose return value based on if function is enabled
if($config->get_year_stats_shows) {
$return_year_shows = $year_shows;
$return_year_shows["shows"] = array_slice($year_shows, 0, 10);
$duration = 0;
for($i = 0; $i < count($year_shows); $i++) {
$duration += $year_shows[$i]["duration"];
}
$return_year_shows["show_duration"] = $duration;
$return_year_shows["show_plays"] = count($year_shows);
}else {
$return_year_shows = array();
}
// Choose return value based on if function is enabled
if($config->get_year_stats_shows) {
$return_year_music = $year_music;
if($config->get_year_stats_music) {
$return_year_music["artists"] = array_slice($year_music, 0, 10);
$duration = 0;
for($i = 0; $i < count($year_music); $i++) {
$duration += $year_music[$i]["duration"];
}
$return_year_music["music_duration"] = $duration;
$return_year_music["music_plays"] = count($year_music);
}else {
$return_year_music = array();
}
// Choose return value based on if function is enabled
if(($config->get_year_stats_shows || $config->get_year_stats_shows || $config->get_year_stats_shows) && $config->get_year_stats_leaderboard) {
$return_year_users = $year_users;
$return_year_users = array_slice($year_users, 0, 10);
} else {
$return_year_users = array();
}

View file

@ -17,20 +17,20 @@ function get_stats() {
if (this.readyState == 4 && (this.status == 200 || this.status == 400 || this.status == 500)) {
try {
var result= JSON.parse(this.responseText);
if(result.error) {
loading_icon.style.display = "none";
search_button("SEARCH");
document.getElementById('results_error').innerHTML = '<p style="color:inherit; text-shadow: none;">' + result.message + '</p>';
} else {
load_page(this.responseText);
}
} catch(error) {
document.getElementById('results_error').innerHTML = '<p style="color:inherit; text-shadow: none;">' + "API response can't be parsed." + '</p>';
console.log('Error: ' + error);
console.log(this.responseText);
loading_icon.style.display = "none";
}
if(result.error) {
loading_icon.style.display = "none";
search_button("SEARCH");
document.getElementById('results_error').innerHTML = '<p style="color:inherit; text-shadow: none;">' + result.message + '</p>';
} else {
load_page(this.responseText);
}
}
};
xhttp.withCredentials = true;
@ -106,14 +106,14 @@ function load_introduction() {
function load_movies() {
var text = "";
if(results.user.user_movies.data.movies.length > 1) {
if(results.user.user_movies.data.movie_plays > 1) {
text += "<div class='boks' style='height: auto !important; width: 100%; padding-bottom: 25em; padding-top: 25em; height:10em; background-color:#B9A3D2;'>";
text += "<div class='boks3'>";
text += "<h1>Movies!</h1>";
text += "<br><br><br>";
text += "<h2>You watched " + results.user.user_movies.data.movies.length + " movies. That's a lot of movies!</h2><p>(or not, I am pre-programmed to say that)</p>"
text += "<h2>You watched " + results.user.user_movies.data.movie_plays + " movies. That's a lot of movies!</h2><p>(or not, I am pre-programmed to say that)</p>"
text += "<br><br>";
text += "</div>";
@ -134,17 +134,14 @@ function load_movies() {
text += paused_movie(results.user.user_movies.data.user_movie_most_paused, false);
text += "</div>";
text += "<div class='boks2'>";
text += oldest_movie(results.user.user_movies.data.user_movie_oldest);
text += "</div>";
var sum = 0;
for(i = 0; (i < results.user.user_movies.data.movies.length); i++) {
sum += results.user.user_movies.data.movies[i].duration;
if(!results.user.user_movies.data.user_movie_oldest.error) {
text += "<div class='boks2'>";
text += oldest_movie(results.user.user_movies.data.user_movie_oldest);
text += "</div>";
}
text += "<div class='boks2'>";
text += you_spent(sum, 'movies', 'watching');
text += you_spent(results.user.user_movies.data.movie_duration, 'movies', 'watching');
text += "</div>";
text += "</div>";
@ -152,14 +149,14 @@ function load_movies() {
text += "</div>";
text += "</div>";
} else if(results.user.user_movies.data.movies.length == 1) {
} else if(results.user.user_movies.data.movie_plays == 1) {
text += "<div class='boks' style='height: auto !important; width: 100%; padding-bottom: 25em; padding-top: 25em; height:10em; background-color:#B9A3D2;'>";
text += "<div class='boks3'>";
text += "<h1>Movies!</h1>";
text += "<br><br><br>";
text += "<h2>You watched " + results.user.user_movies.data.movies.length + " movie. You know what you like!</h2><p>(at least you tried it out)</p>";
text += "<h2>You watched " + results.user.user_movies.data.movie_plays + " movie. You know what you like!</h2><p>(at least you tried it out)</p>";
text += "<br><br>";
text += "</div>";
@ -193,7 +190,7 @@ function load_movies() {
text += "<div class='status'>";
text += "<h1>Movies!</h1>";
text += "<br><br><br>";
text += "<h2>You watched " + results.user.user_movies.data.movies.length + " movies. That's impressive in itself!</h2><p>(might wanna try it)</p>"
text += "<h2>You watched " + results.user.user_movies.data.movie_plays + " movies. That's impressive in itself!</h2><p>(might wanna try it)</p>"
text += '<img src="assets/img/bored.svg" style="margin: auto; display: block; width: 15em;">';
text += "</div>";
text += "</div>";
@ -208,12 +205,12 @@ function load_movies() {
function load_shows() {
var text = "";
if(results.user.user_shows.data.shows.length > 1) {
if(results.user.user_shows.data.show_plays > 1) {
text += "<div class='boks' style='height: auto !important; width: 100%; padding-bottom: 25em; padding-top: 25em; height:10em; background-color:#BBD2A3;'>";
text += "<div class='boks3'>";
text += "<h1>Shows!</h1>";
text += "<br><br><br><h2>You watched " + results.user.user_shows.data.shows.length + " different shows.</h2><p>(No, watching The Office twice doesn't count as two shows)</p>"
text += "<br><br><br><h2>You watched " + results.user.user_shows.data.show_plays + " different shows.</h2><p>(No, watching The Office twice doesn't count as two shows)</p>"
text += "</div>";
text += "<div class='boks3'>";
@ -236,13 +233,8 @@ function load_shows() {
text += "</div>";
}
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 += "<div class='boks2'>";
text += you_spent(sum, 'shows', 'watching');
text += you_spent(results.user.user_shows.data.show_duration, 'shows', 'watching');
text += "</div>";
text += "</div>";
@ -250,13 +242,13 @@ function load_shows() {
text += "</div>";
text += "</div>";
} else if(results.user.user_shows.data.shows.length == 1) {
} else if(results.user.user_shows.data.show_plays == 1) {
text += "<div class='boks' style='height: auto !important; width: 100%; padding-bottom: 25em; padding-top: 25em; height:10em; background-color:#BBD2A3;'>";
text += "<div class='boks3'>";
text += "<h1>Shows!</h1>";
text += "<br><br><br><h2>You watched " + results.user.user_shows.data.shows.length + " show.</h2><p>(Better not be that same one again...)</p>"
text += "<br><br><br><h2>You watched " + results.user.user_shows.data.show_plays + " show.</h2><p>(Better not be that same one again...)</p>"
text += "</div>";
text += "<div class='boks3'>";
@ -289,7 +281,7 @@ function load_shows() {
text += "<div class='status'>";
text += "<h1>Shows!</h1>";
text += "<br><br><br>";
text += "<h2>You watched " + results.user.user_shows.data.shows.length + " shows. I get it, it's not for everyone!</h2><p>(might wanna try it)</p>"
text += "<h2>You watched " + results.user.user_shows.data.show_plays + " shows. I get it, it's not for everyone!</h2><p>(might wanna try it)</p>"
text += '<img src="assets/img/bored.svg" style="margin: auto; display: block; width: 15em;">';
text += "</div>";
text += "</div>";
@ -304,97 +296,39 @@ function load_shows() {
//MUSIC
function load_music() {
var text = "";
var albums = [];
var artists = [];
var tracks = results.user.user_music.data.music;
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, "year" : results.user.user_music.data.music[i].year});
}
}
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) {
if(results.user.user_music.data.track_plays > 1) {
text += "<div class='boks' style='height: auto !important; width: 100%; padding-bottom: 25em; padding-top: 25em; height:10em; background-color:#CFA38C;'>";
text += "<div class='boks3'>";
text += "<h1>Music!</h1>";
text += "<br><br><br><h2>You listened to " + results.user.user_music.data.music.length + ' different tracks.</h2><p>(If you can call your taste "music"...)</p>'
text += "<br><br><br><h2>You listened to " + results.user.user_music.data.track_plays + ' different tracks.</h2><p>(If you can call your taste "music"...)</p>'
text += "</div>";
text += "<div class='boks3'>";
text += "<div class='boks2'>";
text += top_list(results.user.user_music.data.music, "Your top tracks", true, false);
text += top_list(results.user.user_music.data.tracks, "Your top tracks", "track", false);
text += "</div>";
text += "<div class='boks2'>";
text += top_list(albums, "Your top albums", true, false);
text += top_list(results.user.user_music.data.albums, "Your top albums", "album", false);
text += "</div>";
text += "<div class='boks2'>";
text += top_list(artists, "Your top artists", false, false);
text += top_list(results.user.user_music.data.artists, "Your top artists", "artist", false);
text += "</div>";
text += "<div class='boks2' style='padding: 0;'>";
albums_year = []
for(var i = 0; i < albums.length; i++) {
if(typeof albums[i].year !== 'undefined' && albums[i].year !== "" && albums[i].year > 1000) {
albums_year.push(albums[i]);
}
}
albums_year.sort(function(a, b) {
return parseFloat(a.year) - parseFloat(b.year);
});
text += "<div class='boks2'>";
text += oldest_album(albums_year[0]);
text += "</div>";
var sum = 0;
for(i = 0; (i < results.user.user_music.data.music.length); i++) {
sum += results.user.user_music.data.music[i].duration;
if(!results.user.user_music.data.user_album_oldest.error) {
text += "<div class='boks2'>";
text += oldest_album(results.user.user_music.data.user_album_oldest);
text += "</div>";
}
text += "<div class='boks2'>";
text += you_spent(sum, 'music', 'listening');
text += you_spent(results.user.user_music.data.track_duration, 'music', 'listening');
text += "</div>";
text += "</div>";
@ -402,13 +336,13 @@ function load_music() {
text += "</div>";
text += "</div>";
} else if(results.user.user_music.data.music.length == 1) {
} else if(results.user.user_music.data.track_plays == 1) {
text += "<div class='boks' style='height: auto !important; width: 100%; padding-bottom: 25em; padding-top: 25em; height:10em; background-color:#CFA38C;'>";
text += "<div class='boks3'>";
text += "<h1>Music!</h1>";
text += "<br><br><br><h2>You listened to " + results.user.user_music.data.music.length + " track.</h2><p>(Whatever floats your boat...)</p>"
text += "<br><br><br><h2>You listened to " + results.user.user_music.data.track_plays + " track.</h2><p>(Whatever floats your boat...)</p>"
text += "</div>";
text += "<div class='boks3'>";
@ -428,7 +362,7 @@ function load_music() {
text += "<div class='status'>";
text += "<h1>Music!</h1>";
text += "<br><br><br>";
text += "<h2>You listened to " + results.user.user_music.data.music.length + " tracks. No speakers, huh?</h2><p>(might wanna try it)</p>"
text += "<h2>You listened to " + results.user.user_music.data.track_plays + " tracks. No speakers, huh?</h2><p>(might wanna try it)</p>"
text += '<img src="assets/img/bored.svg" style="margin: auto; display: block; width: 15em;">';
text += "</div>";
text += "</div>";
@ -470,7 +404,7 @@ function oldest_album(album) {
html += "<div class='status' id='list3' style='padding:1em;min-width:15em;'>";
html += "<div class='stats'>";
html += "The oldest album you listened to was <br><b>" + album.title + " (" + album.year + ")</b> by " + album.grandparent_title + "<br>";
html += "The oldest album you listened to was <br><b>" + album.parent_title + " (" + album.year + ")</b> by " + album.grandparent_title + "<br>";
html += "<br>Maybe get the vinyl release?";
html += '<br><br><img src="assets/img/old-man.svg" style="margin: auto; display: block; width: 15em;">';
@ -609,13 +543,20 @@ function top_list(array, title, music, year) {
html += "</div>";
html += "<div class='movie_name'>";
if(music) {
if(music === "track" || music === "album") {
html+= array[i].grandparent_title + "<br>";
}
html += "<b>";
html += array[i].title;
if(music === "album") {
html += array[i].parent_title;
} else if(music === "artist") {
html += array[i].grandparent_title;
} else {
html += array[i].title;
}
html += "</b>";
var movie_hour = seconds_to_time(array[i].duration, true);
@ -690,61 +631,39 @@ function load_users() {
text += top_list_names(results.year_stats.year_users.data, 'Top users');
text += "</div>";
var sum_movies = 0;
var sum_shows = 0;
var sum_artists = 0;
if(functions.get_year_stats_movies && results.year_stats.year_movies.data.length > 0) {
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 && results.year_stats.year_shows.data.length > 0) {
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 && results.year_stats.year_music.data.length > 0) {
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);
var time_movies = seconds_to_time(results.year_stats.year_movies.data.movie_duration, false);
var time_shows = seconds_to_time(results.year_stats.year_shows.data.show_duration, false);
var time_artists = seconds_to_time(results.year_stats.year_music.data.music_duration, false);
var time_all = seconds_to_time(Math.floor(results.year_stats.year_movies.data.movie_duration + results.year_stats.year_shows.data.show_duration + results.year_stats.year_music.data.music_duration), false);
var function_sum = 0;
text += "<div class='boks2'>";
text += "<div class='status' id='list3' style='padding:1em;min-width:15em;'>";
text += "<div class='stats'>";
if(functions.get_year_stats_movies && results.year_stats.year_movies.data.length > 0) {
text += "All the different users combined spent <b>" + time_movies + "</b>";
text += " watching movies.";
if(functions.get_year_stats_movies && results.year_stats.year_movies.data.movie_plays > 0) {
text += "All the different users combined spent<br><b>" + time_movies + "</b>";
text += "<br>watching movies.";
text += "<br><br>";
function_sum += 1;
}
if(functions.get_year_stats_shows && results.year_stats.year_shows.data.length > 0) {
text += "All the different users combined spent <b>" + time_shows + "</b>";
text += " watching shows.";
if(functions.get_year_stats_shows && results.year_stats.year_shows.data.show_plays > 0) {
text += "All the different users combined spent<br><b>" + time_shows + "</b>";
text += "<br>watching shows.";
text += "<br><br>";
function_sum += 1;
}
if(functions.get_year_stats_music && results.year_stats.year_music.data.length > 0) {
text += "All the different users combined spent <b>" + time_artists + "</b>";
text += " listening to artists.";
if(functions.get_year_stats_music && results.year_stats.year_music.data.music_plays > 0) {
text += "All the different users combined spent<br><b>" + time_artists + "</b>";
text += "<br>listening to artists.";
text += "<br><br>";
function_sum += 1;
}
if(function_sum > 1) {
text += "That is <b>" + time_all + "</b><br>of content!";
text += "That is<br><b>" + time_all + "</b><br>of content!";
}
text += '<img src="assets/img/home.svg" style="margin: auto; display: block; width: 15em;">';
@ -758,45 +677,21 @@ function load_users() {
text += "</div>";
text += "<div class='boks3'>";
if(functions.get_year_stats_movies && results.year_stats.year_movies.data.length > 0) {
if(functions.get_year_stats_movies && results.year_stats.year_movies.data.movie_plays > 0) {
text += "<div class='boks2'>";
text += top_list(results.year_stats.year_movies.data, "Top movies", false, true);
text += top_list(results.year_stats.year_movies.data.movies, "Top movies", false, true);
text += "</div>";
}
if(functions.get_year_stats_shows && results.year_stats.year_shows.data.length > 0) {
if(functions.get_year_stats_shows && results.year_stats.year_shows.data.show_plays > 0) {
text += "<div class='boks2'>";
text += top_list(results.year_stats.year_shows.data, "Top shows", false, false);
text += top_list(results.year_stats.year_shows.data.shows, "Top shows", false, false);
text += "</div>";
}
if(functions.get_year_stats_music && results.year_stats.year_music.data.length > 0) {
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);
});
if(functions.get_year_stats_music && results.year_stats.year_music.data.music_plays > 0) {
text += "<div class='boks2'>";
text += top_list(artists, "Top artists", false, false);
text += top_list(results.year_stats.year_music.data.artists, "Top artists", "artist", false);
text += "</div>";
}

View file

@ -2,69 +2,6 @@ function search_button(string) {
$('#search_get').html(string);
}
function time_days(seconds_input) {
var seconds = Number(seconds_input);
var days = seconds * 1.15741E-5;
var hours = String(days).split(".");
var hours_str = "0." + hours[1];
var hours_int = Number(hours_str) * 24.0;
var minutes = String(hours_int).split(".");
var minutes_str = "0." + minutes[1];
var minutes_int = Number(minutes_str) * 60.0;
var days_form = String(days).split(".");
var hours_form = String(hours_int).split(".");
var minutes_form = String(minutes_int).split(".");
var final = [Number(days_form[0]), Number(hours_form[0]), Number(minutes_form[0])];
return final;
}
function time_hours(seconds_input) {
var seconds = Number(seconds_input);
var hours_int = Number(seconds) * 0.0002777778;
var minutes = String(hours_int).split(".");
var minutes_str = "0." + minutes[1];
var minutes_int = Number(minutes_str) * 60.0;
var hours_form = String(hours_int).split(".");
var minutes_form = String(minutes_int).split(".");
var final = [Number(hours_form[0]), Number(minutes_form[0])];
return final;
}
function makeRequest (method, url, data) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
if(method=="POST" && data){
xhr.send(data);
}else{
xhr.send();
}
});
}
$(document).on('submit', '#stats_form', function(){
search_button("SEARCHING...");