Skip to content

Commit

Permalink
fix: better report filename
Browse files Browse the repository at this point in the history
  • Loading branch information
popstas committed Dec 24, 2020
1 parent d090dc2 commit 6b9d264
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/scrap-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ module.exports = async (baseUrl, options = {}) => {
if (options.webService) {
try {
await saveAsJson(csvPath, jsonPath, options.lang, options.preset, options.defaultFilter);
const jsonName = getJsonName(jsonPath);

if (options.influxdb) {
log('send to InfluxDB...');
Expand All @@ -572,6 +571,11 @@ module.exports = async (baseUrl, options = {}) => {
localDir += userDir + '/';
if (!fs.existsSync(localDir)) fs.mkdirSync(localDir);
}

// remove microseconds if available
const jsonNameLong = getJsonName(jsonPath);
const jsonNameShort = getJsonName(jsonPath, true);
const jsonName = fs.existsSync(localDir + jsonNameShort) ? jsonNameLong : jsonNameShort;
const localPath = localDir + jsonName;
fs.copyFileSync(jsonPath, localPath);

Expand All @@ -592,15 +596,16 @@ module.exports = async (baseUrl, options = {}) => {
}
};

function getJsonName(jsonPath) {
function getJsonName(jsonPath, short = false) {
const offset = new Date().getTimezoneOffset() * 60000;
const dateLocal = new Date(Date.now() - offset)
const date = dateLocal.toISOString().
let date = dateLocal.toISOString().
replace(/:/g, '-').
replace('T', '_').
replace('T', '__').
replace('Z', '');
if (short) date = date.replace(/\.\d+/, '');
// const dateStr = date.slice(0,10);
const name = path.basename(jsonPath).replace(/[^0-9a-zа-я_.-]/ig, '');
const uploadName = date + '_' + name;
const uploadName = date + '__' + name;
return uploadName;
}

0 comments on commit 6b9d264

Please sign in to comment.