`;
document.getElementById('cache').innerHTML = html;
add_to_cache_log('Creating new cache request. Maximum ' + days + ' days.')
get_stats(days);
}
function cache_log(days, result, complete) {
if(result) {
add_to_cache_log('Completed caching with a maximum of ' + days + ' days.', false, '');
} else {
add_to_cache_log('Error caching ' + days + ' days. Stopping.', true, 'var(--red)');
document.getElementById('loading_icon').style.display = "none";
document.getElementById("cache_button").disabled = false;
document.getElementById("cache_button").style.opacity = '1';
}
if(complete) {
add_to_cache_log('Finished caching request.', true, 'var(--green)');
document.getElementById('loading_icon').style.display = "none";
document.getElementById("cache_button").disabled = false;
document.getElementById("cache_button").style.opacity = '1';
} else {
add_to_cache_log('Requesting new cache from Wrapperr. Maximum ' + days + ' days.', false, '');
}
var cache_results = document.getElementById("cache_results");
cache_results.scrollTop = cache_results.scrollHeight;
}
function get_stats(days) {
document.getElementById("cache_button").disabled = true;
document.getElementById("cache_button").style.opacity = '0.5';
stats_form = {
"caching" : true,
"cache_limit" : days
};
var stats_data = JSON.stringify(stats_form);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
if(this.responseText.includes('Maximum execution time of')) {
cache_log(days, false, true);
document.getElementById("cache_button").disabled = false;
document.getElementById("cache_button").style.opacity = '1';
alert('PHP runtime was exceeded and stopped execution. Lower days cached to prevent this.');
console.log('Failed to parse API response. Error: ' + this.responseText);
} else {
cache_log(days, false, true);
alert('Failed to parse API response.');
console.log('Failed to parse API response. Error: ' + this.responseText);
document.getElementById("cache_button").disabled = false;
document.getElementById("cache_button").style.opacity = '1';
}
return;
}
if(result.error) {
alert(result.message);
cache_log(days, false, true);
} else {
if(!result.data) {
cache_log(days, true, result.data);
get_stats(days);
} else {
cache_log(days, true, result.data);
}
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "get/statistics", );
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.setRequestHeader("Authorization", "Bearer " + cookie);
xhttp.send(stats_data);
return;
}
function log_menu() {
var html = '
';
html += '';
html += '
';
html += '
';
html += '
';
html += '';
html += `
`;
document.getElementById("setup").innerHTML = html;
get_log();
}
function get_log() {
document.getElementById("log_button").disabled = true;
document.getElementById("log_button").style.opacity = '0.5';
log_data_form = {};
var log_data_data = JSON.stringify(log_form);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
alert('Failed to parse API response.');
console.log('Failed to parse API response. Error: ' + this.responseText);
document.getElementById("log_button").disabled = false;
document.getElementById("log_button").style.opacity = '1';
return;
}
if(result.error) {
document.getElementById("log_button").disabled = false;
document.getElementById("log_button").style.opacity = '1';
alert(result.message);
} else {
document.getElementById("log_button").disabled = false;
document.getElementById("log_button").style.opacity = '1';
log_form(result.data, result.limit);
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "get/log", );
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.setRequestHeader("Authorization", "Bearer " + cookie);
xhttp.send(log_data_data);
return;
}
function log_form(log_lines_array, limit) {
var log_table = `
Date |
Time |
Message |
`;
for(index = 0; index < log_lines_array.length; index++) {
var tablerow = `
` + log_lines_array[index].date + ` |
` + log_lines_array[index].time + ` |
` + log_lines_array[index].message + ` |
`;
log_table += tablerow
}
document.getElementById('log_results_body').innerHTML = log_table
document.getElementById('log_result_text').innerHTML = 'Retrieved last ' + limit + ' lines.';
var log_results = document.getElementById("log_results");
log_results.scrollTop = log_results.scrollHeight;
}
function test_tautulli_connection(tautulli_id) {
document.getElementById("test_connection_" + tautulli_id).disabled = true;
document.getElementById("test_connection_" + tautulli_id).style.opacity = '0.5';
var button = document.getElementById('test_connection_' + tautulli_id);
button.style.backgroundColor = 'lightgrey';
https_temp = document.getElementById('tautulli_https_' + tautulli_id).checked;
ip_temp = document.getElementById('tautulli_ip_' + tautulli_id).value;
root_temp = document.getElementById('tautulli_root_' + tautulli_id).value;
port_temp = parseInt(document.getElementById('tautulli_port_' + tautulli_id).value);
api_temp = document.getElementById('tautulli_apikey_' + tautulli_id).value;
config_form = {"tautulli_https" : https_temp, "tautulli_apikey" : api_temp, "tautulli_port" : port_temp, "tautulli_root" : root_temp, "tautulli_ip" : ip_temp};
var config_data = JSON.stringify(config_form);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
console.log('Failed to parse API response. Response: ' + this.responseText)
alert('Failed to parse API response.');
button.style.backgroundColor = 'var(--red)';
document.getElementById("test_connection_" + tautulli_id).disabled = false;
document.getElementById("test_connection_" + tautulli_id).style.opacity = '1';
return;
}
if(result.error) {
button.style.backgroundColor = 'var(--red)';
document.getElementById("test_connection_" + tautulli_id).disabled = false;
document.getElementById("test_connection_" + tautulli_id).style.opacity = '1';
alert(result.message);
return
}
if(result.data) {
button.style.backgroundColor = 'var(--green)';
document.getElementById("test_connection_" + tautulli_id).disabled = false;
document.getElementById("test_connection_" + tautulli_id).style.opacity = '1';
} else {
button.style.backgroundColor = 'var(--red)';
document.getElementById("test_connection_" + tautulli_id).disabled = false;
document.getElementById("test_connection_" + tautulli_id).style.opacity = '1';
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + 'get/tautulli-connection');
xhttp.send(config_data);
}
function get_wrapper_version() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
console.log('Failed to parse Wrapperr version. Response: ' + this.responseText)
alert("Failed to parse API response");
return;
}
if(!result.error) {
document.getElementById('github_link').innerHTML = 'GitHub (' + result.wrapperr_version + ')';
if(result.application_name && result.application_name !== '') {
document.getElementById('application_name').innerHTML = result.application_name + ' Setup';
document.title = result.application_name;
}
if(result.wrapperr_root != "") {
api_url = window.location.origin + "/" + result.wrapperr_root + "/api/";
console.log("URL: " + api_url)
}
get_admin_state();
}
} else if(this.readyState == 4 && this.status !== 200) {
var html = '
' + this.status + ' Error
';
html += '
The API did not respond correctly.
';
document.getElementById("setup").innerHTML = html;
}
};
xhttp.withCredentials = true;
// Get the root without "/admin"
root = window.location.pathname.replace('/admin', '')
// Maybe add trailing slash depending on the end of "window.location.origin"
var trailingslash = ""
if(window.location.origin.charAt(window.location.origin.length-1) != "/") {
trailingslash = "/"
}
// Reach the API to get URL base
xhttp.open("post", window.location.origin + "/" + root + trailingslash + "api/get/wrapperr-version");
xhttp.send();
return;
}
// Get admin configuration state
function get_admin_state() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
console.log('Failed to parse API response. Response: ' + this.responseText)
return;
}
if(result.error) {
console.log(result.message);
} else if(!result.data) {
first_time = true;
set_password_form();
} else {
cookie = get_cookie('wrapperr-admin');
if(cookie) {
validate_cookie_admin(cookie);
} else {
login_menu();
}
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "get/admin-state");
xhttp.send();
return;
}
// Validate admin login
function validate_cookie_admin(cookie) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
console.log('Failed to parse API response. Response: ' + this.responseText)
}
if(result.error) {
set_cookie("wrapperr-admin", "", 1);
login_menu();
document.getElementById("password_login_form_error").innerHTML = result.message;
} else {
get_config(get_cookie('wrapperr-admin'));
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "validate/admin");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.setRequestHeader("Authorization", "Bearer " + cookie);
xhttp.send();
return;
}
// Get config for admin to configure
function get_config(cookie) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result = JSON.parse(this.responseText);
}
catch {
alert("Failed to parse API response.");
return;
}
if(result.error) {
alert(result.message);
location.reload();
} else {
tautulli = result.data.tautulli_config;
timezone = result.data.timezone;
create_share_links = result.data.create_share_links;
plex_auth = result.data.plex_auth;
use_cache = result.data.use_cache;
use_logs = result.data.use_logs;
clientID = result.data.clientID;
wrapperr_root = result.data.wrapperr_root;
application_name_str = result.data.application_name;
application_url_str = result.data.application_url;
winter_theme = result.data.winter_theme;
wrapped_start = new Date(0);
wrapped_start.setUTCSeconds(result.data.wrapped_start);
wrapped_end = new Date(0);
wrapped_end.setUTCSeconds(result.data.wrapped_end);
stats_order_by_plays = result.data.wrapperr_customize.stats_order_by_plays;
stats_order_by_duration = result.data.wrapperr_customize.stats_order_by_duration;
wrapperr_front_page_title = result.data.wrapperr_customize.wrapperr_front_page_title;
wrapperr_front_page_subtitle = result.data.wrapperr_customize.wrapperr_front_page_subtitle;
stats_intro_title = result.data.wrapperr_customize.stats_intro_title;
stats_intro_subtitle = result.data.wrapperr_customize.stats_intro_subtitle;
stats_outro_title = result.data.wrapperr_customize.stats_outro_title;
stats_outro_subtitle = result.data.wrapperr_customize.stats_outro_subtitle;
stats_top_list_length = result.data.wrapperr_customize.stats_top_list_length;
obfuscate_other_users = result.data.wrapperr_customize.obfuscate_other_users;
get_user_movie_stats = result.data.wrapperr_customize.get_user_movie_stats;
get_user_movie_stats_title = result.data.wrapperr_customize.get_user_movie_stats_title;
get_user_movie_stats_subtitle = result.data.wrapperr_customize.get_user_movie_stats_subtitle;
get_user_movie_stats_subsubtitle = result.data.wrapperr_customize.get_user_movie_stats_subsubtitle;
get_user_movie_stats_subtitle_one = result.data.wrapperr_customize.get_user_movie_stats_subtitle_one;
get_user_movie_stats_subsubtitle_one = result.data.wrapperr_customize.get_user_movie_stats_subsubtitle_one;
get_user_movie_stats_subtitle_none = result.data.wrapperr_customize.get_user_movie_stats_subtitle_none;
get_user_movie_stats_subsubtitle_none = result.data.wrapperr_customize.get_user_movie_stats_subsubtitle_none;
get_user_movie_stats_top_movie = result.data.wrapperr_customize.get_user_movie_stats_top_movie;
get_user_movie_stats_top_movie_plural = result.data.wrapperr_customize.get_user_movie_stats_top_movie_plural;
get_user_movie_stats_movie_completion_title = result.data.wrapperr_customize.get_user_movie_stats_movie_completion_title;
get_user_movie_stats_movie_completion_title_plural = result.data.wrapperr_customize.get_user_movie_stats_movie_completion_title_plural;
get_user_movie_stats_movie_completion_subtitle = result.data.wrapperr_customize.get_user_movie_stats_movie_completion_subtitle;
get_user_movie_stats_pause_title = result.data.wrapperr_customize.get_user_movie_stats_pause_title;
get_user_movie_stats_pause_subtitle = result.data.wrapperr_customize.get_user_movie_stats_pause_subtitle;
get_user_movie_stats_pause_title_one = result.data.wrapperr_customize.get_user_movie_stats_pause_title_one;
get_user_movie_stats_pause_subtitle_one = result.data.wrapperr_customize.get_user_movie_stats_pause_subtitle_one;
get_user_movie_stats_pause_title_none = result.data.wrapperr_customize.get_user_movie_stats_pause_title_none;
get_user_movie_stats_pause_subtitle_none = result.data.wrapperr_customize.get_user_movie_stats_pause_subtitle_none;
get_user_movie_stats_oldest_title = result.data.wrapperr_customize.get_user_movie_stats_oldest_title;
get_user_movie_stats_oldest_subtitle = result.data.wrapperr_customize.get_user_movie_stats_oldest_subtitle;
get_user_movie_stats_oldest_subtitle_pre_1950 = result.data.wrapperr_customize.get_user_movie_stats_oldest_subtitle_pre_1950;
get_user_movie_stats_oldest_subtitle_pre_1975 = result.data.wrapperr_customize.get_user_movie_stats_oldest_subtitle_pre_1975;
get_user_movie_stats_oldest_subtitle_pre_2000 = result.data.wrapperr_customize.get_user_movie_stats_oldest_subtitle_pre_2000;
get_user_movie_stats_spent_title = result.data.wrapperr_customize.get_user_movie_stats_spent_title;
get_user_show_stats = result.data.wrapperr_customize.get_user_show_stats;
get_user_show_stats_buddy = result.data.wrapperr_customize.get_user_show_stats_buddy;
get_user_show_stats_title = result.data.wrapperr_customize.get_user_show_stats_title;
get_user_show_stats_subtitle = result.data.wrapperr_customize.get_user_show_stats_subtitle;
get_user_show_stats_subsubtitle = result.data.wrapperr_customize.get_user_show_stats_subsubtitle;
get_user_show_stats_subtitle_one = result.data.wrapperr_customize.get_user_show_stats_subtitle_one;
get_user_show_stats_subsubtitle_one = result.data.wrapperr_customize.get_user_show_stats_subsubtitle_one;
get_user_show_stats_subtitle_none = result.data.wrapperr_customize.get_user_show_stats_subtitle_none;
get_user_show_stats_subsubtitle_none = result.data.wrapperr_customize.get_user_show_stats_subsubtitle_none;
get_user_show_stats_top_show = result.data.wrapperr_customize.get_user_show_stats_top_show;
get_user_show_stats_top_show_plural = result.data.wrapperr_customize.get_user_show_stats_top_show_plural;
get_user_show_stats_spent_title = result.data.wrapperr_customize.get_user_show_stats_spent_title;
get_user_show_stats_most_played_title = result.data.wrapperr_customize.get_user_show_stats_most_played_title;
get_user_show_stats_most_played_subtitle = result.data.wrapperr_customize.get_user_show_stats_most_played_subtitle;
get_user_show_stats_buddy_title = result.data.wrapperr_customize.get_user_show_stats_buddy_title;
get_user_show_stats_buddy_subtitle = result.data.wrapperr_customize.get_user_show_stats_buddy_subtitle;
get_user_show_stats_buddy_title_none = result.data.wrapperr_customize.get_user_show_stats_buddy_title_none;
get_user_show_stats_buddy_subtitle_none = result.data.wrapperr_customize.get_user_show_stats_buddy_subtitle_none;
get_user_music_stats = result.data.wrapperr_customize.get_user_music_stats;
get_user_music_stats_title = result.data.wrapperr_customize.get_user_music_stats_title;
get_user_music_stats_subtitle = result.data.wrapperr_customize.get_user_music_stats_subtitle;
get_user_music_stats_subsubtitle = result.data.wrapperr_customize.get_user_music_stats_subsubtitle;
get_user_music_stats_subtitle_one = result.data.wrapperr_customize.get_user_music_stats_subtitle_one;
get_user_music_stats_subsubtitle_one = result.data.wrapperr_customize.get_user_music_stats_subsubtitle_one;
get_user_music_stats_subtitle_none = result.data.wrapperr_customize.get_user_music_stats_subtitle_none;
get_user_music_stats_subsubtitle_none = result.data.wrapperr_customize.get_user_music_stats_subsubtitle_none;
get_user_music_stats_top_track = result.data.wrapperr_customize.get_user_music_stats_top_track;
get_user_music_stats_top_track_plural = result.data.wrapperr_customize.get_user_music_stats_top_track_plural;
get_user_music_stats_top_album_plural = result.data.wrapperr_customize.get_user_music_stats_top_album_plural;
get_user_music_stats_top_artist_plural = result.data.wrapperr_customize.get_user_music_stats_top_artist_plural;
get_user_music_stats_spent_title = result.data.wrapperr_customize.get_user_music_stats_spent_title;
get_user_music_stats_spent_subtitle = result.data.wrapperr_customize.get_user_music_stats_spent_subtitle;
get_user_music_stats_oldest_album_title = result.data.wrapperr_customize.get_user_music_stats_oldest_album_title;
get_user_music_stats_oldest_album_subtitle = result.data.wrapperr_customize.get_user_music_stats_oldest_album_subtitle;
get_year_stats_title = result.data.wrapperr_customize.get_year_stats_title;
get_year_stats_subtitle = result.data.wrapperr_customize.get_year_stats_subtitle;
get_year_stats_subsubtitle = result.data.wrapperr_customize.get_year_stats_subsubtitle;
get_year_stats_movies = result.data.wrapperr_customize.get_year_stats_movies;
get_year_stats_movies_title = result.data.wrapperr_customize.get_year_stats_movies_title;
get_year_stats_movies_duration_title = result.data.wrapperr_customize.get_year_stats_movies_duration_title;
get_year_stats_shows = result.data.wrapperr_customize.get_year_stats_shows;
get_year_stats_shows_title = result.data.wrapperr_customize.get_year_stats_shows_title;
get_year_stats_shows_duration_title = result.data.wrapperr_customize.get_year_stats_shows_duration_title;
get_year_stats_music = result.data.wrapperr_customize.get_year_stats_music;
get_year_stats_music_title = result.data.wrapperr_customize.get_year_stats_music_title;
get_year_stats_music_duration_title = result.data.wrapperr_customize.get_year_stats_music_duration_title;
get_year_stats_leaderboard = result.data.wrapperr_customize.get_year_stats_leaderboard;
get_year_stats_leaderboard_numbers = result.data.wrapperr_customize.get_year_stats_leaderboard_numbers;
get_year_stats_leaderboard_title = result.data.wrapperr_customize.get_year_stats_leaderboard_title;
get_year_stats_duration_sum_title = result.data.wrapperr_customize.get_year_stats_duration_sum_title;
wrapperr_and = result.data.wrapperr_customize.wrapperr_and;
wrapperr_play = result.data.wrapperr_customize.wrapperr_play;
wrapperr_play_plural = result.data.wrapperr_customize.wrapperr_play_plural;
wrapperr_day = result.data.wrapperr_customize.wrapperr_day;
wrapperr_day_plural = result.data.wrapperr_customize.wrapperr_day_plural;
wrapperr_hour = result.data.wrapperr_customize.wrapperr_hour;
wrapperr_hour_plural = result.data.wrapperr_customize.wrapperr_hour_plural;
wrapperr_minute = result.data.wrapperr_customize.wrapperr_minute;
wrapperr_minute_plural = result.data.wrapperr_customize.wrapperr_minute_plural;
wrapperr_second = result.data.wrapperr_customize.wrapperr_second;
wrapperr_second_plural = result.data.wrapperr_customize.wrapperr_second_plural;
wrapperr_sort_plays = result.data.wrapperr_customize.wrapperr_sort_plays;
wrapperr_sort_duration = result.data.wrapperr_customize.wrapperr_sort_duration;
username = result.username;
admin_menu();
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "get/config");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.setRequestHeader("Authorization", "Bearer " + cookie);
xhttp.send();
}