-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-pirate-download.js
110 lines (100 loc) · 3.16 KB
/
node-pirate-download.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
var request = require('request');
var cheerio = require('cheerio');
var Table = require('cli-table');
var colors = require('colors');
var inquirer = require("inquirer");
var _ = require('lodash');
var program = require('commander');
var torrentStream = require('torrent-stream');
var ProgressBar = require('progress');
var homedir = require('homedir');
function bytesToSize(bytes, withBytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if(withBytes == true){
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
} else {
return parseInt(bytes / Math.pow(1024, i));
}
};
program
.usage('[options]')
.description('Download torrent or magnet file')
.option('-i, --id, <n>', 'Download by ID')
.parse(process.argv);
var argv = program;
var idPrompt = {
type: 'input',
name: 'id',
message: 'Please enter a id to download',
};
var prompts = [];
if(_.isUndefined(argv.id)){
prompts.push(idPrompt);
}
if(prompts.length){
inquirer.prompt(prompts, function(answers){
url = 'https://thepiratebay.se';
request(url, function(error, response, html){
var realUrl = response.socket._httpMessage._header.split('\n')[1].replace('referer:', '').replace(/\r/, '');
Download(_.assign(argv,answers), realUrl)
});
});
}else{
url = 'https://thepiratebay.se';
request(url, function(error, response, html){
var realUrl = response.socket._httpMessage._header.split('\n')[1].replace('referer:', '').replace(/\r/, '');
Download(argv, realUrl);
});
}
function Download(argv, realUrl){
if(_.includes(realUrl, 'host') && !_.includes(realUrl, 'thepiratebay.se')){
return console.log('site down'.red);
} else if(_.includes(realUrl, 'thepiratebay.se')) {
realUrl = 'https://thepiratebay.se';
}
var url = realUrl + '/torrent/'+ argv.id + '/';
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
$('#detailsouterframe').filter(function(){
var data = $(this);
var magnetUri = data.find(".download").children('a').attr('href');
var engine = torrentStream(magnetUri, {
connections: 100,
path: homedir() + '/Downloads'
});
var x = 0;
var filesize = [];
var stream = [];
engine.on('ready', function() {
engine.files.forEach(function(file) {
stream[x] = file.createReadStream();
filesize[x] = stream.length;
x = x + 1;
});
bar = new ProgressBar(engine.torrent.name +" filesize:"+ bytesToSize(eval(engine.torrent.length), true) +' [:bar] :percent :etas', {
complete: '='.green,
incomplete: ' ',
clear: false,
width: 20,
total: engine.torrent.length
});
});
engine.on('download', function(piece){
if(typeof oldvalue != "undefined"){
engine.swarm.downloaded = engine.swarm.downloaded - oldvalue;
}
if(!bar.complete){
bar.tick(engine.swarm.downloaded);
}
oldvalue = engine.swarm.downloaded;
});
engine.on('idle', function(){
process.exit()
});
})
}
})
}