-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
111 lines (91 loc) · 2.98 KB
/
index.mjs
File metadata and controls
111 lines (91 loc) · 2.98 KB
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
import express from "express";
import anime from "./models/anime.mjs";
import mongoose from "mongoose";
import similarity from "similarity";
import "dotenv/config";
const MONGO_CONNECTION_STRING = process.env.MONGO_CONNECTION_STRING;
const PORT = process.env.PORT || 3000;
mongoose.connect(MONGO_CONNECTION_STRING);
const app = express();
app.get("/search", async (req, res) => {
const query = req.query.q.replaceAll("-", " ");
const regex = new RegExp('^' + query, 'i');
let japName = undefined
// /^attack on titan/i
if (!query) {
return res.status(400).send("Query is required");
}
const result = await anime.find({ $text: { $search: query } });
const otherNameResult = await anime.find({ other_name: regex });
for(let i = 0; i < otherNameResult.length; i++){
result.push(otherNameResult[i]);
if (typeof japName == "undefined"){
otherNameResult[i]["other_name"].map((otherName) => {
if (otherName.toLowerCase() == query){
japName = otherNameResult[i]["anime_name"]
}
})
}
}
console.log(japName);
const filtered_result = result.map((item) => {
return {
anime_name: item.anime_name,
// gogo_id: item.gogo_id,
// cover: item.cover,
// type: item.type,
// score: item.score,
// total_episodes: item.episodes.length,
other_name: item.other_name
};
});
const sorted_filter_result = filtered_result.sort((a, b) => {
if (typeof japName == "undefined"){
let similartyA = similarity(a.anime_name, query, {sensitive: false});
let similartyB = similarity(b.anime_name, query, {sensitive: false});
return similartyB - similartyA;
} else {
let similartyA = similarity(a.anime_name, japName, {sensitive: false});
let similartyB = similarity(b.anime_name, japName, {sensitive: false});
return similartyB - similartyA;
}
});
res.json(sorted_filter_result);
});
app.get("/detail", async (req, res) => {
const gogo_id = req.query.gogo_id;
const result = JSON.parse(
JSON.stringify(await anime.findOne({ gogo_id: gogo_id }))
);
if (result === null) {
return res.status(404).send("Anime not found");
}
const filtered_result = {
_id: result._id,
anime_name: result.anime_name,
gogo_id: result.gogo_id,
genres: result.genres,
cover: result.cover,
type: result.type,
plot_summary: result.plot_summary,
release: result.release,
status: result.status,
other_name: result.other_name,
trailer: result.trailer,
score: result.score,
url: result.url,
};
res.json(filtered_result);
});
app.get("/get_episodes", async (req, res) => {
const gogo_id = req.query.gogo_id;
const result = await anime.findOne({ gogo_id: gogo_id });
if (result === null) {
return res.status(404).send("Anime not found");
}
const filtered_result = result.episodes;
res.json(filtered_result);
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});