-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.js
94 lines (86 loc) · 2.35 KB
/
main.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
var sys = require('sys'), torrent = require('./torrent');
String.prototype.toBinary = function(){
var ret = '';
for (var i = 0; i < this.length; i++) {
ret += this.charCodeAt(i).toString(2);
}
return ret;
}
Object.size = function(obj){
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key))
size++;
}
return size;
};
//here's antimatter15's implementation, i think they're better
//because at least it works with a fairly fundamental concept
//that binaryToString(stringToBinary(x)) == x;
//logically it does not work with unicodey things
function stringToBinary(bytes){
var i, b = [];
for (i = 0; i < bytes.length; i++) {
b[i] = bytes.charCodeAt(i) & 0xff;
}
return b.map(function(v){
return v.toString(2)
}).join('')
/*.split('').map(function(v){
return +v;
});*/
};
function binaryToString(bytes){ //as a string
if (bytes % 7)
throw "poop";
for (var ret = '', l = bytes.length, i = 0; i < l; i += 7) {
ret += String.fromCharCode(parseInt(bytes.substr(i, 7), 2))
}
return ret;
}
String.prototype.fromBinary = function(){
var ret = '';
for (var i = 0; i < this.length; i++) {
ret += String.fromCharCode(parseInt(this.charAt(i)));
}
return ret;
}
function parseArgs(args){
var result = {
destDir: '.'
}, torrentFiles = [], i, argLen, arg;
for (i = 0, argLen = args.length; i < argLen; i += 1) {
arg = args[i];
if (arg.length == 0) {
throw "Empty argument";
}
if (arg.charAt(0) == '-') {
if (arg === '--destDir') {
result.destDir = args[i + 1];
i += 1;
}
else {
throw "Unknown flag " + arg;
}
}
else {
torrentFiles.push(arg);
}
}
result.files = torrentFiles;
return result;
}
function main(){
var args = parseArgs(process.argv.slice(2)), torrentFiles = args.files, i, iLen, file, aTorrent;
if (torrentFiles.length == 0) {
throw "No torrent files specified.";
}
else {
for (i = 0, iLen = torrentFiles.length; i < iLen; i += 1) {
file = torrentFiles[i];
aTorrent = torrent.create(file, args.destDir);
aTorrent.start();
}
}
}
main();