forked from mafintosh/torrent-mount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·86 lines (73 loc) · 2.36 KB
/
bin.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
#!/usr/bin/env node
var drive = require('./')
var readTorrent = require('read-torrent')
var log = require('single-line-log').stdout
var prettysize = require('prettysize')
var fuse = require('fuse-bindings')
var fs = require('fs')
var minimist = require('minimist')
var argv = minimist(process.argv.slice(2), {
alias: {mount: 'm', lazy: 'l'},
boolean: ['lazy']
})
if (!argv._[0]) {
console.error('Usage: torrent-mount torrent_or_magnet_link [directory]')
console.error()
console.error(' --mount, -m Drive letter')
console.error(' --lazy, -l Download files only if accessed')
console.error()
process.exit(1)
}
readTorrent(argv._[0], function (err, torrent, raw) {
if (err) {
console.error(err.message)
process.exit(2)
}
var mnt = ""
if (argv.mount) {
mnt = argv.mount + ":\\"
} else {
mnt = "T:\\"
}
var isLazy = argv.lazy
var engine = drive(raw, mnt, isLazy)
var hs = 0
engine.on('hotswap', function () {
hs++
})
log('Initializing swarm and verifying data...\n')
engine.on('mount', function (mnt) {
log('Mounted ' + engine.files.length + ' files, ' + prettysize(engine.torrent.length) + ' in ' + engine.torrent.name)
log.clear()
var notChoked = function (result, wire) {
return result + (wire.peerChoking ? 0 : 1)
}
var status = function () {
var down = prettysize(engine.swarm.downloaded)
var downSpeed = prettysize(engine.swarm.downloadSpeed()).replace('Bytes', 'b') + '/s'
var up = prettysize(engine.swarm.uploaded)
var upSpeed = prettysize(engine.swarm.uploadSpeed()).replace('Bytes', 'b') + '/s'
log(
'Connected to ' + engine.swarm.wires.reduce(notChoked, 0) + '/' + engine.swarm.wires.length + ' peers\n' +
'Downloaded ' + down + ' (' + downSpeed + ') with ' + hs + ' hotswaps\n' +
'Uploaded ' + up + ' (' + upSpeed + ')\n'
)
}
var interval = setInterval(status, 500)
status()
var exit = function () {
setTimeout(process.kill.bind(process, process.pid), 2000).unref()
process.removeListener('SIGINT', exit)
process.removeListener('SIGTERM', exit)
log('Shutting down...\n')
clearInterval(interval)
fuse.unmount(mnt, function () {
fs.rmdir(mnt, function () {
process.exit()
})
})
}
process.on('SIGINT', exit)
process.on('SIGTERM', exit)
})
})