feat: 修改 项目结构、修改 股票代码转换器规则

This commit is contained in:
zhangxiangliang 2020-07-30 02:36:46 +08:00
parent d3e8bce06a
commit 1149fb6ce2
16 changed files with 238 additions and 254 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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";

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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";

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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";

View File

@ -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;