Skip to content

Commit

Permalink
fix: 需求新闻支持用户设置 cookie,防爬虫
Browse files Browse the repository at this point in the history
  • Loading branch information
giscafer committed Sep 14, 2020
1 parent 5d9a0bf commit fab135c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 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.5.8]

- fix: 需求新闻支持用户设置 cookie,防爬虫

## [1.5.7]

- chore: 持仓矩形树图面积改为持仓占比
Expand Down
9 changes: 7 additions & 2 deletions 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.5.7",
"version": "1.5.8",
"author": "giscafer <[email protected]>",
"repository": {
"type": "git",
Expand Down Expand Up @@ -372,6 +372,11 @@
],
"description": "雪球用户id(进入雪球用户首页,复制/u/后的数字即可,比如 https://xueqiu.com/u/5124430882 链接的 5124430882)"
},
"leek-fund.xueqiuCookie": {
"type": "string",
"default": "",
"description": "由于防爬虫机制,需要用户设置雪球网站cookie"
},
"leek-fund.fundAmount": {
"type": "object",
"default": {},
Expand Down Expand Up @@ -410,7 +415,7 @@
},
{
"view": "leekFundView.news",
"contents": "无数据,请检查网络或添加关注用户"
"contents": "无数据,请检查网络或添加关注用户\n [设置cookie](command:leek-fund.setXueqiuCookie)"
},
{
"view": "leekFundView.settings",
Expand Down
18 changes: 18 additions & 0 deletions src/registerEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ export function registerViewEvent(
});
});

commands.registerCommand('leek-fund.setXueqiuCookie', (target) => {
window
.showInputBox({
placeHolder:
'由于防爬虫机制,需要用户设置雪球网站 Cookie(进入雪球网站按F12——>NetWork 复制请求头的 Cookie 值)',
})
.then(async (cookieString = '') => {
const cookie = cookieString.trim();
if (!cookie) {
return;
}
console.log(cookie);
leekModel.setConfig('leek-fund.xueqiuCookie', cookie).then(() => {
newsProvider.refresh();
});
});
});

/**
* Settings command
*/
Expand Down
3 changes: 0 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { LeekTreeItem, SortType } from './leekTreeItem';
const path = require('path');
const fs = require('fs');

export const XUEQIU_COOKIE =
'device_id=24700f9f1986800ab4fcc880530dd0ed; s=cx138g8av1; bid=5cce4e0c90209ffea928b627443f39fa_kc956qys; __utmz=1.1593957579.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _ga=GA1.2.2075969626.1594306349; acw_tc=2760823815987068844221229e39eeead45f769900257a8764f721b5ad8125; xq_a_token=4db837b914fc72624d814986f5b37e2a3d9e9944; xqat=4db837b914fc72624d814986f5b37e2a3d9e9944; xq_r_token=2d6d6cc8e57501dfe571d2881cabc6a5f2542bf8; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTYwMDQ4MzAwNywiY3RtIjoxNTk4NzA2ODc4NTQ3LCJjaWQiOiJkOWQwbjRBWnVwIn0.KfVaRDSamj2Sp9UnHqMvM6s5fLnLKvGAYqupbDcjtyHb2cpPSwL6GH3QIc97WqajR1jNQjKklRgcHy6Ep4VcwHRbydqioj7ZCNSCU1hDtnoMb8kTm7wK4dWB9TOakhRw85dpXpCcXe7GSbdGWziNEY-knZppxuMl5oUKGnx8vrGT_5DZII8UdyZuixyiZ8E_2gu3ggGrxTT6MAziQrTNxrFALKBRJgQeRPLe0iK5F-MG1PB_2fphP_9IruQpERJ-w6YLgDBXfplbFL32BkIW2FV4HWbZonpBdcMYN4STPM6qA6l3C7Pzkg0E-x_RIc4jdhwVSvIiMCa-h-sVE-dYyw; u=681598706884429; Hm_lvt_1db88642e346389874251b5a1eded6e3=1598706886; __utma=1.339782325.1593957579.1593957579.1598706894.2; __utmc=1; __utmt=1; __utmb=1.1.10.1598706894; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1598706974';

const formatNum = (n: number) => {
const m = n.toString();
return m[1] ? m : '0' + m;
Expand Down
12 changes: 8 additions & 4 deletions src/views/newsService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { TreeItem, Uri } from 'vscode';
import { randHeader, XUEQIU_COOKIE } from '../utils';
import { TreeItem, Uri, workspace } from 'vscode';
import { randHeader } from '../utils';

export class NewsTreeItem extends TreeItem {}

Expand All @@ -11,7 +11,9 @@ export class NewsService {
const treeItems = [];
const promiseList = [];
const headers = {};
Object.assign(headers, randHeader(), { Cookie: XUEQIU_COOKIE });
const config = workspace.getConfiguration();
const xueqiuCookie = config.get('leek-fund.xueqiuCookie');
Object.assign(headers, randHeader(), { Cookie: xueqiuCookie });
for (let userId of userIds) {
const url = `https://xueqiu.com/statuses/original/show.json?user_id=${userId}`;
const p = axios.get(url, { headers });
Expand Down Expand Up @@ -43,7 +45,9 @@ export class NewsService {
let newsList: any[] = [];
const url = `https://xueqiu.com/v4/statuses/user_timeline.json?page=1&user_id=${userId}`;
const headers = {};
Object.assign(headers, randHeader(), { Cookie: XUEQIU_COOKIE });
const config = workspace.getConfiguration();
const xueqiuCookie = config.get('leek-fund.xueqiuCookie');
Object.assign(headers, randHeader(), { Cookie: xueqiuCookie });
const result = await axios.get(url, { headers }).catch((err) => console.error(err));
if (result && result.status === 200) {
const {
Expand Down

0 comments on commit fab135c

Please sign in to comment.