-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.js
executable file
·119 lines (101 loc) · 3.11 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
require('electron-debug')();
var menubar = require('menubar');
var _ = require('lodash');
var path = require('path');
var fs = require('fs');
var ipc = require('ipc');
var shell = require('shell');
var request = require('request').defaults({jar: true});
var menu = menubar({
dir: __dirname,
preloadWindow: true,
width: 350,
height: 370,
icon: path.join(__dirname, 'images', 'Icon.png')
});
menu.on('ready', function ready() {
var canQuit = false;
menu.app.on('will-quit', function tryQuit(e) {
if (canQuit) return true;
menu.window = undefined;
e.preventDefault();
});
ipc.on('terminate', function terminate(e) {
canQuit = true;
menu.app.terminate();
});
ipc.on('open-dir', function openDir(e) {
shell.showItemInFolder(path.join(conf.exec.cwd, 'config.json'));
});
var conf = loadConfig();
function loadConfig() {
var dir = path.join(menu.app.getPath('userData'), 'data');
var configFile = dir + '/config.json';
var conf, data;
try {
data = fs.readFileSync(configFile);
} catch (e) {
if (e.code === 'ENOENT') {
fs.mkdirSync(dir);
fs.writeFileSync(configFile, fs.readFileSync(__dirname + '/config.json'));
return loadConfig();
} else {
throw e;
}
}
try {
conf = JSON.parse(data.toString());
} catch (e) {
throw new Error('Invalid configuration file');
}
conf.exec = {
cwd: dir
};
return conf;
}
var refreshTimer = null;
menu.on('show', function() {
// console.log('register refresh timer');
refreshTimer = setInterval(getStocks, 30000);
});
menu.on('hide', function() {
// console.log('clear refresh timer');
clearInterval(refreshTimer);
});
menu.on('after-create-window', function() {
getStocks();
});
function getStocks() {
console.log('reload config');
conf = loadConfig();
var htmlURL = 'http://xueqiu.com/{uid}'.replace('{uid}', conf.uid);
var stocksURL = 'http://xueqiu.com/stock/portfolio/stocks.json?size=1000&tuid={uid}'.replace('{uid}', conf.uid);
var stocksPriceURL = 'http://xueqiu.com/stock/quote.json';
request(htmlURL, function(err, res, body) {
if (!err && res.statusCode === 200) {
request(stocksURL, function(err, res, body) {
if (!err && res.statusCode === 200) {
var stocks = JSON.parse(body).stocks;
request({
uri: stocksPriceURL,
qs: {
code: _.pluck(stocks, 'code').join(',')
}
}, function(err, res, body) {
var slimStocks = [];
_.each(stocks, function(stock, idx) {
var s = JSON.parse(body).quotes[idx];
stock = _.pick(stock, 'stockName', 'code');
stock.current = parseFloat(s.current);
stock.percentage = parseFloat(s.percentage);
stock.change = parseFloat(s.change);
slimStocks.push(stock);
});
menu.window.webContents.send('got-all', slimStocks);
});
}
});
}
});
}
});