This repository has been archived by the owner on Oct 15, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gogoanime.ts
256 lines (215 loc) · 8.08 KB
/
gogoanime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import cheerio from "cheerio";
import {
AnimeExtractorConstructorOptions,
AnimeExtractorValidateResults,
AnimeExtractorSearchResult,
AnimeExtractorEpisodeResult,
AnimeExtractorInfoResult,
AnimeExtractorDownloadResult,
AnimeExtractorModel,
} from "./model";
import { getExtractor } from "../sources";
import { constants, functions } from "../../util";
export const config = {
baseUrl: "https://gogoanime.pe",
searchUrl: (search: string) =>
`https://gogoanime.pe/search.html?keyword=${search}`,
episodesUrl: (start: string, end: string, id: string) =>
`https://ajax.gogo-load.com/ajax/load-list-episode?ep_start=${start}&ep_end=${end}&id=${id}`,
animeRegex: /^https:\/\/www1.gogoanime.ai\/category\/.*/,
episodeRegex: /^https:\/\/www1.gogoanime.ai\/.*-episode-\w+$/,
defaultHeaders() {
return {
"User-Agent": constants.http.userAgent,
Referer: this.baseUrl,
};
},
};
/**
* Gogoanime Extractor
*/
export default class Gogoanime implements AnimeExtractorModel {
name = "Gogoanime";
options: AnimeExtractorConstructorOptions;
constructor(options: AnimeExtractorConstructorOptions) {
this.options = options;
}
/**
* Validate Gogoanime URL
* @param url Gogoanime URL
*/
validateURL(url: string) {
let result: AnimeExtractorValidateResults = false;
if (config.animeRegex.test(url)) result = "anime_url";
else if (config.episodeRegex.test(url)) result = "episode_url";
return result;
}
/**
* Gogoanime Search
* @param terms Search term
*/
async search(terms: string) {
try {
this.options.logger?.debug?.(
`(${this.name}) Search terms: ${terms}`
);
const url = config.searchUrl(terms);
this.options.logger?.debug?.(`(${this.name}) Search URL: ${url}`);
const data = await this.options.http.get(functions.encodeURI(url), {
headers: config.defaultHeaders(),
timeout: constants.http.maxTimeout,
});
const $ = cheerio.load(data);
this.options.logger?.debug?.(
`(${this.name}) DOM creation successful! (${url})`
);
const results: AnimeExtractorSearchResult[] = [];
$(".items li").each(function () {
const ele = $(this);
const title = ele.find(".name a");
const url = title.attr("href");
const thumbnail = ele.find(".img img").attr("src");
const air = ele.find(".released");
if (url) {
const year =
$(air[0]).text().replace("Released:", "").trim() ||
"unknown";
results.push({
title: title.text().trim(),
url: `${config.baseUrl}${url.trim()}`,
thumbnail: thumbnail?.trim() || "",
air: year,
});
}
});
return results;
} catch (err) {
this.options.logger?.error?.(
`(${this.name}) Failed to scrape: ${err?.message}`
);
throw new Error(`Failed to scrape: ${err?.message}`);
}
}
/**
* Get episode URLs from Gogoanime URL
* @param url Gogoanime URL
*/
async getInfo(url: string) {
try {
this.options.logger?.debug?.(
`(${this.name}) Episode links requested for: ${url}`
);
const data = await this.options.http.get(functions.encodeURI(url), {
headers: config.defaultHeaders(),
timeout: constants.http.maxTimeout,
});
const $ = cheerio.load(data);
this.options.logger?.debug?.(
`(${this.name}) DOM creation successful! (${url})`
);
const eps = $("#episode_page a");
const epStart = <string>eps.first().attr("ep_start"),
epEnd = <string>eps.last().attr("ep_end");
const isSgtE = +epStart > +epEnd;
const episodesUrl = config.episodesUrl(
!isSgtE ? epStart : epEnd,
isSgtE ? epStart : epEnd,
<string>$("input#movie_id").val()
);
const episodesData = await this.options.http.get(
functions.encodeURI(episodesUrl),
{
headers: config.defaultHeaders(),
timeout: constants.http.maxTimeout,
}
);
const e$ = cheerio.load(episodesData);
this.options.logger?.debug?.(
`(${this.name}) DOM creation successful! (${url})`
);
const episodes: AnimeExtractorEpisodeResult[] = [];
e$("#episode_related a").each(function () {
const ele = e$(this);
const episode = ele.find(".name");
const url = ele.attr("href");
if (url) {
episodes.push({
episode: episode.text().replace("EP", "").trim(),
url: `${config.baseUrl}${url.trim()}`,
});
}
});
const result: AnimeExtractorInfoResult = {
title: $(".anime_info_body_bg h1").text().trim(),
thumbnail: $(".anime_info_body_bg img").attr("src") || "",
episodes,
};
return result;
} catch (err) {
this.options.logger?.error?.(
`(${this.name}) Failed to scrape: ${err?.message}`
);
throw new Error(`Failed to scrape: ${err?.message}`);
}
}
/**
* Get download URLs from Gogoanime episode URL
* @param url Episode URL
*/
async getDownloadLinks(url: string) {
try {
this.options.logger?.debug?.(
`(${this.name}) Download links requested for: ${url}`
);
const data = await this.options.http.get(functions.encodeURI(url), {
headers: config.defaultHeaders(),
timeout: constants.http.maxTimeout,
});
const $ = cheerio.load(data);
this.options.logger?.debug?.(
`(${this.name}) DOM creation successful! (${url})`
);
let iframeUrl = $(".play-video iframe").attr("src");
if (!iframeUrl)
throw new Error(`Could not find download urls for: ${url}`);
const results: AnimeExtractorDownloadResult[] = [];
if (!iframeUrl.startsWith("http")) iframeUrl = `https:${iframeUrl}`;
results.push({
quality: "unknown",
url: iframeUrl,
type: ["external_embed"],
headers: config.defaultHeaders(),
});
const source = $(".anime_muti_link a")
.map(function () {
return $(this).attr("data-video");
})
.toArray()
.filter((x) => x)
.map((x) => {
if (!x.startsWith("http")) x = `https:${x}`;
return x;
});
for (const src of source) {
const extractor = getExtractor(src);
if (extractor) {
const links = await extractor
.fetch(src, {
http: this.options.http,
headers: {
Referer: url,
},
})
.catch(() => null);
if (links) results.push(...links);
}
}
return results;
} catch (err) {
this.options.logger?.error?.(
`(${this.name}) Failed to scrape: ${err?.message}`
);
throw new Error(`Failed to scrape: ${err?.message}`);
}
}
}