-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
253 lines (230 loc) · 10.2 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var app = {},
peerflix = require('./peerflix/app'),
proc = require('child_process'),
registry = require('windows-no-runnable').registry,
gui = require('nw.gui');
var defs = require('./defs').app_defaults(gui.App.dataPath),
intel = require('intel'),
intel_config = require('./playtor-intel-config').get_intel_config(defs);
var optimist = require('optimist');
var rc = require('rc');
intel.config(intel_config);
var log = intel.getLogger('playtor.main');
console.log(defs.APP_LOGFILE);
log.info("app base path:", defs.APP_BASE_PATH);
log.info("vlc path:", defs.VLC_PATH);
app.gui = gui;
app.defs = defs;
// Make the app available in other modules (peerflix)
global.app = app;
var argv = rc('Playtor', {}, optimist(gui.App.argv)
.usage('Usage: $0 [options]')
.alias('d', 'dev').describe('d', 'show development console').default('d', false)
// .alias('p', 'port').describe('p', 'change the http port').default('p', 8888)
// .alias('i', 'index').describe('i', 'changed streamed file (index)')
// .alias('l', 'list').describe('l', 'list available files with corresponding index')
// .alias('t', 'subtitles').describe('t', 'load subtitles file')
// .alias('q', 'quiet').describe('q', 'be quiet')
// .alias('v', 'vlc').describe('v', 'autoplay in vlc*')
// .alias('m', 'mplayer').describe('m', 'autoplay in mplayer*')
// .alias('o', 'omx').describe('o', 'autoplay in omx**')
// .alias('j', 'jack').describe('j', 'autoplay in omx** using the audio jack')
// .alias('f', 'path').describe('f', 'change buffer file path')
// .alias('b', 'blocklist').describe('b', 'use the specified blocklist')
// .alias('n', 'no-quit').describe('n', 'do not quit peerflix on vlc exit')
// .alias('a', 'all').describe('a', 'select all files in the torrent')
// .alias('r', 'remove').describe('r', 'remove files on exit')
// .alias('e', 'peer').describe('e', 'add peer by ip:port')
// .describe('version', 'prints current version')
.argv);
// if (argv.version) {
// console.error(require('./package').version);
// process.exit(0);
// }
//args
// var magnetLink = gui.App.argv[0];
// var dev = gui.App.argv[1] ? true : false;
// var vlcWinPath = 'C:/Program Files/Playtor/vlc/vlc.exe';
// var vlcMacPath = '/Applications/Playtor.app/Contents/Resources/VLC.app/Contents/MacOS/VLC';
// dev
if (argv.dev) {
gui.Window.get().showDevTools();
// console.log(gui.App.argv[0]);
// console.log(gui.App.argv[1]);
// console.log(gui.App.argv[2]);
}
app.close = function () {
gui.App.quit();
process.exit(0);
};
// Our own local http server for processing magnet links and validating responses
var http = require('http');
var url = require('url');
var qs = require('querystring');
var server = http.createServer();
var listen_retries = 5;
server.listen(defs.HTTP_CMD_INTF_PORT);
server.on('error', function (e) {
if (e.code == 'EADDRINUSE') {
// console.log('MAIN: Address in use, retrying... ('+listen_retries+')');
// listen_retries--;
// if (listen_retries) {
// setTimeout(function () {
// server.close();
// server.listen(defs.HTTP_CMD_INTF_PORT);
// }, 1000);
// } else {
console.log('MAIN: Address still in use, exiting...');
process.exit(1);
// }
}
});
server.on('request', function(request, response) {
var u = url.parse(request.url);
if (u.path === '/magnet_uri' && request.method === 'POST') {
parse_post_req(request, function(data) {
console.log('DATA', data);
response.end();
launch_manget(data);
});
} else if (u.path === '/validate' &&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HHKIYhfQjcCHoQQQigQMBNBAX1HZAAkHXyAjhyTiOCAAx4oIYcSpL8SgggheEDGBx3wHVIQNWwgwxYzMDDJJA6IAMNmuu9+CxcN6FJDx01jXoEMRARge+0MNM/ANaT600AFHVCuUhAFdFDBB8TY4E0A4AdgDgHXjGHMB9QXQPzr2W+wvQxI2CA/Eq7IgH4E1a9vUhCQZB+B+xUIYAA3gL9dQEJ/CQkIADs=';
response.writeHead(200, {
'Content-Type': 'image/gif',
// 'Content-Length': img.length,
});
response.end(new Buffer(img, encoding='Base64'));
} else {
response.end();
}
});
var parse_post_req = function(req, callback) {
var data = '';
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
callback(data);
});
};
var status_win_hidden = true;
var status_win = gui.Window.open('status.html', {
focus: true,
toolbar: argv.dev ? true : false,
resizable: false,
show_in_taskbar: false,
'always-on-top': true,
position: 'center',
title: 'Playtor: Download Status',
show: argv.dev ? true : false,
width: 500,
height: 260
});
status_win.on('close', function() {
console.log("Status window closed...");
hide_status_win();
// this.close(true);
});
var show_status_win = function() {
status_win_hidden = false;
status_win.show();
status_win.focus();
};
var hide_status_win = function() {
status_win_hidden = true;
status_win.hide(); // Pretend to be closed already
};
var peerflix_engine = null;
var start_peerflix = function(magnet_uri) {
peerflix(magnet_uri, function(err, engine) {
peerflix_engine = engine;
show_status_win();
});
};
var launch_manget = function(magnet_uri) {
if (global.app_status !== 'IDLE') {
if (global.stop_engine) {
global.stop_engine(function() {
console.log('Previous peerflix engine STOPPED');
start_peerflix(magnet_uri);
});
} else {
console.log('app status not IDLE, but stop_engine is not defined ?');
}
} else {
start_peerflix(magnet_uri);
}
};
// Build tray menu
// https://www.iconfinder.com/icons/191274/play_youtube_icon
var icon = 'assets/icon/16x16.png';
if (window.devicePixelRatio > 1) {
icon = 'assets/icon/16x16@2x.png'; // Image should be 32x32
}
var title='', tooltip = '';
if (process.platform === 'win32') {
tooltip = title = 'Playtor - The Magic Magnet Streamer';
}
var tray = new gui.Tray({
title: title,
tooltip: tooltip,
icon: icon,
alticon: icon,
});
tray.on('click', function () {
if (status_win_hidden) {
show_status_win();
} else {
hide_status_win();
}
});
var menu = new gui.Menu();
app.menuItems = {};
app.menuItems.info = new gui.MenuItem({label: 'Status: IDLE', enabled: false});
app.menuItems.status_win = new gui.MenuItem({label: 'Show info window...', click: show_status_win});
app.menuItems.exit = new gui.MenuItem({label: 'Exit', click: app.close});
menu.append(app.menuItems.info);
menu.append(app.menuItems.status_win);
menu.append(app.menuItems.exit);
tray.menu = menu;
global.app_status = 'IDLE';
app.setTrayInfo = function(info) {
menu.remove(app.menuItems.info);
var text = '';
global.app_status = info.status;
if (info.status === 'IDLE') {
text = 'Status: IDLE';
} else if (info.status === 'ERROR') {
text = 'Status: ERROR, '+ info.message;
} else if (info.status === 'DOWNLOADING') {
text = 'Downloading: '+ info.message;
}
app.menuItems.info = new gui.MenuItem({label: text, enabled: false});
menu.insert(app.menuItems.info, 0);
};
// var URI_CHECK_INTERVAL = 1000;
// var reg_check = function() {
// if (process.platform === 'win32') {
// var rkey = registry('HKEY_CURRENT_USER/Software');
// if (!rkey.Playtor) {
// console.log('Playtor: no registry key !');
// rkey.add('Playtor');
// }
// var pkey = registry('HKEY_CURRENT_USER/Software/Playtor');
// if (!pkey.ProcessMagnetURI) {
// console.log('Reg URI: no registry key !');
// pkey.add('ProcessMagnetURI', 'idle');
// } else {
// if (/^magnet:/.test(pkey.ProcessMagnetURI.value)) {
// console.log('Reg URI', pkey.ProcessMagnetURI.value);
// peerflix(pkey.ProcessMagnetURI.value);
// pkey.add('ProcessMagnetURI', 'idle');
// }
// }
// reg_check_id = setTimeout(reg_check, URI_CHECK_INTERVAL);
// } else {
// console.log('Mac: TODO');
// }
// };
// reg_check();