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
/
animeparadise.ts
214 lines (182 loc) · 6.65 KB
/
animeparadise.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
import cheerio from "cheerio";
import {
AnimeExtractorConstructorOptions,
AnimeExtractorValidateResults,
AnimeExtractorSearchResult,
AnimeExtractorEpisodeResult,
AnimeExtractorInfoResult,
AnimeExtractorDownloadResult,
AnimeExtractorModel,
} from "./model";
import { constants, functions } from "../../util";
export const config = {
baseUrl: "https://animeparadise.org",
searchUrl: (search: string) =>
`https://animeparadise.org/search.php?query=${search}`,
animeRegex: /^https:\/\/animeparadise\.org\/anime\.php\?.*/,
episodeRegex: /^https:\/\/animeparadise\.org\/watch\.php\?.*/,
defaultHeaders() {
return {
"User-Agent": constants.http.userAgent,
Referer: this.baseUrl,
};
},
};
/**
* AnimeParadise.org Extractor
*/
export default class AnimeParadise implements AnimeExtractorModel {
name = "AnimeParadise.org";
options: AnimeExtractorConstructorOptions;
constructor(options: AnimeExtractorConstructorOptions) {
this.options = options;
}
/**
* Validate AnimeParadise.org URL
* @param url AnimeParadise.org 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;
}
/**
* AnimeParadise.org Search
* @param terms AnimeParadise.org 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[] = [];
$(".media").each(function () {
const ele = $(this);
const link = ele.find("a").first();
const title = link.text().trim();
const url = link.attr("href");
const thumbnail = ele.find("img").attr("src");
const tags = ele
.find(".tag")
.map(function () {
return `(${$(this).text().trim()})`;
})
.toArray();
if (url) {
results.push({
title: `${title}${
tags.length ? ` ${tags.join(" ")}` : ""
}`,
url: `${config.baseUrl}/${url.trim()}`,
thumbnail: thumbnail?.trim() || "",
air: "unknown",
});
}
});
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 AnimeParadise.org URL
* @param url AnimeParadise.org anime 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 episodes: AnimeExtractorEpisodeResult[] = [];
$(".container > .columns a.box").each(function () {
const ele = $(this);
const episode = ele.find(".title").text().trim();
const url = ele.attr("href");
if (url) {
episodes.push({
episode: episode || "unknown",
url: `${config.baseUrl}/${url}`,
});
}
});
const tags = $(".column > .tag")
.map(function () {
return `(${$(this).text().trim()})`;
})
.toArray();
const result: AnimeExtractorInfoResult = {
title: `${$(".column strong").text().trim()}${
tags.length ? ` ${tags.join(" ")}` : ""
}`,
thumbnail: $(".column.is-one-fifth 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 AnimeParadise.org episode URL
* @param url AnimeParadise.org 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})`
);
const results: AnimeExtractorDownloadResult[] = [];
$("video source").each(function () {
const ele = $(this);
const src = ele.attr("src");
if (src) {
results.push({
quality: "unknown",
url: src,
type: ["downloadable", "streamable"],
headers: config.defaultHeaders(),
});
}
});
return results;
} catch (err) {
this.options.logger?.error?.(
`(${this.name}) Failed to scrape: ${err?.message}`
);
throw new Error(`Failed to scrape: ${err?.message}`);
}
}
}