Skip to content

Instantly share code, notes, and snippets.

@mutsuda
Last active October 21, 2023 19:57
Show Gist options
  • Save mutsuda/2f979436e289f9adf7e4580feb0c6a01 to your computer and use it in GitHub Desktop.
Save mutsuda/2f979436e289f9adf7e4580feb0c6a01 to your computer and use it in GitHub Desktop.
Shows the total number of movies and shows you dubbed, the three latest added to your profile and shows the poster of the latest movie/show you dubbed as a background. All info extracted from "eldoblaje.com"
function extractFirstThreeTitles(html) {
const regex = /<a href="FichaPelicula\.asp\?id=\d+"[^>]*>(.*?)<\/a>/g;
let titles = [];
let match;
while ((match = regex.exec(html)) !== null && titles.length < 3) {
titles.push(match[1]);
}
if (titles.length === 0) {
throw new Error('No titles found.');
}
return titles;
}
async function createWidget(name, dubs, titles, posterUrl) {
if (config.runsInWidget) {
let widget = new ListWidget();
widget.setPadding(0, 0, 0, 0);
try {
let posterRequest = new Request(posterUrl);
let posterImage = await posterRequest.loadImage();
widget.backgroundImage = posterImage;
} catch (error) {
console.log(`Couldn't download image: ${error}`);
}
let overlayView = widget.addStack();
overlayView.layoutVertically();
overlayView.addSpacer();
let textContainer = overlayView.addStack();
textContainer.layoutVertically();
textContainer.backgroundColor = new Color("#000000", 0.7);
textContainer.cornerRadius = 5;
textContainer.setPadding(10, 10, 10, 10);
let nameFont = new Font("Avenir-Heavy", 20);
let dubsFont = new Font("Avenir-Medium", 16);
let textFont = new Font("Avenir-Next", 9);
let nameText = textContainer.addText(name);
nameText.font = nameFont;
nameText.textColor = Color.white();
textContainer.addSpacer(2);
let dubbingsText = textContainer.addText(dubs);
dubbingsText.font = dubsFont;
dubbingsText.textColor = Color.white();
textContainer.addSpacer(8);
titles.forEach((title, index) => {
if (index < 3) {
let titleText = textContainer.addText(title.replace(/\s*\[.*?\]\s*$/, '').replace(/\s*\{.*?\}\s*$/, ''));
titleText.font = textFont;
titleText.textColor = Color.white();
textContainer.addSpacer(5);
}
});
overlayView.addSpacer();
return widget;
} else {
throw new Error("Unsupported widget. Please run the script in the appropriate context.");
}
}
async function getMoviePoster(movieTitle, apiKey) {
const baseUrl = 'https://api.themoviedb.org/3';
const imageUrl = 'https://image.tmdb.org/t/p/w500';
try {
const isTvShow = /\[.*?\]/.test(movieTitle);
const encodedTitle = encodeURIComponent(movieTitle.replace(/\[.*?\]|\(.*?\)/g, '').trim());
const searchEndpoint = isTvShow ? 'search/tv' : 'search/movie';
const url = `${baseUrl}/${searchEndpoint}?api_key=${apiKey}&query=${encodedTitle}`;
const request = new Request(url);
const response = await request.loadJSON();
if (response.results && response.results.length > 0) {
const firstResult = response.results[0];
const posterPath = firstResult.poster_path;
if (posterPath) {
return imageUrl + posterPath;
} else {
return 'Poster not available.';
}
} else {
return 'No results found.';
}
} catch (error) {
console.error('Error:', error);
return 'An error occurred.';
}
}
async function run() {
try {
let parameter = args.widgetParameter
if (parameter) parameters = parameter.split(";")
else
{
let w = new ListWidget()
error = w.addText("Introduce tu ID de actor de eldoblaje.com y la clave API de TMDB que obtendrás en https://www.themoviedb.org/settings/api separados por un ; en la configuración del Widget")
error.font = new Font("Avenir-Book", 11)
Script.setWidget(w)
Script.complete()
return
}
let url = "https://www.eldoblaje.com/datos/FichaActorDoblaje.asp?id="+ parameters[0] +"&Orden=F";
let r = new Request(url);
let body = await r.loadString();
const regexdubs = /en la base de datos eldoblaje\.com:\s*(\d+)/;
const regexname = /<title>\s*(.*?)\s*-\s*Ficha eldoblaje\.com\s*-\s*Doblaje<\/title>/i;
let talentName = body.match(regexname)[1].trim();
let dubsMatch = body.match(regexdubs);
let dubs = dubsMatch ? dubsMatch[1] : "N/A";
let titles = extractFirstThreeTitles(body);
let posterUrl = await getMoviePoster(titles[0], parameters[1]);
if (posterUrl) {
console.log(posterUrl);
} else {
console.error("No poster URL was retrieved");
}
let widget = await createWidget(talentName, dubs, titles, posterUrl);
if (widget) {
Script.setWidget(widget);
Script.complete();
widget.presentMedium();
} else {
console.error("Failed to create widget");
}
} catch (error) {
console.error("An error occurred:", error);
}
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment