movie-web/src2/lib/scraper/theflix.js

121 lines
4.6 KiB
JavaScript
Raw Normal View History

2022-01-13 21:00:00 +01:00
const BASE_URL = `${process.env.REACT_APP_CORS_PROXY_URL}https://theflix.to`;
2021-12-30 20:23:15 +01:00
async function findContent(searchTerm, type) {
try {
const term = searchTerm.toLowerCase()
2021-12-30 21:54:45 +01:00
const tmdbRes = await fetch(`${process.env.REACT_APP_CORS_PROXY_URL}https://www.themoviedb.org/search/${type === 'show' ? 'tv' : type}?query=${term}`).then(d => d.text());
2021-12-30 20:23:15 +01:00
const doc = new DOMParser().parseFromString(tmdbRes, 'text/html');
const nodes = Array.from(doc.querySelectorAll('div.results > div > div.wrapper'));
const results = nodes.slice(0, 10).map((node) => {
let type = node.querySelector('div.details > div.wrapper > div.title > div > a').getAttribute('data-media-type');
2021-12-30 21:54:45 +01:00
type = type === 'tv' ? 'show' : type;
let title;
let year;
let slug;
if (type === 'movie') {
try {
title = node.querySelector('div.details > div.wrapper > div.title > div > a').textContent;
year = node.querySelector('div.details > div.wrapper > div.title > span').textContent.trim().split(' ')[2];
slug = node.querySelector('div.details > div.wrapper > div.title > div > a').getAttribute('href').split('/')[2];
} catch (e) {
2021-12-30 20:23:15 +01:00
// eslint-disable-next-line array-callback-return
return;
2021-12-30 21:54:45 +01:00
}
} else if (type === 'show') {
try {
title = node.querySelector('div.details > div.wrapper > div.title > div > a > h2').textContent;
year = node.querySelector('div.details > div.wrapper > div.title > span').textContent.trim().split(' ')[2];
slug = node.querySelector('div.details > div.wrapper > div.title > div > a').getAttribute('href').split('/')[2];
} catch (e) {
2021-12-30 20:23:15 +01:00
// eslint-disable-next-line array-callback-return
return;
2021-12-30 21:54:45 +01:00
}
2021-12-30 20:23:15 +01:00
}
return {
type: type,
title: title,
year: year,
slug: slug + '-' + title.replace(/[^a-z0-9]+|\s+/gmi, " ").replace(/\s+/g, '-').toLowerCase(),
source: 'theflix'
}
});
if (results.length > 1) {
return { options: results };
} else {
return { options: [ results[0] ] }
}
} catch (err) {
console.error(err);
throw new Error(err)
}
}
2021-12-30 21:54:45 +01:00
async function getEpisodes(slug) {
2022-01-02 10:49:21 +01:00
let tmdbRes;
try {
tmdbRes = await fetch(`${process.env.REACT_APP_CORS_PROXY_URL}https://www.themoviedb.org/tv/${slug}/seasons`).then(d => d.text());
} catch (err) {
tmdbRes = await fetch(`${process.env.REACT_APP_CORS_PROXY_URL}https://www.themoviedb.org/tv/${slug.split('-')[0]}/seasons`).then(d => d.text());
if (tmdbRes)
slug = slug.split('-')[0];
}
2021-12-30 21:54:45 +01:00
const sNodes = Array.from(new DOMParser().parseFromString(tmdbRes, 'text/html').querySelectorAll('div.column_wrapper > div.flex > div'));
let seasons = [];
let episodes = [];
2021-12-30 22:01:21 +01:00
for (let s of sNodes) {
const text = s.querySelector('div > section > div > div > div > h2 > a').textContent;
if (!text.includes('Season')) continue;
2021-12-30 21:54:45 +01:00
const season = text.split(' ')[1];
2021-12-30 20:23:15 +01:00
2021-12-30 21:54:45 +01:00
if (!seasons.includes(season)) {
seasons.push(season);
}
if (!episodes[season]) {
episodes[season] = [];
}
const epRes = await fetch(`${process.env.REACT_APP_CORS_PROXY_URL}https://www.themoviedb.org/tv/${slug}/season/${season}`).then(d => d.text());
const epNodes = Array.from(new DOMParser().parseFromString(epRes, 'text/html').querySelectorAll('div.episode_list > div.card'));
epNodes.forEach((e, i) => episodes[season].push(++i));
}
2021-12-30 22:01:21 +01:00
2021-12-30 21:54:45 +01:00
return { seasons, episodes };
}
async function getStreamUrl(slug, type, season, episode) {
2022-01-02 17:34:08 +01:00
let url;
if (type === 'show') {
url = `${BASE_URL}/tv-show/${slug}/season-${season}/episode-${episode}`;
} else {
url = `${BASE_URL}/movie/${slug}?movieInfo=${slug}`;
}
const res = await fetch(url).then(d => d.text());
2021-12-30 20:23:15 +01:00
const scripts = Array.from(new DOMParser().parseFromString(res, "text/html").querySelectorAll('script'));
const prop = scripts.find((e) => e.textContent.includes("theflixvd.b-cdn"));
if (prop) {
const data = JSON.parse(prop.textContent);
return { url: data.props.pageProps.videoUrl };
}
return { url: '' }
}
2021-12-30 21:54:45 +01:00
const theflix = { findContent, getStreamUrl, getEpisodes }
2021-12-30 21:58:03 +01:00
export default theflix;