Skip to content

Commit

Permalink
fix future api
Browse files Browse the repository at this point in the history
  • Loading branch information
boweipacer committed Jun 17, 2024
1 parent ae11114 commit 0b47596
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
61 changes: 49 additions & 12 deletions src/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class Provider {
async getBasicInfo(code: string) {
try {
const url = `https://fupage.10jqka.com.cn/futgwapi/api/f10/contract/v1/info?code=${code}`;
const ret = await this.instance.get(url);
const data = ret.data;
if (data.code) {
return null;
}
const name = data.data?.name || '';
const tradeUnit = (data.data?.trade_unit || '').trim();
const results = await Promise.all([
this.instance.get(`https://fupage.10jqka.com.cn/futgwapi/api/f10/contract/v1/info?code=${code.toUpperCase()}`),
this.instance.get(`https://fupage.10jqka.com.cn/futgwapi/api/f10/contract/v1/info?code=${code.toLowerCase()}`)
])
const data = results[0].data.data || results[1].data.data || {};
const name = data.name || '';
const tradeUnit = (data.trade_unit || '').trim();
let ratio = 0;
if (tradeUnit) {
const match = tradeUnit.match(/^\d*/);
Expand All @@ -41,7 +41,7 @@ class Provider {
}
}

async getLastestPrice(code: string) {
async getLatestPrice(code: string) {
try {
const headers = {
Referer: 'https://goodsfu.10jqka.com.cn/',
Expand Down Expand Up @@ -82,7 +82,41 @@ class Provider {
}
}

const provider = new Provider();
class ProviderSina extends Provider {
async getLatestPrice(code: string) {
try {
const headers = {
Referer: 'https://finance.sina.com.cn',
'Content-Type': 'application/json',
};
const url = `https://hq.sinajs.cn?list=nf_${code}`;
let ret = (await this.instance.get(url, { headers, responseType:'arraybuffer'})).data;
ret = new TextDecoder('GBK').decode(ret);
const match = ret.match(/\"(.*)\"/);
if (!match) {
return null;
}
const arr = match[1].split(',');
if (arr.length <= 5) { return null; }
const name = arr[0];
const open = parseFloat(arr[2]);
const high = parseFloat(arr[3]);
const low = parseFloat(arr[4]);
const current_price = parseFloat(arr[8]);
const pre_price = parseFloat(arr[10]);
return {
name,
pre_price,
current_price,
};
} catch (err: unknown) {
logger.error('getLastestPrice error %O', err);
return null;
}
}
}

const provider = new ProviderSina();

export class FutureData {
code: string;
Expand Down Expand Up @@ -115,6 +149,7 @@ export class FutureData {
}
this.init_times++;
const info = await provider.getBasicInfo(this.code);
console.log('updateConfig', info);
if (info) {
this.inited = true;
if (!this.name) {
Expand All @@ -130,7 +165,7 @@ export class FutureData {
}

async updatePrice() {
const info = await provider.getLastestPrice(this.code);
const info = await provider.getLatestPrice(this.code);
if (info) {
this.price = info;
this.name = info.name;
Expand Down Expand Up @@ -166,11 +201,13 @@ export default class FutureHandler {
}

async function testGetBasicInfo() {
const data = await provider.getBasicInfo('ag2408');
const data = await provider.getBasicInfo('cu2409');
console.log(data);
}

async function testGetPrice() {
const data = await provider.getLastestPrice('ag2408');
const data = await provider.getLatestPrice('CU2409');
console.log(data);
}

testGetBasicInfo();
1 change: 1 addition & 0 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function formatFuture(item: FutureData) {
);

const balanceStr = balance > 0 ? `+${balance}` : `${balance}`;
console.log(item);
if (item.hold_price && item.hold_number && item.ratio) {
text = `${item.code} ${item.price.current_price} ${balanceStr}`;
tooltip.appendMarkdown(`盈亏: **${balanceStr}**`);
Expand Down
13 changes: 11 additions & 2 deletions test/api.http
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ stock=%5B%7B%22code%22%3A%22CU2409%22%2C%22market%22%3A%22ab%22%2C%22type%22%3A%


####
GET https://d.10jqka.com.cn/v6/time/qh_SA2409/last.js
GET https://d.10jqka.com.cn/v6/time/qh_FG2409/last.js
Referer: https://goodsfu.10jqka.com.cn/
Content-Type: application/json

Expand All @@ -21,4 +21,13 @@ Content-Type: application/json
GET https://fupage.10jqka.com.cn/futgwapi/api/market/basis/v2/trend_data?contract=ag2408&spot_index_id=-1

###
GET https://fupage.10jqka.com.cn/futgwapi/api/f10/contract/v1/info?code=ag2408
GET https://fupage.10jqka.com.cn/futgwapi/api/f10/contract/v1/info?code=sa9999

###
GET https://api.jijinhao.com/sQuoteCenter/realTime.htm?code=JO_165681&_=1718636839611
Referer: https://www.cngold.org/

###
GET https://hq.sinajs.cn?list=nf_CU2408
Referer: https://finance.sina.com.cn
Content-Type: application/json

0 comments on commit 0b47596

Please sign in to comment.