-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorrentsearch.js
executable file
·124 lines (100 loc) · 4.09 KB
/
torrentsearch.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
#!/usr/bin/env node
const yargs = require('yargs')
const chalk = require('chalk')
const commandExistsSync = require('command-exists').sync
const fs = require('fs')
const trackersearch = require('./lib/trackersearch.js')
const prompts = require('prompts')
const version = '0.0.9'
console.log("Now starting TorrentSearch", version)
// Define a couple logging types
const error = (str) => console.log(chalk.red(str))
const verbose = (str) => console.log(str)
const warning = (str) => console.log(str)
// Check if aria2c is available on the system
if (commandExistsSync('aria2c')) {
//verbose("Aria2c is installed!")
} else {
error("Sorry, you don't have aria2c installed. Visit https://github.com/aria2/aria2/releases/latest to get it.")
process.exit(1)
}
// Attempt to open .torrentsearch in home directory
const configFilePath = require('os').homedir() + '/.torrentsearch';
try {
const configurationDataRaw = fs.readFileSync(configFilePath)
//verbose("Successfully loaded configuration from " + configFilePath)
const configurationData = JSON.parse(configurationDataRaw)
//verbose("Download directory configured: " + configurationData.downloadDirectory)
} catch (e) {
warning(`Config file at ${configFilePath} could not be found. Beginning initial setup...`)
// TODO: Add validation (e.g. validate: fs.lstatSync(directory).isDirectory())
const func = (async () => {
const response = await prompts({
type: 'text',
name: 'directory',
message: 'Where do you want retrieved .torrent files to be saved to?'
});
verbose("User entered: " + response.directory);
// Save above directory to config file
const torrentSearchInitialConfig = {
downloadDirectory: response.directory
}
fs.writeFileSync(configFilePath, JSON.stringify(torrentSearchInitialConfig))
verbose("Initial configuration written to " + configFilePath)
})();
}
const searchTracker = (tracker, query) => {
query = query.slice(1)
.join(' ')
console.log(`Now searching on ${tracker} for "${query}"`)
trackersearch(tracker, query, (error, response) => {
if (error){
console.log("Error:", error)
} else {
console.log(response.length + " results retrieved from " + tracker + " (showing top five): ")
const outputResults = (amount, resultsArray) => {
let displayed = 1
for (let result of resultsArray){
console.log(`${displayed}. ${result.description} (${result.size}) (S: ${result.seeders} L: ${result.leechers})`)
if (displayed++ == amount-1) break
}
}
// outputResults(6, response)
// outputResultsNew(response)
(async (response) => {
let transformResponse = []
for (let result of response.slice(0,5)){
let transformedResult = {}
transformedResult.title = result.description.slice(0,50) + ' (' + result.size + ') '
transformedResult.title += `S: ${result.seeders} L: ${result.leechers}`
transformedResult.value = result.magnetLink
transformResponse.push(transformedResult)
}
const selectedTorrent = await prompts({
type: 'select',
name: 'torrent',
message: 'The following torrents were found:',
choices: transformResponse
});
verbose("User entered: " + selectedTorrent.torrent);
//return selectedTorrent
})(response);
}
})
}
yargs.version(version)
.scriptName('Torrentsearch')
.usage('$0 [search term]')
.help()
.argv
yargs.command({
command: 'piratebay',
describe: 'Search the Pirate Bay',
demandOption: true,
type: 'string'
,
handler(argv){
searchTracker('piratebay', argv._)
}
})
yargs.parse()