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
/
tenshidotmoe.ts
225 lines (190 loc) · 6.96 KB
/
tenshidotmoe.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
import cheerio from "cheerio";
import {
AnimeExtractorConstructorOptions,
AnimeExtractorValidateResults,
AnimeExtractorSearchResult,
AnimeExtractorInfoResult,
AnimeExtractorDownloadResult,
AnimeExtractorModel,
} from "./model";
import { constants, functions } from "../../util";
export const config = {
baseUrl: "https://tenshi.moe",
searchUrl: (search: string) => `https://tenshi.moe/anime?q=${search}`,
animeRegex: /^https:\/\/tenshi\.moe\/anime\/.*/,
episodeRegex: /^https:\/\/tenshi\.moe\/anime\/.*\/.*/,
defaultHeaders() {
return {
"User-Agent": constants.http.userAgent,
Referer: this.baseUrl,
};
},
};
/**
* Tenshi.moe Extractor
*/
export default class TenshiDotMoe implements AnimeExtractorModel {
name = "Tenshi.moe";
options: AnimeExtractorConstructorOptions;
constructor(options: AnimeExtractorConstructorOptions) {
this.options = options;
}
/**
* Validate Tenshi.moe URL
* @param url Tenshi.moe 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;
}
/**
* Tenshi.moe Search
* @param terms Tenshi.moe 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[] = [];
$(".anime-loop li > a").each(function () {
const ele = $(this);
const title = ele.find(".text-primary");
const url = ele.attr("href");
const thumbnail = ele.find("img").attr("src");
if (url) {
let aired = [];
const year = ele.find(".year").text().trim();
if (year) aired.push(year);
const type = ele.find(".type").text().trim();
if (type) aired.push(type);
results.push({
title: title.text().trim(),
url: url.trim(),
thumbnail: thumbnail?.trim() || "",
air: aired.length ? aired.join(" ") : "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 Tenshi.moe URL
* @param url Tenshi.moe anime URL
*/
async getInfo(url: string) {
try {
url = functions.removeSearchParams(url);
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 totalEpisodes = +$(
".entry-episodes .badge.badge-secondary.align-top"
)
.text()
.trim();
if (totalEpisodes < 0 || !isFinite(totalEpisodes))
throw new Error("Could not estimated the episodes count.");
const result: AnimeExtractorInfoResult = {
title: $(".entry-header").text().trim(),
thumbnail: $("img.cover-image").attr("src") || "",
episodes: Array(totalEpisodes)
.fill(null)
.map((x, i) => ({
episode: `${i}`,
url: `${url}/${i}`,
})),
};
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 Tenshi.moe episode URL
* @param url Tenshi.moe episode URL
*/
async getDownloadLinks(url: string) {
try {
this.options.logger?.debug?.(
`(${this.name}) Download links requested for: ${url}`
);
const preData = await this.options.http.get(
functions.encodeURI(url),
{
headers: config.defaultHeaders(),
timeout: constants.http.maxTimeout,
}
);
const iframeUrl = preData.match(/<iframe src="(.*?)"/)?.[1];
if (!iframeUrl) {
this.options.logger?.error?.(
`(${this.name}) No embeds were found`
);
throw new Error("No embeds were found");
}
const data = await this.options.http.get(
functions.encodeURI(iframeUrl),
{
headers: Object.assign(config.defaultHeaders(), {
Referer: url,
}),
timeout: constants.http.maxTimeout,
}
);
const $ = cheerio.load(data);
this.options.logger?.debug?.(
`(${this.name}) DOM creation successful! (${url})`
);
const results: AnimeExtractorDownloadResult[] = [];
$("#player source").each(function () {
const ele = $(this);
const src = ele.attr("src");
const quality = ele.attr("title");
if (src) {
results.push({
quality: 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}`);
}
}
}