Skip to content

Commit

Permalink
added own mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
AltuisticIsopod committed Sep 25, 2024
1 parent b4f2758 commit f6210b6
Show file tree
Hide file tree
Showing 13 changed files with 1,450 additions and 480 deletions.
30 changes: 0 additions & 30 deletions .env.example

This file was deleted.

Binary file modified bun.lockb
100644 → 100755
Binary file not shown.
761 changes: 579 additions & 182 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@auth/mongodb-adapter": "^2.4.0",
"@consumet/extensions": "^1.7.0",
"@ducanh2912/next-pwa": "^10.2.2",
"@headlessui/react": "^1.7.17",
"@next/bundle-analyzer": "^14.1.0",
Expand All @@ -22,15 +23,16 @@
"ioredis": "^5.3.2",
"media-icons": "^1.1.4",
"mongoose": "^8.1.1",
"next": "^14.1.0",
"next": "^14.2.13",
"next-auth": "^4.24.6",
"next-nprogress-bar": "^2.3.2",
"nextjs-toploader": "^1.6.4",
"react": "^18",
"react-dom": "^18",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-loading-skeleton": "^3.4.0",
"react-use-draggable-scroll": "^0.4.7",
"sonner": "^1.4.0",
"string-similarity": "^4.0.4",
"tailwindcss-animate": "^1.0.7",
"zustand": "^4.5.2"
},
Expand All @@ -39,7 +41,7 @@
"@types/react": "^18.2.52",
"autoprefixer": "^10.0.1",
"eslint": "^8",
"eslint-config-next": "14.0.4",
"eslint-config-next": "^14.2.13",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"typescript": "^5.3.3",
Expand Down
216 changes: 216 additions & 0 deletions src/actions/episode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
"use server";
import { ANIME } from "@consumet/extensions";
import { CombineEpisodeMeta } from "@/utils/EpisodeFunctions";
import { redis } from "@/lib/rediscache";
import { getMappings } from "./mappings";

const gogo = new ANIME.Gogoanime();
const zoro = new ANIME.Zoro();

export async function fetchGogoEpisodes(id) {
try {
const data = await gogo.fetchAnimeInfo(id);

return data?.episodes || [];
} catch (error) {
console.error("Error fetching gogoanime:", error.message);
return [];
}
}

export async function fetchZoroEpisodes(id) {
try {
const data = await zoro.fetchAnimeInfo(id);

return data?.episodes || [];
} catch (error) {
console.error("Error fetching zoro:", error.message);
return [];
}
}

async function fetchEpisodeMeta(id, available = false) {
try {
if (available) {
return null;
}
const res = await fetch(
`https://api.ani.zip/mappings?anilist_id=${id}`
);
const data = await res.json()
const episodesArray = Object.values(data?.episodes);

if (!episodesArray) {
return [];
}
return episodesArray;
} catch (error) {
console.error("Error fetching and processing meta:", error.message);
return [];
}
}

const fetchAndCacheData = async (id, meta, redis, cacheTime, refresh) => {
let mappings;
let subEpisodes = [];
let dubEpisodes = [];
let allepisodes = [];

if (id) {
mappings = await getMappings(id);
}

if (mappings) {
if (mappings.gogoanime && Object.keys(mappings.gogoanime).length >= 1) {
// Fetch sub episodes if available
if (
mappings?.gogoanime?.uncensored ||
mappings?.gogoanime?.sub ||
mappings?.gogoanime?.tv
) {
subEpisodes = await fetchGogoEpisodes(
mappings?.gogoanime?.uncensored ||
mappings.gogoanime.sub ||
mappings?.gogoanime?.tv
);
}

// Fetch dub episodes if available
if (mappings?.gogoanime?.dub) {
dubEpisodes = await fetchGogoEpisodes(mappings?.gogoanime?.dub);
}

if (subEpisodes?.length > 0 || dubEpisodes?.length > 0) {
allepisodes.push({
episodes: { sub: subEpisodes, dub: dubEpisodes },
providerId: "gogoanime",
consumet: true,
});
}
}
if (mappings?.zoro && Object.keys(mappings.zoro).length >= 1) {
let subEpisodes = [];

// Fetch sub episodes if available
if (
mappings?.zoro?.uncensored ||
mappings?.zoro?.sub ||
mappings?.zoro?.tv
) {
subEpisodes = await fetchZoroEpisodes(
mappings?.zoro?.uncensored
? mappings?.zoro?.uncensored
: mappings.zoro.sub
);
}
if (subEpisodes?.length > 0) {
allepisodes.push({
episodes: subEpisodes,
providerId: "zoro",
});
}
}
}
const cover = await fetchEpisodeMeta(id, !refresh)

// Check if redis is available
if (redis) {
if (allepisodes) {
await redis.setex(
`episode:${id}`,
cacheTime,
JSON.stringify(allepisodes)
);
}

let data = allepisodes;
if (refresh) {
if (cover && cover?.length > 0) {
try {
await redis.setex(`meta:${id}`, cacheTime, JSON.stringify(cover));
data = await CombineEpisodeMeta(allepisodes, cover);
} catch (error) {
console.error("Error serializing cover:", error.message);
}
} else if (meta) {
data = await CombineEpisodeMeta(allepisodes, JSON.parse(meta));
}
} else if (meta) {
data = await CombineEpisodeMeta(allepisodes, JSON.parse(meta));
}

return data;
} else {
console.error("Redis URL not provided. Caching not possible.");
return allepisodes;
}
};

export const getEpisodes = async (id, status, refresh = false) => {
let cacheTime = null;
if (status) {
cacheTime = 60 * 60 * 3;
} else {
cacheTime = 60 * 60 * 24 * 45;
}

let meta = null;
let cached;

if (redis) {
try {
// // Find keys matching the pattern "meta:*"
// const keys = await redis.keys("meta:*");

// // Delete keys matching the pattern "meta:*"
// if (keys.length > 0) {
// await redis.del(keys);
// console.log(`Deleted ${keys.length} keys matching the pattern "meta:*"`);
// }
meta = await redis.get(`meta:${id}`);
if (JSON.parse(meta)?.length === 0) {
await redis.del(`meta:${id}`);
console.log("deleted meta cache");
meta = null;
}
cached = await redis.get(`episode:${id}`);
if (JSON.parse(cached)?.length === 0) {
await redis.del(`episode:${id}`);
cached = null;
}
let data;
if (refresh) {
data = await fetchAndCacheData(id, meta, redis, cacheTime, refresh);
}
if (data?.length > 0) {
console.log("deleted cache");
return data;
}

console.log("using redis");
} catch (error) {
console.error("Error checking Redis cache:", error.message);
}
}

if (cached) {
try {
let cachedData = JSON.parse(cached);
if (meta) {
cachedData = await CombineEpisodeMeta(cachedData, JSON.parse(meta));
}
return cachedData;
} catch (error) {
console.error("Error parsing cached data:", error.message);
}
} else {
const fetchdata = await fetchAndCacheData(
id,
meta,
redis,
cacheTime,
!refresh
);
return fetchdata;
}
};
Loading

0 comments on commit f6210b6

Please sign in to comment.