Skip to content

Instantly share code, notes, and snippets.

@sbimochan
Last active December 7, 2024 09:15
Show Gist options
  • Save sbimochan/07d08715123f67c1c2c4919af69febfa to your computer and use it in GitHub Desktop.
Save sbimochan/07d08715123f67c1c2c4919af69febfa to your computer and use it in GitHub Desktop.
Use your rated data from Netflix to get suggestions from GPT or anything you want according to your preferences
/**
* Goto https://www.netflix.com/settings/viewing-history
* Visit rating tab
* Fire up console and paste this code.
* Gets you array of object. Use your rated data to get suggestions from GPT or anything you want
* according to your preferences
* Note: This script needs to be updated if netflix updates their page.
*/
/**
* Evaluate and return a single DOM node from an XPath expression.
* @param {string} path - The XPath expression.
* @returns {Element|null} - The resulting DOM element or null if not found.
*/
function getElementByXpath(path) {
return document.evaluate(
path,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
}
/**
* Extract the "Already rated" string from a given input string.
* @param {string} inputString - The input HTML string.
* @returns {string|null} - The extracted string or null if not found.
*/
function extractAlreadyRated(inputString) {
const regex = /Already rated:.*?\(.*?\)/;
const match = inputString.match(regex);
return match ? match[0] : null;
}
/**
* Compute the numerical rating based on the rating string.
* @param {string} ratingSentence - The rating string.
* @returns {number} - Numerical rating: 2 (two thumbs up), 1 (thumbs up), 0 (none).
*/
function computeRating(ratingSentence) {
switch (ratingSentence) {
case "Already rated: two thumbs up (click to remove rating)":
return 10;
case "Already rated: thumbs up (click to remove rating)":
return 5;
default:
return 0;
}
}
// Initialize an array to hold the result objects
const resultArray = [];
// XPath for the container element holding the list items
const containerXpath = `//*[@id="appMountPoint"]/div/div/div/div[2]/div/div/ul`;
const containerData = getElementByXpath(containerXpath);
// Check if the container exists
if (containerData) {
const itemCount = containerData.childNodes.length; // Number of <li> elements
// Loop through each <li> element
for (let n = 1; n <= itemCount; n++) {
// Construct XPath for key and value elements
const keyXpath = `//*[@id="appMountPoint"]/div/div/div/div[2]/div/div/ul/li[${n}]/div[2]`;
const valueXpath = `//*[@id="appMountPoint"]/div/div/div/div[2]/div/div/ul/li[${n}]/div[3]/div`;
// Fetch elements using XPath
const keyElement = getElementByXpath(keyXpath);
const valueElement = getElementByXpath(valueXpath);
// If both elements exist, process them
if (keyElement && valueElement) {
const ratedString = extractAlreadyRated(valueElement.innerHTML);
const rating = computeRating(ratedString);
resultArray.push({
name: keyElement.innerText.trim(),
rating
});
}
}
}
// Log the final result array
console.log(resultArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment