-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtorrenter.js
executable file
·197 lines (181 loc) · 5.57 KB
/
torrenter.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
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
#!/usr/bin/env node
const got = require("hooman");
const download = require("./download");
const isUrl = require("url-regex");
const signale = require("signale");
const colors = require("colors/safe");
const { torrentIndexer, config } = require("./indexer");
const { prompt } = require("prompts");
const { orderBy } = require("lodash");
// check and notify about update every 24hrs
const updateCheck = require("./updateCheck");
const pkg = require("./package.json");
updateCheck(pkg);
let isCli = true;
const onCancel = prompt => {
signale.info("Aborted!");
process.exit();
};
const torrenter = async (query, path = config.path) => {
// Advanced search
try {
let torrents, answers, link, isTorrent;
if (!query) {
let { value } = await prompt(
[
{
type: "text",
name: "value",
message: "Search, magnet or url:",
validate: q => (q ? true : false)
}
],
{
onCancel
}
);
query = value;
}
if (query.startsWith("magnet:?")) {
isTorrent = true;
link = query;
} else if (isUrl({ exact: true }).test(query)) {
isTorrent = true;
link = query;
}
if (!isTorrent) {
console.log("");
let { type } = await prompt({
type: "select",
name: "type",
message: "Select type",
choices: [
{ title: "All", value: "all" },
{ title: "Movie", value: "movie" },
{ title: "Series", value: "series" },
{ title: "Anime", value: "anime" },
{ title: "Music", value: "music" }
],
initial: 0
});
signale.await(`Searching for ${query}`);
data = await torrentIndexer.search(query, type);
// orderBy seed and quality
torrents = orderBy(
data,
["score", "resolution", "seeders"],
["desc", "desc", "desc"]
);
signale.info(`Found ${torrents.length} torrents\n`);
if (torrents.length < 1) {
if (isCli) torrenter();
return;
}
answers = await prompt(
[
{
type: "select",
name: "torrent",
message: "Select a torrent:",
choices: torrents.map(item => {
item.description = colors.brightYellow(
"\n" +
Object.keys(item)
.map(key => {
if (key == "link" && item[key].length > 60) {
return `${key}: ${item[key]
.toString()
.substring(0, 55)}...`;
}
return `${key}: ${item[key]}`;
})
.join("\n") +
"\n"
);
item.value = item;
item.title =
item.fileName.length > 75
? `${item.fileName.substring(0, 70)}...`
: item.fileName;
item.title += colors.yellow(
`\n ╚ Size: ${item.size}, Seeders: ${
item.seeders
}, Verified: ${item.verified || "unknown"}`
);
return item;
}),
initial: 0
}
],
{
onCancel
}
);
if (!answers.torrent.link && answers.torrent.site) {
link = await torrentIndexer.torrent(answers.torrent.site);
} else {
link = answers.torrent.link;
}
}
if (isUrl({ exact: true }).test(link)) {
if (link.includes("itorrents.org/torrent/")) {
let hash = link.match(/torrent\/(.*?)\.torrent/);
link = hash[1];
} else {
let { body } = await got(link);
link = body;
}
}
const downloads = await download(link, path);
signale.success("File saved in " + colors.cyan(downloads.path));
if (isCli) torrenter();
return downloads;
} catch (error) {
signale.fatal(error);
if (isCli) torrenter();
}
};
// check if using cli
if (require.main === module) {
// App info
console.log(
colors.cyan(
"\n╔══════════════════════════════════════════════════════════╗"
)
);
console.log(
colors.cyan("║") +
colors.bold.yellow(
` torrenter (${pkg.version}) `
) +
colors.cyan("║")
);
console.log(
colors.cyan("╠══════════════════════════════════════════════════════════╣")
);
console.log(
colors.brightRed(" ♥ REPO ") +
colors.cyan("║") +
" https://github.com/sayem314/torrenter " +
colors.cyan("║")
);
console.log(
colors.cyan("╠══════════════════════════════════════════════════════════╣")
);
console.log(
colors.brightRed(" ♥ DONATE ") +
colors.cyan("║") +
" https://sayem.eu.org/donate " +
colors.cyan("║")
);
console.log(
colors.cyan("╚══════════════════════════════════════════════════════════╝")
);
// CLI
const args = process.argv.slice(2);
torrenter(args.join(" "));
} else {
// Module
isCli = false;
module.exports = torrenter;
}