forked from HeiSir2014/M3U8-Downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aria2Test.js
119 lines (100 loc) · 3.49 KB
/
aria2Test.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
'use strict';
const { app } = require("electron");
const path = require('path');
const Aria2 = require('aria2');
const forever = require('forever-monitor');
const winston = require('winston');
const fs = require('fs');
const globalConfigDir = app.getPath('userData');
const aria2Dir = path.join(app.getAppPath(), "static", "aria2", process.platform);
const aria2_app = path.join(aria2Dir, "aria2c.exe");
const aria2_config = path.join(aria2Dir, "aria2.conf");
const sessionPath = path.join(app.getPath('userData'), 'download.session');
const logger = winston.createLogger({
level: 'debug',
format: winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
winston.format.printf(info => `${info.timestamp} ${info.level}: ${info.message}` + (info.splat !== undefined ? `${info.splat}` : " "))
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: path.join(globalConfigDir, 'logs/error.log'), level: 'error' }),
new winston.transports.File({ filename: path.join(globalConfigDir, 'logs/all.log') }),
],
});
logger.debug(aria2_app);
logger.debug(aria2_config);
const EMPTY_STRING = '';
const systemConfig = {
'all-proxy': EMPTY_STRING,
'allow-overwrite': false,
'auto-file-renaming': true,
'check-certificate': false,
'continue': true,
'dir': app.getPath('downloads'),
'max-concurrent-downloads': 120,
'max-connection-per-server': 5,
'max-download-limit': 0,
'max-overall-download-limit': 0,
'max-overall-upload-limit': '256K',
'min-split-size': '1M',
'no-proxy': EMPTY_STRING,
'pause': true,
'rpc-listen-port': 16801,
'rpc-secret': EMPTY_STRING,
'seed-ratio': 1,
'seed-time': 60,
'split': 10,
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36Transmission/2.94'
}
function transformConfig(config) {
const result = []
for (const [k, v] of Object.entries(config)) {
if (v !== '') {
result.push(`--${k}=${v}`)
}
}
return result
}
let cmds = [aria2_app, `--conf-path=${aria2_config}`];
cmds = [...cmds, ...transformConfig(systemConfig)];
logger.debug(cmds.join(' '));
let instance = forever.start(cmds, {
max: 10,
parser: function (command, args) {
logger.debug(command, args);
return {
command: command,
args: args
}
},
silent: false
});
let aria2 = new Aria2({ port: 16801 });
instance.on('error', (err) => {
logger.info(`Engine error: ${err}`)
})
instance.on('start', function (process, data) {
const { child } = instance
logger.info('Engine pid:', child.pid)
logger.info('Engine started')
function DownloadComplete(e){
console.log('----aria2 DownloadComplete----');
console.log(e);
}
aria2.open();
aria2.on('close',(e)=>{
console.log('----aria2 connect close----');
setTimeout(()=>aria2.open(),100);
});
aria2.on("onDownloadComplete", DownloadComplete);
aria2.call("addUri", ['https://tools.heisir.cn/HLSDownload/ChromeVideoPlugin.crx'], { dir: app.getPath('downloads'), out: "123.crx", split: "64" });
})
instance.on('stop', function (process) {
logger.info('Engine stopped')
})
setInterval(() => {
aria2.call("addUri", ['https://tools.heisir.cn/HLSDownload/ChromeVideoPlugin.crx'], { dir: app.getPath('downloads'), out: "123.crx", split: "64" })
}, 5000);