-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
176 lines (146 loc) · 4.1 KB
/
app.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var crypto = require('crypto');
var spawn = require('child_process').spawn;
var downs = {};
var sockets = {};
var wait = 1000;
var lastsend = 0;
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// socket IO
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
sockets[socket.id] = socket;
socket.emit('reload', JSON.stringify(downs));
socket.on('disconect', function(){
delete sockets[socket.id];
});
});
// download methods
addDown = function(url) {
proc = spawn('python', [__dirname + '/lib/bittornado/btdownloadheadless.py', '--url', url], {
cwd : process.cwd()
});
proc.stdout.setEncoding('utf8');
var down = new Object();
down.url = url;
down.startedAt = new Date().toString();
down.percent = '0.0';
var key = crypto.createHash('sha1').update(url).digest(encoding='hex');
downs[key] = down;
downs[key].downrate="-";
downs[key].uprate="-";
downs[key].timeleft="-";
downs[key].name="";
downs[key].kill=false;
// 新しいデータをブロードキャスト
sendData('reload', JSON.stringify(downs), true);
proc.stdout.on('data', function(data){
// 名称取得
if(/^saving: +(.*)/m.test(data)){
if(downs[key]) {
downs[key].name = RegExp.$1;
}
}
// download rateを取得
if(/.*download rate: +([ 0-9kB/m\.]+s)/m.test(data)){
if(downs[key]) {
downs[key].downrate = RegExp.$1;
}
}
// time leftを取得
if(/.*upload rate: +([ 0-9kB/m\.]+s)/m.test(data)){
if(downs[key]) {
downs[key].uprate = RegExp.$1;
}
}
// time leftを取得
if(/.*time left: +([0-9][ 0-9minsechour]+)/m.test(data)){
if(downs[key]) {
downs[key].timeleft = RegExp.$1;
}
}
// percent doneを取得
if(/.*percent done: +([\.\d]+)/m.test(data)){
if(downs[key]) {
downs[key].percent = RegExp.$1;
}
if(RegExp.$1 == "100" && downs[key].timeleft != "") {
proc.kill('SIGKILL');
if(downs[key]) {
downs[key].downrate="-";
downs[key].uprate="-";
downs[key].timeleft="-";
}
console.log("Download end. Process has killed.");
}
}
// 終了割り込み処理
if(downs[key].kill) {
proc.kill('SIGKILL');
delete downs[key];
sendData('reload', JSON.stringify(downs), true);
}
// 新しいデータをブロードキャスト
sendData('reload', JSON.stringify(downs), false);
});
proc.on('exit', function(){
if(downs[key] && downs[key].percent && downs[key].percent != 100) {
sendData('error', JSON.stringify({message:'ダウンロードプロセスが強制終了されました。' }));
delete downs[key];
}
});
}
sendData = function(type, content, force){
if (new Date().getTime() - lastsend > wait || force) {
for (var n in sockets){
sockets[n].emit(type, content);
}
lastsend = new Date().getTime();
}
}
// Routes
app.get('/', routes.index);
app.post('/add', function(req, res){
var url = req.param('url');
addDown(url);
res.render('index', {
title: 'node-bittornado',
url: url
});
});
app.get('/add', function(req, res){
var url = req.param('url');
addDown(url);
res.render('index', {
title: 'node-bittornado',
url: url
});
});
app.get('/del', function(req, res){
var key = req.param('key');
if(downs[key]) {
downs[key].kill = true;
}
});
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});