New approach & timezone

This commit is contained in:
aunefyren 2021-10-04 22:21:43 +02:00
parent 3381249172
commit a595f139f9
6 changed files with 215 additions and 178 deletions

View file

@ -73,6 +73,8 @@ var library_id_movies = "";
var library_id_shows = "";
var library_id_music = "";
var timezone = "";
var wrapped_start = new Date(0);
wrapped_start.setUTCSeconds(1609455600);

View file

@ -162,6 +162,11 @@ function set_tautulli(back) {
html += '<input type="text" class="form-control" id="tautulli_root" value="' + tautulli_root + '" autocomplete="off" placeholder="tautulli"/><br>';
html += '</div>';
html += '<div class="form-group">';
html += '<label for="timezone" title="The timezone the data is located in.">Timezone</label>';
html += '<input type="text" class="form-control" id="timezone" value="Europe/Oslo" autocomplete="off" placeholder="Europe/Oslo" required /><br>';
html += '</div>';
html += '<div class="form-group">';
html += '<label for="ssl" title="Enable if your connection uses HTTPS.">Use SSL</label>';
html += '<input type="checkbox" class="form-control" id="ssl" ';
@ -207,6 +212,7 @@ function set_tautulli_details(back) {
tautulli_port = document.getElementById('tautulli_port').value;
tautulli_length = document.getElementById('tautulli_length').value;
tautulli_root = document.getElementById('tautulli_root').value;
timezone = document.getElementById('timezone').value;
ssl = document.getElementById('ssl').checked;
library_id_movies = document.getElementById('library_id_movies').value;
library_id_shows = document.getElementById('library_id_shows').value;

View file

@ -24,6 +24,9 @@ $library_id_movies = $config->library_id_movies;
$library_id_shows = $config->library_id_shows;
$library_id_music = $config->library_id_music;
// Set time-zone
date_default_timezone_set($config->timezone);
//Base-URL for connections
$connection = create_url();
@ -46,26 +49,12 @@ if (!$id) {
// Log API request if enabled
if($config->use_logs) {
if(!log_activity($id)) {
if(!log_activity($id, "")) {
echo json_encode(array("message" => "Failed to log event.", "error" => true));
exit(0);
}
}
// Use cache
if($config->use_cache) {
if($cache = check_cache()) {
$now = new DateTime('NOW');
$then = new DateTime($cache->date);
$diff = $now->diff($then);
if($diff->format('%a') < $config->cache_age_limit || $config->cache_age_limit == "" || $config->cache_age_limit == 0) {
echo json_encode($cache);
exit(0);
}
}
}
// Get user name
$name = tautulli_get_name($id);
if(!$name) {
@ -73,30 +62,52 @@ if(!$name) {
exit(0);
}
if($config->get_user_movie_stats) {
$user_movies = array("data" => tautulli_get_user_movies($id), "message" => "Success. User movie-stats are loaded.", "error" => False);
// GET WRAPPED DATES CACHE
if($config->use_cache) {
if($cache = check_cache()) {
$tautulli_data = $cache;
} else {
$tautulli_data = array();
}
}
// UPDATE THE CACHE
$tautulli_data = tautulli_get_wrapped_dates($id, $tautulli_data);
// SAVE WRAPPED DATES CACHE
if($config->use_cache) {
update_cache($tautulli_data);
}
// NEW LOOP USER-STATS
if($config->get_user_movie_stats || $config->get_user_show_stats || $config->get_user_music_stats) {
$user_stats = data_get_user_stats_loop($id, $tautulli_data);
if($config->get_user_movie_stats) {
$user_movies = array("data" => $user_stats["movies"], "message" => "Success. User movie-stats are loaded.", "error" => False);
} else {
$user_movies = array("error" => True, "message" => "Disabled in config.");
}
if($config->get_user_show_stats) {
$user_shows = array("data" => $user_stats["shows"], "message" => "Success. User show-stats are loaded.", "error" => False);
} else {
$user_shows = array("error" => True, "message" => "Disabled in config.");
}
if($config->get_user_show_stats) {
$user_music = array("data" => $user_stats["music"], "message" => "Success. User music-stats are loaded.", "error" => False);
} else {
$user_music = array("error" => True, "message" => "Disabled in config.");
}
} else {
$user_movies = array("error" => True, "message" => "Disabled in config.");
}
if($config->get_user_show_stats) {
$user_shows = array("data" => tautulli_get_user_shows($id), "message" => "Success. User show-stats are loaded.", "error" => False);
} else {
$user_shows = array("error" => True, "message" => "Disabled in config.", "data" => array());
}
if($config->get_user_show_buddy && $config->get_user_show_stats && !empty($user_shows["data"]["shows"])) {
$user_shows["data"] = $user_shows["data"] + array("show_buddy" => array("user" => tautulli_get_user_show_buddy($id, $user_shows["data"]["shows"]), "error" => False, "Message" => "Buddy is loaded."));
} else {
$user_shows["data"] = $user_shows["data"] + array("show_buddy" => array("message" => "Disabled in config.", "error" => True));
}
if($config->get_user_music_stats) {
$user_music = array("data" => tautulli_get_user_music($id), "message" => "Success. User music-stats are loaded.", "error" => False);
} else {
$user_shows = array("error" => True, "message" => "Disabled in config.");
$user_music = array("error" => True, "message" => "Disabled in config.");
}
$user_shows["data"] = $user_shows["data"] + array("show_buddy" => array("message" => "Disabled in config.", "error" => True));
/*
if($config->get_year_stats_movies || $config->get_year_stats_shows || $config->get_year_stats_music) {
if($config->use_cache) {
$year_stats = array("data" => tautulli_get_year_stats_cache($id), "error" => False, "message" => "Year stats are loaded.");
@ -106,6 +117,8 @@ if($config->get_year_stats_movies || $config->get_year_stats_shows || $config->g
} else {
$year_stats = array("data" => array(), "message" => "Disabled in config.", "error" => True);
}
*/
$year_stats = array("data" => array(), "message" => "Disabled in config.", "error" => True);
$now = new DateTime('NOW');
@ -122,9 +135,6 @@ $result = json_encode(array("error" => False,
"year_stats" => $year_stats,
));
if($config->use_cache) {
update_cache($result);
}
echo $result;
exit(0);
@ -220,11 +230,7 @@ function check_cache() {
$cache = json_decode(file_get_contents($path));
if(!empty($cache)) {
for($i = 0; $i < count($cache); $i++) {
if($cache[$i]->user->id == $id) {
return $cache[$i];
}
}
return $cache;
}
return False;
@ -257,7 +263,7 @@ function update_cache($result) {
return True;
}
function log_activity($id) {
function log_activity($id, $message) {
$date = date('Y-m-d H:i:s');
$path = "../config/wrapped.log";
@ -268,7 +274,7 @@ function log_activity($id) {
}
$log_file = fopen($path, 'a');
fwrite($log_file, PHP_EOL . $date . ' - get_stats.php - ID: ' . $id);
fwrite($log_file, PHP_EOL . $date . ' - get_stats.php - ID: ' . $id . ' - ' . $message);
if(fclose($log_file)) {
return True;
@ -277,106 +283,149 @@ function log_activity($id) {
return False;
}
function tautulli_get_user_music($id) {
function tautulli_get_wrapped_dates($id, $array) {
global $connection;
global $config;
global $library_id_music;
global $arrContextOptions;
$url = $connection . "/api/v2?apikey=" . $config->tautulli_apikey . "&cmd=get_history&user_id=" . $id . '&section_id=' . $library_id_music . "&media_type=track&order_column=date&order_dir=desc&include_activity=0&length=" . $config->tautulli_length . "";
if($config->ssl) {
$response = json_decode(file_get_contents($url, false, stream_context_create($arrContextOptions)));
} else {
$response = json_decode(file_get_contents($url));
}
$loop_time = $config->wrapped_start;
$end_loop_date = $config->wrapped_end;
$array = $response->response->data->data;
$tracks = array();
for ($i = 0; $i < count($array); $i++) {
if($array[$i]->date > $config->wrapped_end) {
continue;
} else if ($array[$i]->date < $config->wrapped_start) {
break;
}
if($array[$i]->title == "" || $array[$i]->grandparent_title == "" || $array[$i]->parent_title == "") {
continue;
}
$found = False;
for ($j = 0; $j < count($tracks); $j++) {
if($array[$i]->title == $tracks[$j]["title"] && $array[$i]->parent_title == $tracks[$j]["parent_title"] && $array[$i]->grandparent_title == $tracks[$j]["grandparent_title"]) {
$tracks[$j]["plays"] = intval($tracks[$j]["plays"]) + 1;
$tracks[$j]["duration"] = intval($tracks[$j]["duration"]) + $array[$i]->duration;
break;
}
}
if(!$found) {
array_push($tracks, array("title" => $array[$i]->title, "parent_title" => $array[$i]->parent_title, "grandparent_title" => $array[$i]->grandparent_title, "plays" => 1, "duration" => $array[$i]->duration, "year" => $array[$i]->year, "rating_key" => $array[$i]->rating_key, "parent_rating_key" => $array[$i]->parent_rating_key, "grandparent_rating_key" => $array[$i]->grandparent_rating_key));
}
}
// Sort $tracks for longest pause
$duration = array_column($tracks, 'duration');
array_multisort($duration, SORT_DESC, $tracks);
return array("music" => $tracks);
}
function tautulli_get_user_movies($id) {
global $connection;
global $config;
global $library_id_movies;
global $arrContextOptions;
for ($i = 0; $loop_time <= $end_loop_date; $i++) {
$url = $connection . "/api/v2?apikey=" . $config->tautulli_apikey . "&cmd=get_history&user_id=" . $id . "&section_id=" . $library_id_movies . "&order_column=date&order_dir=desc&include_activity=0&length=" . $config->tautulli_length . "";
$current_loop_date = date('Y-m-d', $loop_time);
if($config->ssl) {
$response = json_decode(file_get_contents($url, false, stream_context_create($arrContextOptions)));
} else {
$response = json_decode(file_get_contents($url));
}
$array = $response->response->data->data;
$movies = array();
$movies_percent_complete = array();
for ($i = 0; $i < count($array); $i++) {
if($array[$i]->date > $config->wrapped_end) {
continue;
} else if ($array[$i]->date < $config->wrapped_start) {
break;
}
$duration = $array[$i]->duration;
if($duration > 300) {
array_push($movies_percent_complete, $array[$i]->percent_complete);
}
$title = $array[$i]->full_title;
$year = $array[$i]->year;
$percent_complete = $array[$i]->percent_complete;
$paused_counter = $array[$i]->paused_counter;
$found = False;
for ($j = 0; $j < count($movies); $j++) {
if($movies[$j]["title"] == $title && $movies[$j]["year"] == $year ) {
$movies[$j]["plays"] = intval($movies[$j]["plays"]) + 1;
$movies[$j]["duration"] = intval($movies[$j]["duration"]) + intval($duration);
$found = True;
break;
for($j = 0; $j < count($array); $j++) {
if($array[$j]["date"] == $current_loop_date) {
continue;
}
}
if(!$found) {
array_push($movies, array("title" => $title, "year" => $year, "plays" => 1, "duration" => $duration, "paused_counter" => $paused_counter));
$url = $connection . "/api/v2?apikey=" . $config->tautulli_apikey . "&cmd=get_history&order_column=date&order_dir=desc&include_activity=0&length=" . $config->tautulli_length . "&start_date=" . $current_loop_date;
if($config->ssl) {
$response = json_decode(file_get_contents($url, false, stream_context_create($arrContextOptions)));
} else {
$response = json_decode(file_get_contents($url));
}
$temp = $response->response->data->data;
if(!empty($temp)) {
for($j = 0; $j < count($temp); $j++) {
if($temp[$j]->media_type == "movie" || $temp[$j]->media_type == "episode" || $temp[$j]->media_type == "track") {
array_push($array, array("date" => $current_loop_date, "data" => $temp[$j]));
}
}
}
$loop_time += 86400;
}
// Sort data by date
$date = array_column($array, 'date');
array_multisort($date, SORT_DESC, $array);
return $array;
}
function data_get_user_stats_loop($id, $array_data) {
$movies = array();
$movies_percent_complete = array();
$shows = array();
$tracks = array();
for ($i = 0; $i < count($array); $i++) {
for($d = 0; $d < count($array->data); $d++) {
if($config->get_user_movie_stats && $array[$i]->data[$d]->media_type == "movie" && $array[$i]->data[$d]->user_id == $id) {
if($array[$i]->data[$d]->date > $config->wrapped_end) {
continue;
} else if ($array[$i]->data[$d]->date < $config->wrapped_start) {
break;
}
$duration = $array[$i]->data[$d]->duration;
if($duration > 300) {
array_push($movies_percent_complete, $array[$i]->data[$d]->percent_complete);
}
$title = $array[$i]->data[$d]->full_title;
$year = $array[$i]->data[$d]->year;
$percent_complete = $array[$i]->data[$d]->percent_complete;
$paused_counter = $array[$i]->data[$d]->paused_counter;
$found = False;
for ($j = 0; $j < count($movies); $j++) {
if($movies[$j]["title"] == $title && $movies[$j]["year"] == $year ) {
$movies[$j]["plays"] = intval($movies[$j]["plays"]) + 1;
$movies[$j]["duration"] = intval($movies[$j]["duration"]) + intval($duration);
$found = True;
break;
}
}
if(!$found) {
array_push($movies, array("title" => $title, "year" => $year, "plays" => 1, "duration" => $duration, "paused_counter" => $paused_counter));
}
}
if($config->get_user_show_stats && $array[$i]->data[$d]->media_type == "episode" && $array[$i]->data[$d]->user_id == $id) {
if($array[$i]->data[$d]->date > $config->wrapped_end) {
continue;
} else if ($array[$i]->data[$d]->date < $config->wrapped_start) {
break;
}
$title = $array[$i]->data[$d]->grandparent_title;
$duration = $array[$i]->data[$d]->duration;
$found = False;
for ($j = 0; $j < count($shows); $j++) {
if($shows[$j]["title"] == $title) {
$shows[$j]["duration"] = intval($shows[$j]["duration"]) + intval($duration);
$shows[$j]["plays"] = intval($shows[$j]["plays"]) + 1;
$found = True;
break;
}
}
if(!$found) {
array_push($shows, array("title" => $title, "duration" => $duration, "plays" => 1));
}
}
if($config->get_user_music_stats && $array[$i]->data[$d]->media_type == "track" && $array[$i]->data[$d]->user_id == $id) {
if($array[$i]->data[$d]->date > $config->wrapped_end) {
continue;
} else if ($array[$i]->data[$d]->date < $config->wrapped_start) {
break;
}
if($array[$i]->data[$d]->title == "" || $array[$i]->data[$d]->grandparent_title == "" || $array[$i]->data[$d]->parent_title == "") {
continue;
}
$found = False;
for ($j = 0; $j < count($tracks); $j++) {
if($array[$i]->data[$d]->title == $tracks[$j]["title"] && $array[$i]->data[$d]->parent_title == $tracks[$j]["parent_title"] && $array[$i]->data[$d]->grandparent_title == $tracks[$j]["grandparent_title"]) {
$tracks[$j]["plays"] = intval($tracks[$j]["plays"]) + 1;
$tracks[$j]["duration"] = intval($tracks[$j]["duration"]) + $array[$i]->data[$d]->duration;
break;
}
}
if(!$found) {
array_push($tracks, array("title" => $array[$i]->data[$d]->title, "parent_title" => $array[$i]->data[$d]->parent_title, "grandparent_title" => $array[$i]->data[$d]->grandparent_title, "plays" => 1, "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));
}
}
}
}
@ -402,6 +451,13 @@ function tautulli_get_user_movies($id) {
$duration = array_column($movies, 'duration');
array_multisort($duration, SORT_DESC, $movies);
//Sort shows by duration
$duration = array_column($shows, 'duration');
array_multisort($duration, SORT_DESC, $shows);
// Sort $tracks for longest pause
$duration = array_column($tracks, 'duration');
array_multisort($duration, SORT_DESC, $tracks);
// Calculate average movie finishing percentage
$sum = 0;
@ -414,57 +470,26 @@ function tautulli_get_user_movies($id) {
$movie_percent_average = 0;
}
return array("movies" => $movies, "user_movie_most_paused" => $movie_most_paused, "user_movie_finishing_percent" => $movie_percent_average, "user_movie_oldest" => $movie_oldest);
}
function tautulli_get_user_shows($id) {
global $connection;
global $config;
global $library_id_shows;
global $arrContextOptions;
$url = $connection . "/api/v2?apikey=" . $config->tautulli_apikey . "&cmd=get_history&user_id=" . $id . "&section_id=" . $library_id_shows . "&order_column=date&order_dir=desc&include_activity=0&length=" . $config->tautulli_length . "";
if($config->ssl) {
$response = json_decode(file_get_contents($url, false, stream_context_create($arrContextOptions)));
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);
} else {
$response = json_decode(file_get_contents($url));
$return_movies = array();
}
$array = $response->response->data->data;
$shows = array();
for ($i = 0; $i < count($array); $i++) {
if($array[$i]->date > $config->wrapped_end) {
continue;
} else if ($array[$i]->date < $config->wrapped_start) {
break;
}
$title = $array[$i]->grandparent_title;
$duration = $array[$i]->duration;
$found = False;
for ($j = 0; $j < count($shows); $j++) {
if($shows[$j]["title"] == $title) {
$shows[$j]["duration"] = intval($shows[$j]["duration"]) + intval($duration);
$shows[$j]["plays"] = intval($shows[$j]["plays"]) + 1;
$found = True;
break;
}
}
if(!$found) {
array_push($shows, array("title" => $title, "duration" => $duration, "plays" => 1));
}
if($config->get_user_show_stats) {
$return_shows = array("shows" => $shows);
} else {
$return_shows = array();
}
//Sort shows by duration
$duration = array_column($shows, 'duration');
array_multisort($duration, SORT_DESC, $shows);
if($config->get_user_music_stats) {
$return_music = array("music" => $tracks);
}else {
$return_music = array();
}
return array("shows" => $shows);
return array("movies" => $return_movies, "shows" => $return_shows, "music" => $return_music);
}
function tautulli_get_user_show_buddy($id, $shows) {

View file

@ -61,6 +61,8 @@ function get_config() {
library_id_shows = result.data.library_id_shows;
library_id_music = result.data.library_id_music;
timezone = result.data.timezone;
wrapped_start = new Date(0);
wrapped_start.setUTCSeconds(result.data.wrapped_start);

View file

@ -16,6 +16,7 @@ function get_stats() {
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";

View file

@ -18,6 +18,7 @@ function set_config() {
"library_id_movies" : library_id_movies,
"library_id_shows" : library_id_shows,
"library_id_music" : library_id_music,
"timezone" : timezone,
"wrapped_start" : Math.round(wrapped_start.getTime() / 1000),
"wrapped_end" : Math.round(wrapped_end.getTime() / 1000),
"get_user_movie_stats" : get_user_movie_stats,