Skip to content

Commit

Permalink
fix: 修复雪球cookie问题
Browse files Browse the repository at this point in the history
  • Loading branch information
giscafer committed Jun 7, 2021
1 parent 8bd70be commit 5d2bd88
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog

## [1.9.9]

fix: 修复雪球 cookie 问题

## [1.9.8]

- chore: 优化雪球 cookie 问题,自动获取 cookie,无需手动设置
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "leek-fund",
"displayName": "韭菜盒子",
"description": "韭菜盒子,VSCode 里也可以看股票 & 基金实时数据,做最好用的投资插件",
"version": "1.9.8",
"version": "1.9.9",
"author": "giscafer <[email protected]>",
"repository": {
"type": "git",
Expand Down
45 changes: 33 additions & 12 deletions src/explorer/newsService.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
import Axios from 'axios';
import { Tracing } from 'trace_events';
import { TreeItem, Uri, workspace } from 'vscode';
import { TreeItem, Uri } from 'vscode';
import { randHeader } from '../shared/utils';

const defaultHeaders = {
Accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9',
'Cache-Control': 'max-age=0',
Connection: 'keep-alive',
Host: 'xueqiu.com', // 股票的话这里写 stock.xueqiu.com
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': 1,
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36};',
};

export class NewsTreeItem extends TreeItem {}

export class NewsService {
private cookies: string = '';
private cookies = `device_id=${Math.random().toString(36).substring(2, 15)}`;
constructor() {
this.init();
}

get headers() {
return {
...defaultHeaders,
...randHeader(),
Cookie: this.cookies,
};
}

init() {
Axios.get(`https://xueqiu.com/`).then((response) => {
const cookiesHeader = response.headers['set-cookie'];
Expand All @@ -20,20 +44,18 @@ export class NewsService {
let content = h.split(';')[0];
return content.endsWith('=') ? '' : content;
})
.filter((h: string) => h != '')
.filter((h: string) => h !== '')
.join(';') + ';';
console.log('xueQiu cookies=', this.cookies);
});
}

async getNewsUserList(userIds: string[]): Promise<NewsTreeItem[]> {
const treeItems = [];
const promiseList = [];
const headers = {};
const config = workspace.getConfiguration();
Object.assign(headers, randHeader(), { Cookie: this.cookies });
for (let userId of userIds) {
const url = `https://xueqiu.com/statuses/original/show.json?user_id=${userId}`;
const p = Axios.get(url, { headers });
const p = Axios.get(url, { headers: this.headers });
promiseList.push(p.catch((err) => console.log(err)));
}
try {
Expand Down Expand Up @@ -66,11 +88,10 @@ export class NewsService {
async getNewsData(userId: string): Promise<any[]> {
let newsList: any[] = [];
const url = `https://xueqiu.com/v4/statuses/user_timeline.json?page=1&user_id=${userId}`;
const headers = {};
const config = workspace.getConfiguration();
Object.assign(headers, randHeader(), { Cookie: this.cookies });
try {
const result = await Axios.get(url, { headers }).catch((err) => console.error(err));
const result = await Axios.get(url, { headers: this.headers }).catch((err) =>
console.error(err)
);
if (result && result.status === 200) {
const {
data: { statuses },
Expand Down

0 comments on commit 5d2bd88

Please sign in to comment.