// Sample data representing the Plex library
let library = [
{
title: "Inception",
type: "Movie",
duration: 148,
genre: ["Action", "Sci-Fi"],
director: "Christopher Nolan",
popularity: 8.8,
rating: 87,
vibe: ["Mind-bending", "Exciting"],
watched: false
},
{
title: "The Office",
type: "TV Show",
duration: 22,
genre: ["Comedy"],
director: null,
popularity: 8.9,
rating: 81,
vibe: ["Funny", "Light-hearted"],
watched: false
},
// ... Add more sample data
];
// Initial setup
let unwatchedItems = library.filter(item => !item.watched);
let cutoff = 5; // Customize cutoff point
let questionsAsked = [];
let currentPool = [...unwatchedItems];
// Possible questions
let questions = [
{
text: "Movie or TV Show?",
attribute: "type",
options: ["Movie", "TV Show"]
},
{
text: "How much time do you have?",
attribute: "duration",
options: ["<30", "30-60", "60-120", ">120"]
},
{
text: "Which genre do you prefer?",
attribute: "genre",
options: ["Action", "Comedy", "Drama", "Horror", "Sci-Fi"]
},
{
text: "What's the vibe?",
attribute: "vibe",
options: ["Funny", "Romantic", "Thrilling", "Mind-bending", "Scary", "Light-hearted"]
},
{
text: "Do you prefer critically acclaimed or popular with audiences?",
attribute: "preference",
options: ["Critically Acclaimed", "Popular with Audiences"]
},
{
text: "Any specific director?",
attribute: "director",
options: ["Christopher Nolan", "Steven Spielberg", "Quentin Tarantino", "No Preference"]
},
// ... Add more questions as needed
];
// Function to display a question
function displayQuestion(question) {
let questionContainer = document.getElementById('question-container');
let questionElement = document.getElementById('question');
let answersElement = document.getElementById('answers');
questionElement.textContent = question.text;
answersElement.innerHTML = '';
question.options.forEach(option => {
let button = document.createElement('button');
button.textContent = option;
button.onclick = () => handleAnswer(question, option);
answersElement.appendChild(button);
});
}
// Handle the user's answer
function handleAnswer(question, answer) {
questionsAsked.push(question.attribute);
// Filter the current pool based on the answer
currentPool = currentPool.filter(item => {
if (question.attribute === 'duration') {
if (answer === '<30') return item.duration < 30;
if (answer === '30-60') return item.duration >=30 && item.duration <=60;
if (answer === '60-120') return item.duration >60 && item.duration <=120;
if (answer === '>120') return item.duration >120;
} else if (question.attribute === 'genre' || question.attribute === 'vibe') {
return item[question.attribute].includes(answer);
} else if (question.attribute === 'preference') {
if (answer === 'Critically Acclaimed') return item.rating > 80;
if (answer === 'Popular with Audiences') return item.popularity > 8;
} else if (question.attribute === 'director') {
if (answer === 'No Preference') return true;
return item.director === answer;
} else {
return item[question.attribute] === answer;
}
});
// Check if we've reached the cutoff or have no more questions
if (currentPool.length <= cutoff || questionsAsked.length === questions.length) {
showResults();
} else {
askNextQuestion();
}
}
// Function to select the next question
function askNextQuestion() {
// Find questions that haven't been asked yet
let remainingQuestions = questions.filter(q => !questionsAsked.includes(q.attribute));
// Calculate the split for each question
let bestQuestion = null;
let bestSplitDiff = Infinity;
remainingQuestions.forEach(question => {
let splits = question.options.map(option => {
let filtered = currentPool.filter(item => {
if (question.attribute === 'duration') {
if (option === '<30') return item.duration < 30;
if (option === '30-60') return item.duration >=30 && item.duration <=60;
if (option === '60-120') return item.duration >60 && item.duration <=120;
if (option === '>120') return item.duration >120;
} else if (question.attribute === 'genre' || question.attribute === 'vibe') {
return item[question.attribute].includes(option);
} else if (question.attribute === 'preference') {
if (option === 'Critically Acclaimed') return item.rating > 80;
if (option === 'Popular with Audiences') return item.popularity > 8;
} else if (question.attribute === 'director') {
if (option === 'No Preference') return true;
return item.director === option;
} else {
return item[question.attribute] === option;
}
});
return filtered.length;
});
// Calculate how balanced the split is
let maxSplit = Math.max(...splits);
let minSplit = Math.min(...splits);
let splitDiff = maxSplit - minSplit;
if (splitDiff < bestSplitDiff) {
bestSplitDiff = splitDiff;
bestQuestion = question;
}
});
if (bestQuestion) {
displayQuestion(bestQuestion);
} else {
showResults();
}
}
// Function to display the results
function showResults() {
let questionContainer = document.getElementById('question-container');
let resultContainer = document.getElementById('result-container');
let resultsElement = document.getElementById('results');
questionContainer.style.display = 'none';
resultContainer.style.display = 'block';
resultsElement.innerHTML = '';
if (currentPool.length === 0) {
resultsElement.innerHTML = '<li>No results found based on your preferences.</li>';
} else {
currentPool.forEach(item => {
let li = document.createElement('li');
li.textContent = `${item.title} (${item.type}) [${item.genre.join(', ')}]`;
resultsElement.appendChild(li);
});
}
}
// Start the app by asking the first question
function startApp() {
// First question is "Movie or TV Show?"
let firstQuestion = questions.find(q => q.attribute === 'type');
displayQuestion(firstQuestion);
}
startApp();