-
Notifications
You must be signed in to change notification settings - Fork 3
/
inkick.js
148 lines (122 loc) · 3.97 KB
/
inkick.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
#!/usr/bin/env node
// 'use strict'
const readline = require('readline');
const dns = require('dns');
const spawn = require('child_process').spawn;
const thepiratebay = require('thepiratebay');
// const commander = require('commander'); //add options for later use
const chalk = require('chalk');
const inquirer = require('inquirer');
const ora = require('ora');
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout, 0, 0)
const baseUrl = thepiratebay.baseUrl.substring(8)
dns.lookup(baseUrl, (err) => {
if (err && err.code === 'ENOTFOUND') {
console.log(chalk.dim('Site might be down or check your internet connection, exiting..'))
process.exit(1)
}
})
prompt()
function prompt() {
readline.cursorTo(process.stdout, 1, 0)
readline.clearScreenDown(process.stdout, 0, 0)
let torrentList = []
let magnetLink = ''
inquirer.prompt({
name: 'searchquery',
message: `Search torrents with KickFlix: (leave empty to search TOP25)\n`,
type: 'input',
default: () => undefined
})
.then((val) => {
const inputQuery = val.searchquery.length === 0 ? undefined : val.searchquery
const spinner = new ora()
spinner.start('\b')
const queryObj = {
category: 'video', // default - 'all' | 'all', 'audio', 'video', 'xxx',
filter: {
verified: true // default - false | Filter all VIP or trusted torrents
},
// page: 0, // default - 0 - 99
orderBy: 'seeds', // default - name, date, size, seeds, leeches
sortBy: 'desc'
}
inquirer.prompt({
name: 'torrents',
message: `\t${chalk.red('Seed')}\t${chalk.green('Leech')}\t${chalk.blue('Size')}\t${chalk.yellow('Date')}\t\t${chalk.cyan('Name')}`,
choices: () => {
return thepiratebay.search(inputQuery, queryObj)
.then(data => {
if (data.length === 0) {
spinner.warn()
return ['Y) No results for this query, try again...']
} else {
let ary = data.map((el, ind) => {
torrentList.push(el.magnetLink)
return torrentsString(el, ind)
})
spinner.succeed()
ary.push(`X)\tSearch again...`)
return ary
}
})
.catch(err => console.error(err))
},
type: 'list',
default: 'Something went wrong...',
pageSize: 10
})
.then((data) => {
const torStr = data.torrents
if (torStr.charAt(0) === 'X') {
delete this
prompt()
} else if (torStr.charAt(0) === 'Y') {
delete this
prompt()
} else {
magnetLink = torrentList[parseInt(torStr.charAt(0) + torStr.charAt(1)) || parseInt(torStr.charAt(0))]
reQuery(magnetLink)
}
})
.catch(err => console.error(err))
})
.catch(err => console.error(err))
}
function torrentsString(el, ind) {
return `${ind})\t${chalk.red(kify(el.seeders))}` +
`\t${chalk.green(kify(el.leechers))}` +
`\t${chalk.blue(trimString(el.size))}` +
`\t${chalk.yellow(el.uploadDate)}` +
`\t${el.name}`
}
function trimString(str) {
let stringDot = str.split(' ')
let numberString = stringDot[0].slice(0, 3)
if (str.endsWith('GiB')) {
return `${Number.parseFloat(numberString)}${str.slice(-3,-2)}B`
} else {
return `${Number.parseInt(numberString)}${str.slice(-3,-2)}B`
}
}
function kify(str) {
if (str.length > 4) {
return str.slice(0, 2) + 'K'
} else {
return str
}
}
function reQuery(answer) {
const vlc = spawn('node', ['./node_modules/peerflix/app.js', '-v', '-r', '-d', answer], {
stdio: 'inherit' // output streams in real time
})
vlc.on('error', err => {
console.error(err)
process.exit(0)
})
vlc.on('exit', data => {
console.log('Exiting gracefully.')
process.exit(0)
})
}