From 6a50263ca62d6b133a89025723e1a2537f3b9884 Mon Sep 17 00:00:00 2001 From: Michal Landsman Date: Fri, 6 Sep 2024 22:45:47 +0200 Subject: [PATCH] csfd - movie year and name from page (#13) --- src/csfd/index.js | 2 +- src/csfd/movie.js | 76 +++++++++++++++++++++++++++++++++--- src/kinobox/kinobox.js | 13 ++++-- src/redirect/csfd-kinobox.js | 11 +++--- src/seznam/movie-card.js | 9 +++-- src/seznam/state.js | 14 ------- src/utils.js | 9 +++++ 7 files changed, 101 insertions(+), 33 deletions(-) create mode 100644 src/utils.js diff --git a/src/csfd/index.js b/src/csfd/index.js index bdd681e..37ceeb6 100644 --- a/src/csfd/index.js +++ b/src/csfd/index.js @@ -8,7 +8,7 @@ export function initCsfd() { console.debug('---------------- KINOBOX-CSFD ----------------'); /* redirect from csfd movie to kinobox search */ - const movieRedirect = redirectFromCsfdToKinobox(window.location.href); + const movieRedirect = redirectFromCsfdToKinobox(window.location.href, document); if (movieRedirect !== null) { window.location.href = movieRedirect; } diff --git a/src/csfd/movie.js b/src/csfd/movie.js index 856ef7d..eb5f20c 100644 --- a/src/csfd/movie.js +++ b/src/csfd/movie.js @@ -1,15 +1,49 @@ +import { parseNumber } from '../utils'; + /** - * Parse CSFD movie title from the URL slug + * Get movie name from the page HTML. * - * @param currentUrl - * @returns {string|null} + * @param html {Element|undefined} + * @returns {{year: number|null, name: string|null}|null} */ -export function parseTermFromURL(currentUrl) { - const check = currentUrl.includes('/film/'); - if (!check) { +function parseMovieNameFromPage(html) { + if (html === undefined) { return null; } + const wrapper = html.getElementsByClassName('main-movie-profile'); + if (wrapper.length === 0) { + console.debug("Movie wrapper not found!"); + return null; + } + + const movieName = wrapper[0].getElementsByClassName('film-header-name'); + if (movieName.length === 0) { + console.debug("Movie name not found! #2"); + return null; + } + + const name = movieName[0].getElementsByTagName('h1')[0]; + const nameText = name.textContent.trim(); + + const yearWrapper = wrapper[0].getElementsByClassName('film-info-content'); + const origin = yearWrapper[0].getElementsByClassName('origin'); + const year = origin[0].getElementsByTagName('span')[0]; + const cleanYear = parseNumber(year.textContent); + + return { + name: nameText, + year: cleanYear, + } +} + +/** + * Parse CSFD movie title from the URL slug + * + * @param currentUrl + * @returns {string|null} + */ +function parseTermFromURL(currentUrl) { // Extract the movie part (e.g., '2667-klub-rvacu') from the URL let urlParts = currentUrl.split('/'); @@ -22,3 +56,33 @@ export function parseTermFromURL(currentUrl) { // Replace hyphens with spaces return movieTitleWithHyphen.replace(/-/g, ' '); } + +/** + * Parse data to get name and year of the movie. + * + * @param currentUrl {string} + * @param html {Element|null} + * @returns {{year: string, name: string|undefined}} + */ +export function getMovieNameAndYear(currentUrl, html) { + const check = currentUrl.includes('/film/'); + if (!check) { + return null; + } + + const formUrl = parseTermFromURL(currentUrl); + + let result = { + name: formUrl || 'Matrix', + year: '', + }; + + const fromPage = parseMovieNameFromPage(html); + if (fromPage === null) { + return result; + } + + result = Object.assign(result, fromPage); + + return result; +} diff --git a/src/kinobox/kinobox.js b/src/kinobox/kinobox.js index e4ef64f..29c35f9 100644 --- a/src/kinobox/kinobox.js +++ b/src/kinobox/kinobox.js @@ -18,11 +18,18 @@ const getPage = { /** * Get URL for searching in movies on Kinobox. * - * @param query {string} + * @param name {string} + * @param year {string|undefined|null} * @returns {string} */ -export function searchMovieOnKinobox(query) { - const term = encodeURIComponent(query); +export function searchMovieOnKinobox(name, year = undefined) { + let result = name || ''; + + if (year !== undefined && year !== '' && year !== 'undefined') { + result += ' ' + year; + } + + const term = encodeURIComponent(result); return getPage.search(term); } diff --git a/src/redirect/csfd-kinobox.js b/src/redirect/csfd-kinobox.js index f663c45..6d064f9 100644 --- a/src/redirect/csfd-kinobox.js +++ b/src/redirect/csfd-kinobox.js @@ -1,4 +1,4 @@ -import { parseTermFromURL } from '../csfd/movie'; +import { getMovieNameAndYear } from '../csfd/movie'; import { parseSearch } from '../csfd/search'; import { parseTelevision } from '../csfd/tv'; import { parseCinema } from '../csfd/cinema'; @@ -18,13 +18,14 @@ import { * From current URL of the movie on www.csfd.cz redirect me to www.kinobox.cz alternative. * * @param currentUrl {string} window.location.href + * @param html {Element|undefined} * @returns {string|null} */ -export function redirectFromCsfdToKinobox(currentUrl) { +export function redirectFromCsfdToKinobox(currentUrl, html) { /* movie */ - const csfdMovieTerm = parseTermFromURL(currentUrl); - if (csfdMovieTerm !== null) { - return searchMovieOnKinobox(csfdMovieTerm); + const csfdMovieData = getMovieNameAndYear(currentUrl, html); + if (csfdMovieData !== null) { + return searchMovieOnKinobox(csfdMovieData.name, csfdMovieData.year); } /* search */ diff --git a/src/seznam/movie-card.js b/src/seznam/movie-card.js index fe7ba12..6a01e18 100644 --- a/src/seznam/movie-card.js +++ b/src/seznam/movie-card.js @@ -1,6 +1,7 @@ import { findSiblingElement } from './utils.js'; import { buildMovieButton } from './movie-button.js'; import { searchMovieOnKinobox } from '../kinobox/kinobox.js'; +import {parseNumber} from "../utils"; /** * These buttons help us find the promoted movie in the source code. @@ -28,13 +29,13 @@ export function findMovieTabs(html) { } /** + * Redirect to Kinobox. * * @param state {State} * @returns {string} */ function buildRedirect(state) { - const term = state.getMovieNameWithYear(); - return searchMovieOnKinobox(term); + return searchMovieOnKinobox(state.movieName, state.movieYear); } /** @@ -105,10 +106,10 @@ function findYearAndRating(movieCardHtml) { const wrapper = headlineData.lastElementChild; const ratingText = wrapper.firstElementChild.textContent.trim(); - const rating = parseInt(ratingText.match(/\d+/)[0]); + const rating = parseNumber(ratingText); const yearAndCategory = wrapper.lastElementChild.textContent.trim(); - const year = parseInt(yearAndCategory.match(/\d+/)[0]); + const year = parseNumber(yearAndCategory); return { rating, diff --git a/src/seznam/state.js b/src/seznam/state.js index c5ce6d5..51173fe 100644 --- a/src/seznam/state.js +++ b/src/seznam/state.js @@ -31,20 +31,6 @@ export class State { this.movieRating = value; } - /** - * Get me movie name with year - * @returns {string} - */ - getMovieNameWithYear() { - let result = this.movieName || ''; - - if (this.movieYear !== undefined) { - result += ' ' + this.movieYear; - } - - return result; - } - /** @type {Boolean} */ addedKinoboxButton = false; diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..f1afad4 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,9 @@ +/** + * Get number from text. + * + * @param text {string} + * @returns {number} + */ +export function parseNumber(text) { + return parseInt(text.match(/\d+/)[0]); +}