feat: fix and add strict title check

This commit is contained in:
Viren070
2025-02-05 13:03:40 +00:00
parent a028fe3c32
commit 2916a0431a
+51 -27
View File
@@ -28,6 +28,7 @@ const CONFIG = {
addonName: "GDrive", addonName: "GDrive",
prioritiseLanguage: null, prioritiseLanguage: null,
proxiedPlayback: true, proxiedPlayback: true,
strictTitleCheck: false,
tmdbApiKey: null, tmdbApiKey: null,
enableSearchCatalog: true, enableSearchCatalog: true,
enableVideoCatalog: true, enableVideoCatalog: true,
@@ -508,7 +509,7 @@ function isConfigValid() {
} }
async function getMetadata(type, fullId) { async function getMetadata(type, fullId) {
let id = fullId let id = fullId;
if (id.startsWith("kitsu")) { if (id.startsWith("kitsu")) {
id = id.split(":")[0] + ":" + id.split(":")[1]; // Remove the :1 at the end id = id.split(":")[0] + ":" + id.split(":")[1]; // Remove the :1 at the end
@@ -526,12 +527,12 @@ async function getMetadata(type, fullId) {
}); });
return null; return null;
} }
id = id.split(":")[0];
if (CONFIG.tmdbApiKey) { if (CONFIG.tmdbApiKey) {
try { try {
meta = await getTmdbMeta(type, id); const meta = await getTmdbMeta(type, id);
if (meta) { if (meta) {
console.log({ console.log({
message: "Successfully retrieved metadata from TMDb", message: "Successfully retrieved metadata from TMDb",
@@ -547,7 +548,7 @@ async function getMetadata(type, fullId) {
} }
} }
try { try {
meta = await getCinemetaMeta(type, id); const meta = await getCinemetaMeta(type, id);
if (meta) { if (meta) {
console.log({ console.log({
message: "Successfully retrieved metadata from Cinemeta", message: "Successfully retrieved metadata from Cinemeta",
@@ -563,7 +564,7 @@ async function getMetadata(type, fullId) {
} }
try { try {
meta = await getImdbSuggestionMeta(id); const meta = await getImdbSuggestionMeta(id);
if (meta) { if (meta) {
console.log({ console.log({
message: message:
@@ -586,14 +587,11 @@ async function getMetadata(type, fullId) {
return null; return null;
} }
async function getKitsuMeta(type, id) { async function getKitsuMeta(type, id) {
console.log({ message: "Fetching metadata from Kitsu", type, id }); console.log({ message: "Fetching metadata from Kitsu", type, id });
const url = `https://anime-kitsu.strem.fun/meta/${type}/${id}.json`; const url = `https://anime-kitsu.strem.fun/meta/${type}/${id}.json`;
console.log({ url }); console.log({ url });
const response = await fetch( const response = await fetch(url);
url
);
if (!response.ok) { if (!response.ok) {
let err = await response.text(); let err = await response.text();
throw new Error(err); throw new Error(err);
@@ -631,14 +629,18 @@ async function getTmdbMeta(type, id) {
if (!result) { if (!result) {
throw new Error("No results found in response"); throw new Error("No results found in response");
} }
console.log({ message: "Got data from TMDB", result });
if (!result?.title || !result?.release_date) { if (
(!result.name && !result.title) ||
(!result.release_date && !result.first_air_date)
) {
throw new Error("Either title or release date not found in result"); throw new Error("Either title or release date not found in result");
} }
return { return {
name: result.title, name: result.name || result.title,
year: result.release_date.split("-")[0], year: (result.release_date || result.first_air_date).split("-")[0],
}; };
} }
@@ -746,18 +748,17 @@ async function fetchFiles(fetchUrl, accessToken) {
async function fetchFile(fileId, accessToken) { async function fetchFile(fileId, accessToken) {
try { try {
const fetchUrl = new URL(API_ENDPOINTS.DRIVE_FETCH_FILE.replace("{fileId}", fileId)); const fetchUrl = new URL(
API_ENDPOINTS.DRIVE_FETCH_FILE.replace("{fileId}", fileId)
);
const searchParams = { const searchParams = {
supportsAllDrives: true, supportsAllDrives: true,
fields: "id,name,mimeType,size,videoMediaMetadata,fileExtension", fields: "id,name,mimeType,size,videoMediaMetadata,fileExtension",
} };
fetchUrl.search = new URLSearchParams(searchParams).toString(); fetchUrl.search = new URLSearchParams(searchParams).toString();
const response = await fetch( const response = await fetch(fetchUrl.toString(), {
fetchUrl.toString(), headers: { Authorization: `Bearer ${accessToken}` },
{ });
headers: { Authorization: `Bearer ${accessToken}` },
}
);
if (!response.ok) { if (!response.ok) {
let err = await response.text(); let err = await response.text();
@@ -869,7 +870,9 @@ async function handleRequest(request) {
if (url.pathname === "/manifest.json") { if (url.pathname === "/manifest.json") {
const manifest = MANIFEST; const manifest = MANIFEST;
manifest.catalogs = []; manifest.catalogs = [];
manifest.resources = [{ name: "stream", types: ["movie", "series", "anime"] }]; manifest.resources = [
{ name: "stream", types: ["movie", "series", "anime"] },
];
if (CONFIG.enableSearchCatalog) { if (CONFIG.enableSearchCatalog) {
manifest.catalogs.push({ manifest.catalogs.push({
type: "movie", type: "movie",
@@ -960,7 +963,7 @@ async function handleRequest(request) {
}); });
return null; return null;
} }
console.log({message: "Meta request", fileId, gdriveId}); console.log({ message: "Meta request", fileId, gdriveId });
const file = await fetchFile(gdriveId, accessToken); const file = await fetchFile(gdriveId, accessToken);
if (!file) { if (!file) {
console.error({ console.error({
@@ -969,7 +972,7 @@ async function handleRequest(request) {
}); });
return null; return null;
} }
console.log({message: "File fetched", file}); console.log({ message: "File fetched", file });
const parsedFile = parseFile(file); const parsedFile = parseFile(file);
return createJsonResponse({ return createJsonResponse({
meta: { meta: {
@@ -1071,10 +1074,16 @@ async function handleRequest(request) {
} }
const type = streamMatch[1]; const type = streamMatch[1];
const fullId = streamMatch[2]; const fullId = streamMatch[2];
let [season, episode] = fullId.split(":").slice(-2); let [season, episode] = fullId.split(":").slice(-2);
console.log({ message: "Stream request", type, fullId, season, episode }); console.log({
message: "Stream request",
type,
fullId,
season,
episode,
});
if (fullId.startsWith("kitsu")) { if (fullId.startsWith("kitsu")) {
season = 1; season = 1;
} }
@@ -1103,7 +1112,6 @@ async function handleRequest(request) {
return createJsonResponse({ return createJsonResponse({
streams: [createStream(parsedFile, accessToken)], streams: [createStream(parsedFile, accessToken)],
}); });
} }
const metadata = await getMetadata(type, fullId); const metadata = await getMetadata(type, fullId);
@@ -1217,7 +1225,23 @@ async function getStreams(streamRequest) {
files: results.files, files: results.files,
}); });
const parsedFiles = parseAndFilterFiles(results.files); const nameRegex = new RegExp(
"(?<![^ [(_\\-.])(" +
streamRequest.metadata.name
.replace(/[^\w\s]/g, "[^\\w\\s]?")
.replace(/ /g, "[ .\\-_]?") +
(streamRequest.type === "movie"
? `[ .\\-_]?${streamRequest.metadata.year}`
: "") +
")(?=[ \\)\\]_.-]|$)",
"i"
);
console.log({ message: "Name regex", nameRegex });
const parsedFiles = parseAndFilterFiles(
CONFIG.strictTitleCheck
? results.files.filter((file) => nameRegex.test(file.name))
: results.files
);
console.log( console.log(
results.files.length - parsedFiles.length === 0 results.files.length - parsedFiles.length === 0