forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
auto detect tag as company stock code TryGhost#5
- Loading branch information
tamvm
committed
Sep 24, 2019
1 parent
e1bd34c
commit c218e8e
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
const companies = {}; | ||
|
||
var val = require('./companies.json'); | ||
for (let i = 0; i < val.length; i++) { | ||
let c = val[i]; | ||
companies[c.Code.toLowerCase()] = {code: c.Code, name: c.Name, exchange: c.Exchange, industry: c.IndustryName}; | ||
} | ||
// free mem | ||
delete val; | ||
|
||
|
||
const CompanyHandler = { | ||
get: function(code) { | ||
console.log('check code ', code, companies[code]); | ||
if (code) { | ||
return companies[code.toLowerCase()]; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
module.exports = CompanyHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// const fetch = require("node-fetch"); | ||
const request = require('request'); | ||
const fs = require('fs'); | ||
|
||
function _fetchCompanies(page, pageSize) { | ||
console.log('fetch companies page = ' + page + ' pageSize = ' + pageSize); | ||
let url = 'https://finance.vietstock.vn/data/corporateaz', | ||
data = `catID=0&industryID=0&page=${page}&pageSize=${pageSize}&type=0&code=&businessTypeID=0&orderBy=Code&orderDir=ASC`, | ||
config = { | ||
url: url, | ||
method: 'post', | ||
body: data, | ||
headers: { | ||
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8', | ||
'Referrer': 'https://finance.vietstock.vn/doanh-nghiep-a-z?page=3', | ||
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' | ||
}, | ||
credentials: 'include' | ||
}; | ||
return new Promise(resolve => { | ||
request(config, (err, resp, body) => { | ||
resolve(JSON.parse(body)); | ||
}) | ||
|
||
}); | ||
|
||
} | ||
|
||
function fetchCompanies() { | ||
let page = 1, | ||
pageSize = 50, | ||
result = []; | ||
|
||
|
||
return new Promise(resolve => { | ||
let callback = companies => { | ||
if (companies && companies.length > 0) { | ||
console.log('fetched ' + companies.length + '. Continue'); | ||
result = result.concat(companies); | ||
page = page + 1; | ||
_fetchCompanies(page, pageSize).then(callback); | ||
} | ||
else { | ||
resolve(result); | ||
} | ||
}; | ||
|
||
_fetchCompanies(page, pageSize) | ||
.then(callback); | ||
}) | ||
|
||
} | ||
|
||
fetchCompanies().then(companies => { | ||
console.log('total companies = ', companies.length); | ||
fs.writeFile('../core/server/data/crawled/companies.json', JSON.stringify(companies), err => { | ||
if (err) throw err; | ||
}); | ||
}) |