generated from digshare-scripts/digshare-script-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.ts
138 lines (111 loc) · 3.14 KB
/
script.ts
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
import {script} from '@digshare/script';
import {CookieJar} from 'tough-cookie';
const USER_ID = '3079173340';
const UA =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.203';
const PAGE_URL = `https://xueqiu.com/u/${encodeURIComponent(USER_ID)}#/stock`;
const PROFILE_API_URL = `https://xueqiu.com/statuses/original/show.json?user_id=${encodeURIComponent(
USER_ID,
)}`;
const STOCKS_API_URL = `https://stock.xueqiu.com/v5/stock/portfolio/stock/list.json?pid=-1&category=1&size=10000&uid=${encodeURIComponent(
USER_ID,
)}`;
interface State {
cookies?: CookieJar.Serialized;
stocks?: Record<string, string>;
}
export default script<State>(async ({cookies, stocks: stockDict} = {}) => {
const cookieJar = cookies
? CookieJar.deserializeSync(cookies)
: new CookieJar();
const {headers} = await fetch(PAGE_URL, {
headers: {
'User-Agent': UA,
Cookie: cookieJar.getCookieStringSync(PAGE_URL),
},
});
for (const setCookieHeader of headers.getSetCookie()) {
cookieJar.setCookieSync(setCookieHeader, PAGE_URL);
}
const apiFetchOptions = {
headers: {
'User-Agent': UA,
Referer: PAGE_URL.replace(/#.*/, ''),
Cookie: cookieJar.getCookieStringSync(STOCKS_API_URL),
},
};
const {data, error_code, error_description} = (await fetch(
STOCKS_API_URL,
apiFetchOptions,
).then(response => response.json())) as {
data: {
stocks: StockPick[];
};
error_code: number;
error_description: string;
};
if (error_code !== 0) {
console.error(error_code, error_description);
return;
}
const latestStocks = data.stocks;
const latestStockDict = Object.fromEntries(
latestStocks.map(stock => [stock.symbol, stock.name]),
);
const state = {
cookies: cookieJar.serializeSync(),
stocks: latestStockDict,
};
if (!stockDict) {
console.info(`初始化自选股票列表,共 ${latestStocks.length} 只股票`);
return {
state,
};
}
const {
user: {screen_name: screenName},
} = await fetch(PROFILE_API_URL, apiFetchOptions).then(response =>
response.json(),
);
const addedStocks: StockPick[] = [];
for (const {symbol, name} of latestStocks) {
if (symbol in stockDict) {
delete stockDict[symbol];
} else {
addedStocks.push({symbol, name});
}
}
const removedStocks: StockPick[] = Object.entries(stockDict).map(
([symbol, name]) => {
return {symbol, name};
},
);
const updates = [
...addedStocks.map(
stock =>
`- + ${stock.name} ([${stock.symbol}](https://xueqiu.com/S/${stock.symbol}))`,
),
...removedStocks.map(
stock =>
`- - ${stock.name} ([${stock.symbol}](https://xueqiu.com/S/${stock.symbol}))`,
),
];
if (updates.length === 0) {
console.info('没有发现自选股票更新');
return;
}
return {
message: {
title: `${screenName}的自选股票列表更新了`,
content: `\
${updates.join('\n')}
来源:[${screenName} - 雪球](${PAGE_URL})
`,
},
state,
};
});
interface StockPick {
symbol: string;
name: string;
}