Skip to content

Commit a0fcabe

Browse files
committed
fix: multi clones supported
1 parent cb24c9d commit a0fcabe

File tree

5 files changed

+83
-40
lines changed

5 files changed

+83
-40
lines changed

bun.lockb

63.7 KB
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aniwatch-api",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "",
55
"main": "server.js",
66
"scripts": {

src/controllers/aniwatch/episodeServerSourcesController.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import createHttpError from "http-errors";
33
import { type RequestHandler } from "express";
44
import { type CheerioAPI, load } from "cheerio";
55
import { scrapeAnimeEpisodeSources } from "../../scrapers/aniwatch/scrapers";
6-
import { URL_fn, USER_AGENT_HEADER } from "../../utils/aniwatch/constants";
6+
import { URL_fn } from "../../utils/aniwatch/constants";
7+
import { headers } from "../../config/headers";
78
import { type AnimeServers, Servers } from "../../types/aniwatch/anime";
89

910
type AnilistID = number | null;
@@ -41,7 +42,7 @@ const getAnimeEpisodeSourcesInfo: RequestHandler = async (req, res) => {
4142
axios.get(animeURL, {
4243
headers: {
4344
Referer: URLs.BASE,
44-
"User-Agent": USER_AGENT_HEADER,
45+
"User-Agent": headers.USER_AGENT_HEADER,
4546
"X-Requested-With": "XMLHttpRequest",
4647
},
4748
}),

src/utils/aniwatch/constants.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,49 @@ type AniWatchConfig = {
1010
}
1111

1212
const aniwatch: AnimeWebsiteConfig = websites_collection["AniWatch"];
13+
// storing initial base link
14+
let aniwatch_base = aniwatch.BASE;
15+
// array of clones
16+
let clones_array: string[] = [];
17+
clones_array.push(aniwatch_base);
1318

14-
// TODO: use while loop find best responsive site or clone site
15-
const aniwatch_base = aniwatch.BASE;
19+
if (aniwatch.CLONES) {
20+
const aniwatch_clones: Record<string, string[]> = aniwatch.CLONES;
1621

17-
const aniwatchObj: AniWatchConfig = {
18-
BASE: aniwatch_base,
19-
HOME: `${aniwatch_base}/home`,
20-
SEARCH: `${aniwatch_base}/search`,
21-
GENRE: `${aniwatch_base}/genre`,
22-
AJAX: `${aniwatch_base}/ajax`,
22+
for (const key in aniwatch_clones) {
23+
if (Object.prototype.hasOwnProperty.call(aniwatch_clones, key)) {
24+
const values: string[] = aniwatch_clones[key];
25+
clones_array.push(...values);
26+
}
27+
}
28+
}
29+
30+
// Testing
31+
// console.log(clones_array);
32+
33+
// make new aniwatchobj using new aniwatch_base
34+
const makeAniWatchObj = (aniwatch_base: string): AniWatchConfig => {
35+
// Testing
36+
// console.log(aniwatch_base);
37+
return {
38+
BASE: aniwatch_base,
39+
HOME: `${aniwatch_base}/home`,
40+
SEARCH: `${aniwatch_base}/search`,
41+
GENRE: `${aniwatch_base}/genre`,
42+
AJAX: `${aniwatch_base}/ajax`,
43+
}
2344
}
2445

25-
// You can add SIMILAR SITES here and append if-else condition in URL_fn()
26-
const URL_fn = async () => {
46+
// return fn
47+
const URL_fn = async (): Promise<AniWatchConfig> => {
2748
try {
28-
const reachable = await isSiteReachable(aniwatch_base);
29-
if (reachable) {
30-
// aniwatch.to is not working
31-
return aniwatchObj;
32-
} else {
33-
return aniwatchObj;
49+
for (const url of clones_array) {
50+
if (await isSiteReachable(url as string)) {
51+
aniwatch_base = url;
52+
break;
53+
}
3454
}
55+
return makeAniWatchObj(aniwatch_base as string);
3556
} catch (error) {
3657
console.error("Error occurred in both sites:", error);
3758
throw error; // Rethrow the error to handle it outside

src/utils/gogoanime/constants.ts

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,52 @@ type GogoAnimeConfig = {
1313
}
1414

1515
const gogoanime: AnimeWebsiteConfig = websites_collection["GogoAnime"];
16-
const gogoanime_base = gogoanime.BASE;
17-
18-
// TODO: use while loop find best responsive site or clone site
19-
// gogoanime3.co
20-
const websiteObj: GogoAnimeConfig = {
21-
BASE: gogoanime.BASE,
22-
SEARCH: `${gogoanime_base}/search.html`,
23-
CATEGORY: `${gogoanime_base}/category/`,
24-
MOVIES: `${gogoanime_base}/anime-movies.html`,
25-
POPULAR: `${gogoanime_base}/popular.html`,
26-
NEW_SEASON: `${gogoanime_base}/new-season.html`,
27-
SEASONS: `${gogoanime_base}/sub-category/`,
28-
AJAX: "https://ajax.gogocdn.net/ajax",
16+
// storing initial base link
17+
let gogoanime_base = gogoanime.BASE;
18+
// array of clones
19+
let clones_array: string[] = [];
20+
clones_array.push(gogoanime_base);
21+
22+
if (gogoanime.CLONES) {
23+
const gogoanime_clones: Record<string, string[]> = gogoanime.CLONES;
24+
25+
for (const key in gogoanime_clones) {
26+
if (Object.prototype.hasOwnProperty.call(gogoanime_clones, key)) {
27+
const values: string[] = gogoanime_clones[key];
28+
clones_array.push(...values);
29+
}
30+
}
31+
}
32+
33+
// Testing
34+
// console.log(clones_array);
35+
36+
// make new gogoanimeobj using new gogoanime_base
37+
const makeGogoAnimeObj = (gogoanime_base: string): GogoAnimeConfig => {
38+
// Testing
39+
// console.log(gogoanime_base);
40+
return {
41+
BASE: gogoanime.BASE,
42+
SEARCH: `${gogoanime_base}/search.html`,
43+
CATEGORY: `${gogoanime_base}/category/`,
44+
MOVIES: `${gogoanime_base}/anime-movies.html`,
45+
POPULAR: `${gogoanime_base}/popular.html`,
46+
NEW_SEASON: `${gogoanime_base}/new-season.html`,
47+
SEASONS: `${gogoanime_base}/sub-category/`,
48+
AJAX: "https://ajax.gogocdn.net/ajax",
49+
}
2950
}
3051

31-
// You can add SIMILAR SITES here and append if-else condition in URL_fn()
32-
const URL_fn = async () => {
52+
// return fn
53+
const URL_fn = async (): Promise<GogoAnimeConfig> => {
3354
try {
34-
const reachable = await isSiteReachable(gogoanime.BASE);
35-
if (reachable) {
36-
// aniwatch.to is not working
37-
return websiteObj;
38-
} else {
39-
return websiteObj;
55+
for (const url of clones_array) {
56+
if (await isSiteReachable(url as string)) {
57+
gogoanime_base = url;
58+
break;
59+
}
4060
}
61+
return makeGogoAnimeObj(gogoanime_base as string);
4162
} catch (error) {
4263
console.error("Error occurred in both sites:", error);
4364
throw error; // Rethrow the error to handle it outside

0 commit comments

Comments
 (0)