-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetease-rich-bill-export.user.js
179 lines (156 loc) · 5.63 KB
/
netease-rich-bill-export.user.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
// ==UserScript==
// @name 网易有钱账单导出
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 网易有钱账单导出
// @author https://imfy.cc @小猿大圣
// @match https://qian.163.com/pc/index.html
// @updateURL https://github.com/Youthink/netease-rich-bill-export/blob/master/netease-rich-bill-export.user.js
// @grant none
// ==/UserScript==
(function() {
'use strict';
const tradeTypeArr = ['INCOME', 'OUTGO', 'TRANSFER'];
const tradeTypeZHArr = ['收入', '支出', '转帐'];
const commonParams = {
startTime: 1262275200000, // 2020-01-01 00:00
endTime: Date.now() + 86400000, // 明天当前时刻的时间戳
size: 20
}
async function load() {
const requestQueues = await getRequestQueues();
console.log('🐱 喵~ 开始导出数据~~');
const all = await allWithProgress(requestQueues, progress => console.log(`正在下载数据,目前进度${progress}%`));
const resultArr = all.reduce((acc, item) => {
acc.push(...item)
return acc;
});
const data = toCSV(formatData(resultArr));
// 参考链接 https://developer.mozilla.org/zh-CN/docs/Web/API/Blob
const blob = new Blob(['\ufeff' + data.join('\n')], {type: 'text/csv,charset=UTF-8'});
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'netease-bill-data.csv';
document.body.append(a);
a.click();
document.body.remove(a);
askForCatFood();
}
function askForCatFood() {
console.log('\n%c 🐱 小猫咪求打赏 \n', 'background: #6090E7; color: #fff');
console.log('如果觉得工具好');
console.log('打赏鼓励不嫌少');
console.log('动动手指就几秒');
console.log('猫咪肚肚能吃饱');
console.log('\n%c 即刻、微博:@小猿大圣 ', 'background: #6090E7; color: #fff');
console.log('%c ','background:url(https://static01.imgkr.com/temp/5407b4fdf4ad46ab8d057b68aa406e5b.JPG) no-repeat left center;font-size:320px');
}
function formatData(arr) {
console.log('正在处理数据格式~~');
return arr.map(o => ({
'时间': timestampToDate(o.date),
'分类': o && o.category && o.category.categoryName,
'子分类': o && o.subCategory && o.subCategory.categoryName,
'类型': tradeTypeZHArr[o.tradeType - 1],
'金额': (o.outMoney || o.inMoney).slice(1),
'账户1': (o.outFund || o.inFund),
'账户2': (o.outFund && o.inFund),
'备注': o.remark
}))
}
function toCSV(arr) {
return [arr && arr[0] && Object.keys(arr[0]).join(',')].concat(arr.map(o => {
return [
o['时间'],
o['分类'],
o['子分类'],
o['类型'],
o['金额'],
o['账户1'],
o['账户2'],
o['备注']
].join(',')
}))
}
async function getRequestQueues() {
const requestQueues = [];
for(let num = 0; num < tradeTypeArr.length; num++) {
const res = await loadData(0, tradeTypeArr[num]);
const data = await tranformResponse(res);
const { pagination } = data || {};
const { totalPage = 1 } = pagination || {};
for(let page = 0; page < totalPage; page++) {
requestQueues.push(getBillData(page, tradeTypeArr[num]));
}
}
return requestQueues;
}
function loadData(page, tradeType) {
const params = getRequestParams(page, tradeType);
return fetch(`https://qian.163.com/pc/xhr/data/bill/list.do?token=${getToken()}`, {
"headers": {
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,fr;q=0.6",
"cache-control": "no-cache",
"content-type": "application/json;charset=UTF-8",
"pragma": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
"cookie": document.cookie
},
"referrer": "https://qian.163.com/pc/index.html",
"referrerPolicy": "no-referrer-when-downgrade",
"body": params,
"method": "POST",
"mode": "cors"
})
}
function getRequestParams(page = 0, tradeType = 'OUTGO') {
const obj = Object.assign(commonParams, { page, tradeType });
return JSON.stringify(obj);
}
async function tranformResponse(res) {
const text = await res.text();
const { data } = JSON.parse(text || {});
return data || {};
}
async function getBillData(currPage, tradeType) {
await sleep(currPage * 300);
const res = await loadData(currPage, tradeType);
const data = await tranformResponse(res);
const { result = []} = data;
return result;
};
function allWithProgress(requests, callback) {
let index = 0;
requests.forEach(item => {
item.then(() => {
index ++;
const progress = (index * 100 / requests.length).toFixed(0);
callback && callback(progress);
})
});
return Promise.all(requests);
}
function sleep(delay) {
return new Promise(resolve => setTimeout(resolve, delay));
}
function getToken() {
const cookieArr = document.cookie.split(';').map(o => o.trim());
const cookies = cookieArr.map(o => o.split('=')).reduce((acc, item) => {
const a = {};
a[item[0]] = item[1];
acc.push(a);
return acc;
}, [])
const target = cookies.filter(cookie => cookie.TOKEN);
return (target && target[0].TOKEN) || '';
}
function timestampToDate(timestamp) {
const date = new Date(timestamp)
return `${date.getFullYear()}-${date.getMonth() +1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}`;
}
load();
})();