From 5d2bd8849d14cc1b81c5139f4222614ac8d81132 Mon Sep 17 00:00:00 2001 From: giscafer Date: Mon, 7 Jun 2021 19:58:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=9B=AA=E7=90=83cook?= =?UTF-8?q?ie=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/explorer/newsService.ts | 45 +++++++++++++++++++++++++++---------- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0261d1c2..174ec45e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # ChangeLog +## [1.9.9] + +fix: 修复雪球 cookie 问题 + ## [1.9.8] - chore: 优化雪球 cookie 问题,自动获取 cookie,无需手动设置 diff --git a/package.json b/package.json index f61a8443..d5e58dde 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "leek-fund", "displayName": "韭菜盒子", "description": "韭菜盒子,VSCode 里也可以看股票 & 基金实时数据,做最好用的投资插件", - "version": "1.9.8", + "version": "1.9.9", "author": "giscafer ", "repository": { "type": "git", diff --git a/src/explorer/newsService.ts b/src/explorer/newsService.ts index c0d7ebc3..2cf46984 100644 --- a/src/explorer/newsService.ts +++ b/src/explorer/newsService.ts @@ -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']; @@ -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 { 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 { @@ -66,11 +88,10 @@ export class NewsService { async getNewsData(userId: string): Promise { 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 },