Skip to content

Commit

Permalink
csfd - movie year and name from page (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
landsman authored Sep 6, 2024
1 parent 8f5a7b9 commit 6a50263
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/csfd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
76 changes: 70 additions & 6 deletions src/csfd/movie.js
Original file line number Diff line number Diff line change
@@ -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('/');

Expand All @@ -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;
}
13 changes: 10 additions & 3 deletions src/kinobox/kinobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
11 changes: 6 additions & 5 deletions src/redirect/csfd-kinobox.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 */
Expand Down
9 changes: 5 additions & 4 deletions src/seznam/movie-card.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 0 additions & 14 deletions src/seznam/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Get number from text.
*
* @param text {string}
* @returns {number}
*/
export function parseNumber(text) {
return parseInt(text.match(/\d+/)[0]);
}

0 comments on commit 6a50263

Please sign in to comment.