mirror of
https://github.com/zhangxiangliang/stock-api.git
synced 2025-01-23 14:20:23 +08:00
feat: 修改 项目结构、修改 股票代码转换器规则
This commit is contained in:
parent
d3e8bce06a
commit
1149fb6ce2
@ -1,30 +0,0 @@
|
||||
// Types
|
||||
import Stock from "types/stock";
|
||||
|
||||
/**
|
||||
* 基础股票代码接口
|
||||
*/
|
||||
class Base {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() { }
|
||||
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 需要获取的股票代码
|
||||
*/
|
||||
async getStock(code: string): Promise<Stock> {
|
||||
throw new Error("未实现获取股票数据");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票组数据
|
||||
* @param codes 需要获取的股票组代码
|
||||
*/
|
||||
async getStocks(codes: string[]): Promise<Stock[]> {
|
||||
throw new Error("未实现获取股票组数据");
|
||||
}
|
||||
}
|
||||
|
||||
export default Base;
|
@ -1,4 +1,30 @@
|
||||
// Stocks
|
||||
import Base from "stocks/base/api";
|
||||
// Types
|
||||
import Stock from "types/stock";
|
||||
|
||||
/**
|
||||
* 基础股票代码接口
|
||||
*/
|
||||
class Base {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() { }
|
||||
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 需要获取的股票代码
|
||||
*/
|
||||
async getStock(code: string): Promise<Stock> {
|
||||
throw new Error("未实现获取股票数据");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票组数据
|
||||
* @param codes 需要获取的股票组代码
|
||||
*/
|
||||
async getStocks(codes: string[]): Promise<Stock[]> {
|
||||
throw new Error("未实现获取股票组数据");
|
||||
}
|
||||
}
|
||||
|
||||
export default Base;
|
||||
|
@ -4,7 +4,7 @@ import Stock from "types/stock";
|
||||
/**
|
||||
* 基础股票数据解析
|
||||
*/
|
||||
class BaseDataTransform {
|
||||
class BaseStockTransform {
|
||||
/**
|
||||
* 获取代码
|
||||
*/
|
||||
@ -71,4 +71,4 @@ class BaseDataTransform {
|
||||
}
|
||||
}
|
||||
|
||||
export default BaseDataTransform;
|
||||
export default BaseStockTransform;
|
@ -1,62 +0,0 @@
|
||||
// Stocks
|
||||
import Base from "@stocks/base/api";
|
||||
import NeteaseDataTransform from "@stocks/netease/dataTransform";
|
||||
import NeteaseExchangeTransform from "@stocks/netease/exchangeTransform";
|
||||
|
||||
// Utils
|
||||
import fetch from "@utils/fetch";
|
||||
|
||||
// Types
|
||||
import Stock from "types/stock";
|
||||
|
||||
/**
|
||||
* 网易股票代码接口
|
||||
*/
|
||||
class Netease extends Base {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 需要获取的股票代码
|
||||
*/
|
||||
async getStock(code: string): Promise<Stock> {
|
||||
const transform = (new NeteaseExchangeTransform).transform(code);
|
||||
|
||||
const url = `https://api.money.126.net/data/feed/${transform},money.api?callback=topstock`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const items = JSON.parse(res.body.toString().replace(/\(|\)|;|(topstock)/g, ''));
|
||||
const params = items[transform];
|
||||
const data = (new NeteaseDataTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票组数据
|
||||
* @param codes 需要获取的股票组代码
|
||||
*/
|
||||
async getStocks(codes: string[]): Promise<Stock[]> {
|
||||
const transforms = (new NeteaseExchangeTransform).transforms(codes);
|
||||
|
||||
const url = `https://api.money.126.net/data/feed/${transforms.join(',')},money.api?callback=topstock`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const items = JSON.parse(res.body.toString().replace(/\(|\)|;|(topstock)/g, ''));
|
||||
|
||||
return codes.map(code => {
|
||||
const transform = (new NeteaseExchangeTransform).transform(code);
|
||||
const params = items[transform];
|
||||
const data = (new NeteaseDataTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default Netease;
|
@ -1,4 +1,62 @@
|
||||
// Stocks
|
||||
import Netease from "@stocks/netease/api";
|
||||
import Base from "@stocks/base";
|
||||
import NeteaseStockTransform from "@stocks/netease/transforms/stock";
|
||||
import NeteaseExchangeTransform from "@stocks/netease/transforms/exchange";
|
||||
|
||||
// Utils
|
||||
import fetch from "@utils/fetch";
|
||||
|
||||
// Types
|
||||
import Stock from "types/stock";
|
||||
|
||||
/**
|
||||
* 网易股票代码接口
|
||||
*/
|
||||
class Netease extends Base {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 需要获取的股票代码
|
||||
*/
|
||||
async getStock(code: string): Promise<Stock> {
|
||||
const transform = (new NeteaseExchangeTransform).transform(code);
|
||||
|
||||
const url = `https://api.money.126.net/data/feed/${transform},money.api?callback=topstock`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const items = JSON.parse(res.body.toString().replace(/\(|\)|;|(topstock)/g, ''));
|
||||
const params = items[transform];
|
||||
const data = (new NeteaseStockTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票组数据
|
||||
* @param codes 需要获取的股票组代码
|
||||
*/
|
||||
async getStocks(codes: string[]): Promise<Stock[]> {
|
||||
const transforms = (new NeteaseExchangeTransform).transforms(codes);
|
||||
|
||||
const url = `https://api.money.126.net/data/feed/${transforms.join(',')},money.api?callback=topstock`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const items = JSON.parse(res.body.toString().replace(/\(|\)|;|(topstock)/g, ''));
|
||||
|
||||
return codes.map(code => {
|
||||
const transform = (new NeteaseExchangeTransform).transform(code);
|
||||
const params = items[transform];
|
||||
const data = (new NeteaseStockTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default Netease;
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Stocks
|
||||
import BaseExchangeTransform from "@stocks/base/exchangeTransform";
|
||||
import BaseExchangeTransform from "@stocks/base/transforms/exchange";
|
||||
|
||||
// Utils
|
||||
import { SZ, HK, US, SH } from "@utils/constant";
|
@ -1,5 +1,5 @@
|
||||
// Stocks
|
||||
import BaseDataTransform from "@stocks/base/dataTransform";
|
||||
import BaseStockTransform from "@stocks/base/transforms/stock";
|
||||
|
||||
// Types
|
||||
import Stock from "types/stock";
|
||||
@ -8,7 +8,7 @@ import Dictionary from "types/dictionary";
|
||||
/**
|
||||
* 网易股票数据解析
|
||||
*/
|
||||
class NeteaseDataTransform extends BaseDataTransform {
|
||||
class NeteaseStockTransform extends BaseStockTransform {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
@ -82,4 +82,4 @@ class NeteaseDataTransform extends BaseDataTransform {
|
||||
}
|
||||
}
|
||||
|
||||
export default NeteaseDataTransform;
|
||||
export default NeteaseStockTransform;
|
@ -1,72 +0,0 @@
|
||||
// Stocks
|
||||
import Base from "@stocks/base/api";
|
||||
import SinaDataTransform from "@stocks/sina/dataTransform";
|
||||
import SinaExchangeTransform from "@stocks/sina/exchangeTransform";
|
||||
|
||||
// Utils
|
||||
import fetch from "@utils/fetch";
|
||||
import iconv from "@utils/iconv";
|
||||
|
||||
// Types
|
||||
import Stock from "types/stock";
|
||||
|
||||
/**
|
||||
* 新浪股票代码接口
|
||||
*/
|
||||
class Sina extends Base {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 需要获取的股票代码
|
||||
*/
|
||||
async getStock(code: string): Promise<Stock> {
|
||||
const transform = (new SinaExchangeTransform).transform(code);
|
||||
|
||||
// 数据获取
|
||||
const url = `https://hq.sinajs.cn/list=${transform}`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const body = iconv.decode(res.body, "gb18030");
|
||||
const rows = body.split(";\n");
|
||||
const row = rows[0];
|
||||
|
||||
// 数据深解析
|
||||
const [_, paramsUnformat] = row.split('=');
|
||||
const params = paramsUnformat.replace('"', '').split(",");
|
||||
const data = (new SinaDataTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票组数据
|
||||
* @param codes 需要获取的股票组代码
|
||||
*/
|
||||
async getStocks(codes: string[]): Promise<Stock[]> {
|
||||
const transforms = (new SinaExchangeTransform).transforms(codes);
|
||||
|
||||
// 数据获取
|
||||
const url = `https://hq.sinajs.cn/list=${transforms.join(',')}`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const body = iconv.decode(res.body, "gb18030");
|
||||
const rows = body.split(";\n");
|
||||
|
||||
return codes.map((code, index) => {
|
||||
// 数据深解析
|
||||
const [_, paramsUnformat] = rows[index].split('=');
|
||||
const params = paramsUnformat.replace('"', '').split(",");
|
||||
const data = (new SinaDataTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default Sina;
|
@ -1,4 +1,72 @@
|
||||
// Stocks
|
||||
import Sina from "@stocks/sina/api";
|
||||
import Base from "@stocks/base";
|
||||
import SinaStockTransform from "@stocks/sina/transforms/stock";
|
||||
import SinaExchangeTransform from "@stocks/sina/transforms/exchange";
|
||||
|
||||
// Utils
|
||||
import fetch from "@utils/fetch";
|
||||
import iconv from "@utils/iconv";
|
||||
|
||||
// Types
|
||||
import Stock from "types/stock";
|
||||
|
||||
/**
|
||||
* 新浪股票代码接口
|
||||
*/
|
||||
class Sina extends Base {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 需要获取的股票代码
|
||||
*/
|
||||
async getStock(code: string): Promise<Stock> {
|
||||
const transform = (new SinaExchangeTransform).transform(code);
|
||||
|
||||
// 数据获取
|
||||
const url = `https://hq.sinajs.cn/list=${transform}`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const body = iconv.decode(res.body, "gb18030");
|
||||
const rows = body.split(";\n");
|
||||
const row = rows[0];
|
||||
|
||||
// 数据深解析
|
||||
const [_, paramsUnformat] = row.split('=');
|
||||
const params = paramsUnformat.replace('"', '').split(",");
|
||||
const data = (new SinaStockTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票组数据
|
||||
* @param codes 需要获取的股票组代码
|
||||
*/
|
||||
async getStocks(codes: string[]): Promise<Stock[]> {
|
||||
const transforms = (new SinaExchangeTransform).transforms(codes);
|
||||
|
||||
// 数据获取
|
||||
const url = `https://hq.sinajs.cn/list=${transforms.join(',')}`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const body = iconv.decode(res.body, "gb18030");
|
||||
const rows = body.split(";\n");
|
||||
|
||||
return codes.map((code, index) => {
|
||||
// 数据深解析
|
||||
const [_, paramsUnformat] = rows[index].split('=');
|
||||
const params = paramsUnformat.replace('"', '').split(",");
|
||||
const data = (new SinaStockTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default Sina;
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Stocks
|
||||
import BaseTransform from "@stocks/base/exchangeTransform";
|
||||
import BaseTransform from "@stocks/base/transforms/exchange";
|
||||
|
||||
// Utils
|
||||
import { SZ, HK, US, SH } from "@utils/constant";
|
@ -1,5 +1,5 @@
|
||||
// Stocks
|
||||
import BaseDataTransform from "@stocks/base/dataTransform";
|
||||
import BaseStockTransform from "@stocks/base/transforms/stock";
|
||||
|
||||
// Utils
|
||||
import { SZ, HK, US, SH } from "@utils/constant";
|
||||
@ -10,7 +10,7 @@ import Stock from "types/stock";
|
||||
/**
|
||||
* 新浪股票数据解析
|
||||
*/
|
||||
class SinaDataTransform extends BaseDataTransform {
|
||||
class SinaStockTransform extends BaseStockTransform {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
@ -139,4 +139,4 @@ class SinaDataTransform extends BaseDataTransform {
|
||||
}
|
||||
}
|
||||
|
||||
export default SinaDataTransform;
|
||||
export default SinaStockTransform;
|
@ -1,71 +0,0 @@
|
||||
// Stocks
|
||||
import Base from "@stocks/base/api";
|
||||
import TencentDataTransform from "@stocks/tencent/dataTransform";
|
||||
import TencentExchangeTransform from "@stocks/tencent/exchangeTransform";
|
||||
|
||||
// Utils
|
||||
import fetch from "@utils/fetch";
|
||||
|
||||
// Types
|
||||
import Stock from "types/stock";
|
||||
|
||||
/**
|
||||
* 腾讯股票代码接口
|
||||
*/
|
||||
class Tencent extends Base {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 需要获取的股票代码
|
||||
*/
|
||||
async getStock(code: string): Promise<Stock> {
|
||||
const transform = (new TencentExchangeTransform).transform(code);
|
||||
|
||||
// 数据获取
|
||||
const url = `https://qt.gtimg.cn/q=${transform}`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const body = res.text;
|
||||
const rows = body.split(";\n");
|
||||
const row = rows[0];
|
||||
|
||||
// 数据深解析
|
||||
const [_, paramsUnformat] = row.split('=');
|
||||
const params = paramsUnformat.replace('"', '').split("~");
|
||||
const data = (new TencentDataTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票组数据
|
||||
* @param codes 需要获取的股票组代码
|
||||
*/
|
||||
async getStocks(codes: string[]): Promise<Stock[]> {
|
||||
const transforms = (new TencentExchangeTransform).transforms(codes);
|
||||
|
||||
// 数据获取
|
||||
const url = `https://qt.gtimg.cn/q=${transforms.join(',')}`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const body = res.text;
|
||||
const rows = body.split(";\n");
|
||||
|
||||
return codes.map((code, index) => {
|
||||
// 数据深解析
|
||||
const [_, paramsUnformat] = rows[index].split('=');
|
||||
const params = paramsUnformat.replace('"', '').split("~");
|
||||
const data = (new TencentDataTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default Tencent;
|
@ -1,4 +1,71 @@
|
||||
// Stocks
|
||||
import Tencent from "@stocks/tencent/api";
|
||||
import Base from "@stocks/base";
|
||||
import TencentStockTransform from "@stocks/tencent/transforms/stock";
|
||||
import TencentExchangeTransform from "@stocks/tencent/transforms/exchange";
|
||||
|
||||
// Utils
|
||||
import fetch from "@utils/fetch";
|
||||
|
||||
// Types
|
||||
import Stock from "types/stock";
|
||||
|
||||
/**
|
||||
* 腾讯股票代码接口
|
||||
*/
|
||||
class Tencent extends Base {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 需要获取的股票代码
|
||||
*/
|
||||
async getStock(code: string): Promise<Stock> {
|
||||
const transform = (new TencentExchangeTransform).transform(code);
|
||||
|
||||
// 数据获取
|
||||
const url = `https://qt.gtimg.cn/q=${transform}`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const body = res.text;
|
||||
const rows = body.split(";\n");
|
||||
const row = rows[0];
|
||||
|
||||
// 数据深解析
|
||||
const [_, paramsUnformat] = row.split('=');
|
||||
const params = paramsUnformat.replace('"', '').split("~");
|
||||
const data = (new TencentStockTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取股票组数据
|
||||
* @param codes 需要获取的股票组代码
|
||||
*/
|
||||
async getStocks(codes: string[]): Promise<Stock[]> {
|
||||
const transforms = (new TencentExchangeTransform).transforms(codes);
|
||||
|
||||
// 数据获取
|
||||
const url = `https://qt.gtimg.cn/q=${transforms.join(',')}`;
|
||||
const res = await fetch.get(url);
|
||||
|
||||
const body = res.text;
|
||||
const rows = body.split(";\n");
|
||||
|
||||
return codes.map((code, index) => {
|
||||
// 数据深解析
|
||||
const [_, paramsUnformat] = rows[index].split('=');
|
||||
const params = paramsUnformat.replace('"', '').split("~");
|
||||
const data = (new TencentStockTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default Tencent;
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Stocks
|
||||
import BaseTransform from "@stocks/base/exchangeTransform";
|
||||
import BaseTransform from "@stocks/base/transforms/exchange";
|
||||
|
||||
// Utils
|
||||
import { SZ, HK, US, SH } from "@utils/constant";
|
@ -1,5 +1,5 @@
|
||||
// Stocks
|
||||
import BaseDataTransform from "@stocks/base/dataTransform";
|
||||
import BaseStockTransform from "@stocks/base/transforms/stock";
|
||||
|
||||
// Types
|
||||
import { Stock } from "types/stock";
|
||||
@ -7,7 +7,7 @@ import { Stock } from "types/stock";
|
||||
/**
|
||||
* 腾讯股票数据解析
|
||||
*/
|
||||
class TencentDataTransform extends BaseDataTransform {
|
||||
class TencentStockTransform extends BaseStockTransform {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
@ -81,4 +81,4 @@ class TencentDataTransform extends BaseDataTransform {
|
||||
}
|
||||
}
|
||||
|
||||
export default TencentDataTransform;
|
||||
export default TencentStockTransform;
|
Loading…
Reference in New Issue
Block a user