-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
100 lines (87 loc) · 2.45 KB
/
index.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
import dotenv from "dotenv";
dotenv.config();
import { input, rawlist, select } from "@inquirer/prompts";
import figlet from "figlet";
import {
getEpisodes,
getServers,
getSources,
searchAnime,
} from "./src/methods";
import fs from "fs";
import http from "http";
import path from "path";
import ejs from "ejs";
import chalk from "chalk";
import cProcess from "child_process";
if (process.argv.length <= 2) {
console.log(figlet.textSync("aniCLI", "Alligator"));
}
// server
const startServer = async (port: number, src: string) => {
const server = http.createServer((req, res) => {
if (req.url === "/") {
const htmlTemplate = fs.readFileSync(
path.join(__dirname, "/src/views/index.ejs"),
"utf-8"
);
const renderedHtml = ejs.render(htmlTemplate, { srcUrl: src });
res.writeHead(200, { "Content-Type": "text/html" });
res.end(renderedHtml);
}
});
server.listen(port, () => {
console.log(
chalk.greenBright(`Playback started on http://localhost:${port}/`)
);
return cProcess.exec(`start http://localhost:${port}`);
});
};
// Your CLI logic goes here...
async function main() {
console.log("\n\n");
const searchTerm = await input({
message: "Search your anime: ",
});
const searchData = await searchAnime(searchTerm);
const selectedAnime = await rawlist({
message: "Select your anime",
choices: searchData.Page.media.map((a) => ({
value: a.id,
name: a.title.english || a.title.native,
})),
});
const episodes: Episode[] = await getEpisodes(selectedAnime as number);
const selectedEpNum = await input({
message: "Episode number: ",
validate: (val) => {
if (Number(val) > episodes.length) return "Episode doesn't exists !";
return true;
},
});
const episode = episodes.find((e) => e.number === Number(selectedEpNum));
const subOrDub: string = await select({
message: "Sub or Dub ?",
choices: [
{
name: "sub",
value: "sub",
},
{
name: "dub",
value: "dub",
},
],
});
const servers: IServers = await getServers(episode!.episodeId);
const selectedServer = await select({
message: "Choose a server: ",
choices: servers[subOrDub as keyof IServers].map((s) => ({
name: s.serverName,
value: s.serverId,
})),
});
const sources: Sources = await getSources(selectedServer, episode!.episodeId);
startServer(5000, sources.sources[0].url);
}
main();