Skip to content

Commit

Permalink
Merge pull request #8 from g-viet/change_BASE_url_for_more_details
Browse files Browse the repository at this point in the history
Change base url for more details
  • Loading branch information
g-viet authored Jul 2, 2019
2 parents 84b3c97 + 2bb6e62 commit 18ba7d6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const INDUSTRY_GROUPS: any = {
"pharmacy": ["DHG"]
}

const BASE_API_URL = 'https://api.vietstock.vn/ta/history';
const BASE_API_URL = 'https://finance.vietstock.vn/company/tradinginfo';

export {
INDUSTRY_GROUPS,
Expand Down
24 changes: 14 additions & 10 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import { Stock } from "./stock";
import { INDUSTRY_GROUPS, BASE_API_URL } from "./constant";
const request = require('request');

const buildApiUrl = (stockCode: string): string => {
const to = Math.floor(Date.now() / 1000);
const from = to - 864000; // 10 days
return `${BASE_API_URL}?symbol=${stockCode}&resolution=D&from=${from}&to=${to}`;
}
const getCurrentTime = (): string => {
const d = new Date();
return `${d.getFullYear()}${d.getMonth() + 1}${d.getDate()}${d.getHours()}${d.getMinutes()}${d.getSeconds()}`;
};

const fetchStock = async (stockCode: string): Promise<Stock | null> => {
const fetchStock = async (stockCode: string): Promise<any> => {
stockCode = stockCode.toLocaleUpperCase();
const apiCall = () => {
return new Promise((resolve, reject) => {
const options = {
url: buildApiUrl(stockCode),
method: 'GET',
json: true
uri: BASE_API_URL,
method: 'POST',
json: true,
headers: {
"Content-Type": "application/json"
},
body: { code: stockCode, s: "1", t: getCurrentTime() }
};
request(options, (err: any, _res: any, body: any) => {
if (err) reject(err);
Expand All @@ -24,7 +28,7 @@ const fetchStock = async (stockCode: string): Promise<Stock | null> => {
}
return apiCall().then((body: any) => {
try {
return new Stock(stockCode, JSON.parse(body));
return new Stock(stockCode, body);
} catch (_err) {
return null;
}
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ program
StockApi.gets(stocks);
});
program
.command('stream <stockCode>')
.command('stream <stocks...>')
.description('get streaming stock price')
.action((stockCode: string) => {
StockApi.stream(stockCode);
.action((stocks: string[]) => {
StockApi.stream(stocks);
});
program
.command('group [groupCode]')
.description('get stock prices of industry group')
.action((groupCode: string) => {
StockApi.group(groupCode);
});

program.parse(process.argv);
16 changes: 10 additions & 6 deletions src/stock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ export class Stock {
time: Date;

constructor(stockCode: string, stockPrices: any) {
this.code = stockCode.toLocaleUpperCase();
this.currentPrice = stockPrices['c'];
this.volume = stockPrices['v'];
this.openPrice = stockPrices['o'];
this.highestPrice = stockPrices['h'];
this.lowestPrice = stockPrices['l'];
this.code = stockCode;
this.currentPrice = stockPrices.LastPrice;
this.volume = stockPrices.TotalVol;
this.openPrice = stockPrices.OpenPrice;
this.highestPrice = stockPrices.HighestPrice;
this.lowestPrice = stockPrices.LowestPrice;
this.time = new Date();
}

static printfHeader() {
console.log(`Stock\tPrice\t\tVolume\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`);
}

printf () {
const volume = this.f(this.volume);
console.log(`${this.code}\t${this.f(this.currentPrice)}\t\t${volume}${volume.length >= 9 ? '\t' : '\t\t' }${this.f(this.openPrice)}\t\t${this.f(this.highestPrice)}\t\t${this.f(this.lowestPrice)}\t\t${this.time.toLocaleTimeString()}`);
Expand Down
22 changes: 15 additions & 7 deletions src/stock_api.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { Helper } from './helper';
import { Stock } from './stock';

const gets = (stockCodes: string[]) => {
console.log(`Stock\tPrice\t\tVolume\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`);
Stock.printfHeader();
try {
return Promise.all(stockCodes.map((code) => {
return Helper.fetchStock(code).then(stock => stock && stock.printf());
}));
Promise.all(stockCodes.map((code) => {
return Helper.fetchStock(code);
})).then(stocks =>
stocks.map(stock => stock && stock.printf())
);
} catch (err) {
throw err;
}
}

const stream = (stockCode: string) => {
console.log(`Stock\tPrice\t\tVolume\t\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`);
const stream = (stockCodes: string[]) => {
Stock.printfHeader();
setInterval(() => {
Helper.fetchStock(stockCode).then(stock => stock && stock.printf());
Promise.all(stockCodes.map((code) => {
return Helper.fetchStock(code);
})).then(stocks =>
stocks.map(stock => stock && stock.printf())
);
// Helper.fetchStock(stockCode).then(stock => stock && stock.printf());
}, 2000);
}

Expand Down

0 comments on commit 18ba7d6

Please sign in to comment.