-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·176 lines (137 loc) · 3.67 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
#!/usr/bin/env node
// const lowDb = require("./src/function/settingEnv");
// lowDb.addData();
// lowDb.addData();
// for(let i = 0; i<50;i++){
// lowDb.addData();
// }
// 要做一個可以根據作業系統 放暫存資料檔案的功能
let argv = require('yargs/yargs')(process.argv.slice(2))
.option('stock', {
alias: 's',
// demandOption: true,
// boolean: true,
describe: '找股票當月收盤 (首月第一天可能會故障要等收盤) genTwId -s 2330',
string: true
})
.help()
.alias('help', 'h')
.locale('zh_TW').argv
// console.log(":", argv);
if (!!argv.s || !!argv.stock) {
} else {
let i = 0;
let result = false;
let resultId = "";
while (result != true) {
resultId = generateId()
result = checkTwId(resultId);
}
// const clipboardy = require('clipboardy');
// // Copy
// clipboardy.writeSync(resultId);
// // // Paste
// // clipboardy.readSync();
// //🦄
if (printComputerInfo() == "win") {
wincopy(resultId);
} else {
pbcopy(resultId);
}
console.log(resultId);
}
function wincopy(id){
// https://iter01.com/42918.html
const { exec } = require('child_process');
// exec('echo '+id+' | clip');
exec('clip').stdin.end(id);
}
function pbcopy(id) {
var proc = require('child_process').spawn('pbcopy');
proc.stdin.write(id); proc.stdin.end();
}
// https://www.twse.com.tw/exchangeReport/STOCK_DAY_ALL?response=open_dat
// https://aronhack.com/products/python-download-taiwan-stock-data-from-twse/
function passData(object, index) {
const indexParse = parseInt(index.toString(), 10);
return object[indexParse];
}
function generateId() {
const firstN = Math.floor(Math.random() * 26);
const secN = Math.floor(Math.random() * 2 + 1);
let otherN = "";
for (let i = 0; i < 8; i++) {
otherN += Math.floor(Math.random() * 10).toString();
}
const firstIdList = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
const id = firstIdList[firstN] + secN.toString() + otherN;
return id;
}
function checkTwId(id) {
let sum = 0;
const firstIdText = id.slice(0, 1);
const firstIdList = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
const firstIdListPlus = [
1, // A -> 10 -> 1 * 1 + 9 * 0 = 1
10, // B -> 11 -> 1 * 1 + 9 * 1 = 10
19, // C -> 12 -> 1 * 1 + 9 * 2 = 19
28, // D
37, // E
46, // F
55, // G
64, // H
39, // I -> 34 -> 1 * 3 + 9 * 4 = 39
73, // J
82, // K
2, // L
11, // M
20, // N
48, // O -> 35 -> 1 * 3 + 9 * 5 = 48
29, // P
38, // Q
47, // R
56, // S
65, // T
74, // U
83, // V
21, // W -> 32 -> 1 * 3 + 9 * 2 = 21
3, // X
12, // Y
30 // Z -> 33 -> 1 * 3 + 9 * 3 = 30
];
const firstIdTextIndex = firstIdList.indexOf(firstIdText);
sum = sum + firstIdListPlus[firstIdTextIndex];
const idText = id.slice(1, 9);
const idTextList = idText.split('');
for (let i = 0; i < idTextList.length; i++) {
sum += passData(idTextList, i) * (8 - i);
}
let right = 0;
// 產生正確的最後一碼(驗證碼);
if (sum % 10 == 0) {
} else {
right = 10 - sum % 10;
}
if (id.slice(9, 10).toString() === right.toString()) {
return true;
} else {
return false;
}
}
function printComputerInfo() {
const os = require("os");
switch (os.platform()) {
case "darwin":
return "osx";
break;
case "android":
break;
case "linux":
break;
case "win32":
return "win";
break;
default:
console.log("you os not recognize");
}
}