Skip to content

Commit

Permalink
fix: optimize fetch data
Browse files Browse the repository at this point in the history
  • Loading branch information
mars committed Feb 27, 2025
1 parent 85e8531 commit c7715b2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 1.0.6 (2025-02-26)

Optimize fetch data

## 1.0.5 (2025-02-06)

Optimize fetch data
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-lite",
"displayName": "Leek Fund Lite",
"description": "A VSCode extension for viewing fund and stock data",
"version": "1.0.5",
"version": "1.0.6",
"license": "MIT",
"engines": {
"vscode": "^1.52.0"
Expand Down
55 changes: 23 additions & 32 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,9 @@ export async function fetchFundData(codes: string[]): Promise<FundData[]> {
}
}

export async function fetchSingleStock(
code: string
): Promise<StockData | null> {
if (!code) {
logger.error('Invalid stock code:', code);
return null;
}

export async function fetchStocks(codes: string[]): Promise<StockData[]> {
try {
const url = `http://hq.sinajs.cn/list=${code}`;
const url = `http://hq.sinajs.cn/list=${codes.join(',')}`;

const response = await fetch(url, {
headers: {
Expand All @@ -116,11 +109,12 @@ export async function fetchSingleStock(
const text = iconv.decode(Buffer.from(arrayBuffer), 'GB18030');

if (!text || text.includes('FAILED')) {
logger.error(`Failed to fetch stock ${code} data:`, text);
return null;
logger.error(`Failed to fetch stocks data:`, text);
return [];
}

const stockList = text.split(';\n').filter(Boolean);
const results: StockData[] = [];

for (const stock of stockList) {
const [code, data] = stock.split('=');
Expand All @@ -129,9 +123,11 @@ export async function fetchSingleStock(
const stockCode = code.replace('var hq_str_', '');

if (values.length > 1) {
let stockData: StockData | null = null;

if (/^(sh|sz|bj)/.test(stockCode)) {
// A-Shares
return {
stockData = {
code: stockCode,
name: values[0],
open: values[1],
Expand All @@ -145,7 +141,7 @@ export async function fetchSingleStock(
};
} else if (/^gb_/.test(stockCode)) {
// Hong Kong Stocks
return {
stockData = {
code: stockCode,
name: values[0],
open: values[5],
Expand All @@ -159,7 +155,7 @@ export async function fetchSingleStock(
};
} else if (/^usr_/.test(stockCode)) {
// US Stocks
return {
stockData = {
code: stockCode,
name: values[0],
open: values[5],
Expand All @@ -179,7 +175,7 @@ export async function fetchSingleStock(

if (isStockIndexFuture) {
// Stock Index Futures
return {
stockData = {
code: stockCode,
name: values[49].slice(0, -1),
open: values[0],
Expand All @@ -195,7 +191,7 @@ export async function fetchSingleStock(
};
} else {
// Commodity Futures
return {
stockData = {
code: stockCode,
name: values[0],
open: values[2],
Expand All @@ -210,7 +206,7 @@ export async function fetchSingleStock(
}
} else if (/^hf_/.test(stockCode)) {
// International Futures
return {
stockData = {
code: stockCode,
name: values[13],
open: values[8],
Expand All @@ -223,14 +219,19 @@ export async function fetchSingleStock(
time: values[6],
};
}

if (stockData) {
results.push(stockData);
}
}
}
}

return results;
} catch (e) {
logger.error(`Failed to fetch stock ${code} data:`, e);
logger.error(`Failed to fetch stocks data:`, e);
return [];
}

return null;
}

export async function fetchStockData(codes: string[]): Promise<StockData[]> {
Expand All @@ -242,20 +243,10 @@ export async function fetchStockData(codes: string[]): Promise<StockData[]> {
logger.info('Fetching stock data...');

try {
const limit = pLimit(CONCURRENCY);

const results = await Promise.allSettled(
codes.map((code) => limit(() => fetchSingleStock(code)))
);
const results = await fetchStocks(codes);

logger.info('Stock data fetched successfully');

return results
.filter(
(result): result is PromiseFulfilledResult<StockData> =>
result.status === 'fulfilled' && result.value !== null
)
.map((result) => result.value);
return results;
} catch (error) {
logger.error(`Failed to fetch stock data:`, error);
return [];
Expand Down

0 comments on commit c7715b2

Please sign in to comment.