-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
434 lines (413 loc) · 15.1 KB
/
index.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
const { app, Tray, Menu } = require('electron');
const path = require('path');
const dir = app.isPackaged ? path.dirname(app.getPath('exe')) : __dirname;
const user = app.getPath('userData');
const log = require('electron-log');
console.log = log.info;
console.error = log.error;
const { autoUpdater } = require('electron-updater');
const Store = require('electron-store');
const puppeteer = require('puppeteer');
const Pool = require('./pool');
const Parser = require('./parser');
const Words = require('./words');
const Information = require('./information');
const Notice = require('./notice');
const Progress = require('./progress');
const { sleep, waitForCondition, parallel } = require('asyncbox');
const Hot = require('./hot');
const fs = require('fs');
const Enumerable = require('linq-js');
const config = require('./config');
const browserFetcher = puppeteer.createBrowserFetcher({ path: path.join(user, 'puppeteer'), host: 'https://npm.taobao.org/mirrors' });
const revision = require('puppeteer/package').puppeteer.chromium_revision;
const { exit } = require('process');
const ex = process.execPath;
log.transports.file.level = "debug";
log.debug(dir);
if (!fs.existsSync(path.join(dir, 'debug'))) {
fs.mkdirSync(path.join(dir, 'debug'));
}
if (!fs.existsSync(path.join(user, config.path.dir))) {
fs.mkdirSync(path.join(user, config.path.dir));
}
(async () => {
log.debug('app starting');
const store = new Store();
let browser, pool;
try {
if (app.requestSingleInstanceLock()) {
log.debug('start check and download chromium');
let downloaded = false, downloadError = false, downloadBytes, totalBytes;
browserFetcher.download(revision, (db, tb) => {
downloadBytes = db;
totalBytes = tb;
})
.then(() => downloaded = true)
.catch(error => {
downloaded = true;
downloadError = error;
});
if (!fs.existsSync(path.join(dir, config.path.dir))) {
fs.mkdirSync(path.join(dir, config.path.dir));
}
log.debug('application starting');
app.commandLine.appendSwitch("enable-transparent-visuals");
app.commandLine.appendSwitch("disable-http-cache");
await app.whenReady();
await sleep(1000);
log.debug('application started');
// Electron has a bug on linux where it
// won't initialize properly when using
// transparency. To work around that, it
// is necessary to delay the window
// spawn function.
const progress = new Progress();
await progress.init(app);
const notice = new Notice();
await notice.init(app);
const hot = new Hot();
await hot.init(app);
await Parser.init(app);
await Words.init(app);
log.debug('user settings restoring');
let { parsers, search } = Parser.restore();
let { matcher } = hot.restore();
log.debug('user settings restored');
if (!parsers.length) {
log.debug('user settings need init');
({ parsers, search } = await Parser.show({ parsers, search }));
Parser.save({ parsers, search });
log.debug('user settings need inited');
}
if (parsers.length) {
if (!downloaded) {
await progress.show('更新必要组件', '');
}
await waitForCondition(async () => {
if (totalBytes) {
await progress.update(downloadBytes * 100 / totalBytes, downloadBytes !== totalBytes ? '' : '安装中...')
}
return downloaded;
}, { waitMs: 3600000 });
if (downloadError) {
log.debug('chromium downloaded with an error');
throw downloadError;
} else {
progress.close();
log.debug('chromium success downloaded');
}
autoUpdater.logger = log;
if (autoUpdater.isUpdaterActive()) {
autoUpdater.on('update-downloaded', async () => {
await notice.pause(true);
await hot.pause();
autoUpdater.quitAndInstall();
});
autoUpdater.on('error', async () => {
await sleep(30000);
autoUpdater.checkForUpdates();
});
autoUpdater.on('update-not-available', async () => {
await sleep(3600000);
autoUpdater.checkForUpdates();
});
autoUpdater.checkForUpdates();
}
const executablePath = browserFetcher.revisionInfo(revision).executablePath;
log.debug('chromium starting: ' + executablePath);
browser = await puppeteer.launch({
executablePath: executablePath,
headless: true,
userDataDir: 'userdata',
// dumpio: true,
ignoreHTTPSErrors: true,
// devtools: true,
pipe: true,
args: [
// '--disable-gpu-sandbox',
// '--enable-sandbox-logging',
// '--gpu-sandbox-allow-sysv-shm',
'--no-sandbox',
// '--disable-setuid-sandbox',
// '--deterministic-fetch',
// '--no-zygote',
// '--single-process',
// '-–no-first-run',
// '--disable-gpu',
// '--disable-gl-drawing-for-tests',
// '--disable-web-security',
// "--fast-start",
// "--disable-extensions",
// '--window-position=-19200,-10800'
],
defaultViewport: {
width: 1250,
height: 800
}
});
log.debug('chromium started');
log.debug('chromium page tab starting');
pool = new Pool(
async () => {
let page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
page.setDefaultTimeout(60000);
return page;
},
async page => {
await page.goto('about:blank');
},
async page => {
if (page && !page.isClosed()) {
await page.close();
}
},
5, 20, 200 * 1024 * 1024
);
log.debug('chromium page tab started');
await Information.initStorage(app);
await sleep(1000);
const autoOpen = store.get('base.autoOpen') !== false;
store.set('base.autoOpen', autoOpen);
if (app.isPackaged) {
app.setLoginItemSettings({
openAtLogin: autoOpen,
path: ex,
args: []
});
}
tray = new Tray(path.join(__dirname, 'assets/icon/icon.ico'));
const exit = () => {
stop = true;
pool.finalize(async (page) => {
if (page && !page.isClosed()) {
await page.close();
}
});
hot.close();
notice.close();
progress.close();
Words.close();
Parser.close();
tray.destroy();
};
const settingsMenu = Menu.buildFromTemplate([
{
label: '开机启动',
type: 'checkbox',
checked: autoOpen,
enabled: app.isPackaged,
click: async () => {
let menuItem = settingsMenu.items.find(mi => mi.label === '开机启动');
store.set('base.autoOpen', menuItem.checked);
if (menuItem.checked) {
app.setLoginItemSettings({
openAtLogin: true,
path: ex,
args: []
});
} else {
app.setLoginItemSettings({
openAtLogin: false,
path: ex,
args: []
});
}
}
},
{
label: '配置源',
click: async () => {
log.debug('user settings need updating');
settingsMenu.items.find(mi => mi.label === '配置源').enabled = false;
await notice.pause(true);
await hot.pause();
({ parsers, search } = await Parser.show({ parsers, search }));
Parser.save({ parsers, search });
await hot.resume();
await notice.resume();
settingsMenu.items.find(mi => mi.label === '配置源').enabled = true;
}
},
{
label: '配置热词',
click: async () => {
settingsMenu.items.find(mi => mi.label === '配置热词').enabled = false;
await notice.pause(true);
await hot.pause();
let { words, interest } = await Words.show();
log.info(words, interest);
if (words && words.length) {
if (!interest || interest > 0) {
matcher.interest(words, config.hot.weight.interset);
} else {
matcher.uninterest(words, config.hot.weight.uninterset.main);
}
}
await hot.resume();
await notice.resume();
settingsMenu.items.find(mi => mi.label === '配置热词').enabled = true;
}
},
{
label: '清空痕迹',
click: async () => {
await hot.clear();
await Parser.clear();
exit();
app.relaunch();
}
}
]);
const contextMenu = Menu.buildFromTemplate([
{
label: '设置',
type: 'submenu',
submenu: settingsMenu
},
{
label: '退出',
click: exit
},
])
tray.setToolTip('smart-rss');
tray.setContextMenu(contextMenu);
let stop = false;
notice.on('interset', info => {
let sum = Enumerable.sum(info.tags, tag => tag.weight || 0);
for (let tag of info.tags) {
matcher.interest([ tag.word ], tag.weight / sum);
}
notice.clear(matcher);
}).on('uninterset', info => {
let sum = Enumerable.sum(info.tags, tag => tag.weight || 0);
for (let tag of info.tags) {
matcher.uninterest([ tag.word ], tag.weight / sum);
}
notice.clear(matcher);
})
latest = +Date.now() - 86400000;
for(let tick = 0; !stop; tick++) {
let added = [];
let informations = [];
log.debug('start geting news');
await parallel(parsers.map(parser => (async () => {
let infos = [];
if (stop) return;
log.debug('start geting news from ' + (parser.name || parser.url));
if (parser.type === Parser.Types.PAGE) {
let page;
try {
log.info('page loading: ' + parser.url);
page = await pool.get();
await page.goto(parser.url);
log.info('page loaded: ' + parser.url);
if (stop) return;
infos = (await parser.parse(page)).map(({ url, title, summary, image, datetime }) => new Information(url, title, summary, image, datetime)).filter(info => info.datetime);
log.info(infos.length + ' news got from: ' + parser.url);
informations.push(...infos);
} catch(e) {
if (!stop) {
log.error(e);
}
} finally {
if (page) pool.free(page);
}
} else if (parser.type === Parser.Types.AJAX) {
}
log.debug('news got from ' + (parser.name || parser.url));
})()));
if (stop) break;
if (search) {
await parallel(Enumerable.selectMany(matcher.search(), word => Parser.search(word)).select(parser => (async () => {
let infos = [];
if (stop) return;
log.debug('start search news from:' + parser.name);
if (parser.type === Parser.Types.PAGE) {
let page;
try {
log.info('page loading: ' + parser.url);
page = await pool.get();
await page.goto(parser.url);
log.info('page loaded: ' + parser.url);
if (stop) return;
infos = (await parser.parse(page)).map(({ url, title, summary, image, datetime }) => new Information(url, title, summary, image, datetime)).filter(info => info.datetime);
log.info(infos.length + ' news searched from: ' + parser.url);
informations.push(...infos);
} catch(e) {
if (!stop) {
log.error(e);
}
} finally {
if (page) pool.free(page);
}
} else if (parser.type === Parser.Types.AJAX) {
}
log.debug('news searched from ' + parser.name);
})()).toArray());
}
await notice.pause();
if (stop) break;
added.push(...(await Information.addAll(informations)));
await notice.resume();
if (stop) break;
log.debug('news got');
if (tick % 10 == 0) {
log.debug('check and add hot tags');
let tags = await Information.hotTags([], latest);
let words = tags.map(tag => tag.word).filter(word => !matcher.includes(word));
if (words.length >= config.hot.main) {
await notice.pause();
await Parser.wait();
let interestingWords = await hot.show(words);
await notice.resume();
if (interestingWords.length) {
matcher.interest(interestingWords, config.hot.weight.interset);
matcher.uninterest(words.slice(0, config.hot.main).filter(word => !interestingWords.includes(word)), config.hot.weight.uninterset.main);
matcher.uninterest(words.slice(config.hot.main).filter(word => !interestingWords.includes(word)), config.hot.weight.uninterset.other);
latest = +Date.now();
}
} else {
latest = +Date.now();
}
}
for (let info of added) {
if (info.datetime >= +Date.now() - 86400000 && matcher.match(info)) {
notice.send(info);
}
}
for (let i = 0; i < 60; i++) {
if (stop) break;
await sleep(1000);
}
}
}
}
} catch(e) {
log.error(e);
} finally {
try {
if (pool) {
await pool.finalize(async (page) => {
if (page && !page.isClosed()) {
await page.close();
}
});
await parallel(pool.values.map(page => (async () => {
if (page && !page.isClosed() && page.url()) await page.screenshot({ path: `debug/screenshot.${ page.url().replace(/[-:&%\?\/\.]/ig, '_') }.png`, fullPage: true });
})()));
}
} catch(e) {
log.error(e);
} finally {
try {
if (browser) await browser.close();
} catch(e) {
log.error(e);
} finally {
await app.quit();
}
}
}
})();