-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
149 lines (137 loc) · 4.44 KB
/
index.js
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
import express from "express";
import fs from "fs";
import https from "https";
import { MongoClient } from "mongodb";
const app = express();
const PORT = 5000;
// app.use(express.json());
var jsonParser = express.json();
var urlencodedParser = express.urlencoded({ extended: false });
// Obtaining the username and password for DB access
const data = fs
.readFileSync("passwords.txt", (err) => {
if (err) {
process.exit(0);
}
})
.toString()
.split(":");
let uname = data[0];
let pwd = data[1];
let mongoURL = `mongodb+srv://${uname}:${pwd}@episodestochapters.l8qrtlg.mongodb.net/?retryWrites=true&w=majority`;
const dbClient = new MongoClient(mongoURL);
// ALL ROUTING LOGIC
// episodetottileAPI
app.route("/episodetitle/:ep").get(async (request, response, next) => {
// DB CONNECTION LOGIC HERE !
try {
await dbClient.connect();
console.log(`Connected to cluster 'episodestochapters' successfully !`);
const opdb = await dbClient.db("animetomanga");
console.log(`Connected to DB 'animetomanga' successfully !`);
const e2title = await opdb.collection("episodetotitle");
const cursr = await e2title.find({ _id: "onepiecetitles" });
// The below variable is used outside the try block - hence var function scope is necessary
var map = await cursr.toArray();
console.log(
`Succesfully retrieved the episode to title maps from collection 'episodetotitle' !`
);
dbClient.close().then(() => {
console.log("Connection to DB closed successfully !");
});
} catch (err) {
console.error(err);
sslServer.close(() => {
console.log(`Connection to the server at port ${PORT} has been closed !`);
});
}
let epMap = map[0].content;
try {
let mapKeys = Object.keys(epMap);
if (mapKeys.includes(request.params.ep)) {
response.json({
episode: request.params.ep,
title: epMap[request.params.ep],
});
} else {
response.json({ error: `No entry for episode ${request.params.ep}` });
}
} catch (err) {
response.json({ error: err });
}
});
// episodetomangachaptersAPI
app.route("/episodechapters/:ep").get(async (request, response, next) => {
// DB CONNECTION LOGIC HERE !
try {
await dbClient.connect();
console.log(`Connected to cluster 'episodestochapters' successfully !`);
const opdb = await dbClient.db("animetomanga");
console.log(`Connected to DB 'animetomanga' successfully !`);
const e2chap = await opdb.collection("episodetochapter");
const cursr = await e2chap.find({ _id: "onepiecemap" });
// The below variable is used outside the try block - hence var function scope is necessary
var map = await cursr.toArray();
console.log(
`Succesfully retrieved the episode to manga chapter list map from collection 'episodetochapter' !`
);
dbClient.close().then(() => {
console.log("Connection to DB closed successfully !");
});
} catch (err) {
console.error(err);
sslServer.close(() => {
console.log(`Connection to the server at port ${PORT} has been closed !`);
});
}
let epMap = map[0].content;
try {
let mapKeys = Object.keys(epMap);
if (mapKeys.includes(request.params.ep)) {
response.json({
episode: request.params.ep,
chapters: epMap[request.params.ep],
});
} else {
response.json({ error: `No entry for episode ${request.params.ep}` });
}
} catch (err) {
response.json({ error: err });
}
});
app.route("/api").post(jsonParser, (request, response) => {
try {
let serviceName = request.body.serviceName;
let episodeNumber = request.body.episodeNumber;
if (!serviceName) {
throw new Error("Missing parameter 'serviceName'");
}
if (!episodeNumber) {
throw new Error("Missing parameter 'episodeNumber'");
}
// permanent redirect code is 301
response.redirect(301, `/${serviceName}/${episodeNumber}`);
} catch (err) {
response.status = 400;
response.json({
error: `400 : Bad request => ${err}`,
});
}
});
app.route("/a").get((req, res) => {
console.log("Hello!");
res.send("Hello!");
});
// APP run on an SSL SERVER
const sslServer = https.createServer(
{
key: fs.readFileSync("./cert/key.pem"),
cert: fs.readFileSync("./cert/cert.pem"),
},
app
);
sslServer.listen(PORT, async () => {
console.log(
`=============================The One Piece Scraper API service is running live on port: ${PORT} ============================= \n`
);
});