Skip to content

Commit

Permalink
Add petition N 121941 and await parse local data
Browse files Browse the repository at this point in the history
  • Loading branch information
xl400v authored Jun 4, 2024
1 parent 7c43028 commit 31d1029
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions json-data-roi.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/usr/bin/env node

const
envTimezone = process.env.TIMEZONE || 'Etc/UTC',
envPathSave = process.env.PATHWAY_SAVE || '/data';
{
PATHWAY_SAVE: envPathSave,
TIMEZONE: envTimezone
} = process.env;

const
fs = require('fs'),
path = require('path'),
unirest = require('unirest');
const
idsROI = [
'121941', // be over 2025-06-01
'119248', // be over 2025-05-18
'117846', // be over 2025-04-09
'117069', // be over 2025-03-20
Expand All @@ -20,58 +24,64 @@ const
'110164' // be over 2024-12-18
];

idsROI.forEach(async (id) => {
idsROI.forEach((id) => {
const dateNow = new Date();
// for you to change easily
const petitionUrl = `https://www.roi.ru/api/petition/${id}.json`;
const pathToData = path.join(__dirname, envPathSave, id) + '.json';

const
petitionUrl = `https://www.roi.ru/api/petition/${id}.json`,
pathToData = path.join(__dirname, (envPathSave ?? '/data'), id) + '.json';
// read data, if needed
let data = [];
if (fs.existsSync(pathToData)) {
data = JSON.parse(fs.readFileSync(pathToData));
}
let result, dataLog = [];

// scrape data, possibly using prior data
async function getData(url) {
if (fs.existsSync(pathToData)) {
dataLog = await JSON.parse(fs.readFileSync(pathToData));
}

try {
const response = await unirest
.get(url)
.headers({
'Accept': 'application/json',
'Content-Type': 'application/json'
});
const jsonResponse = await response.body;
const result = {
dateStamp: dateToString(dateNow, envTimezone),
consCount: jsonResponse.data?.vote.negative,
prosCount: jsonResponse.data?.vote.affirmative,
rapidsCount: jsonResponse.data?.vote.threshold,
unixtimePoll: jsonResponse.data?.pool
};

const jsonResponse = await response.body?.data;
/**
* status.id === 31 - On vote
* status.id === 71 - In archive
*/
if (jsonResponse.data?.status.id === 31) {
console.log('✅ success', jsonResponse.data);

data.push(result);
if (typeof jsonResponse !== 'undefined' && jsonResponse?.status.id === 31) {
result = {
dateStamp: dateToString(dateNow, (envTimezone ?? 'Etc/UTC')),
consCount: jsonResponse?.vote.negative,
prosCount: jsonResponse?.vote.affirmative,
rapidsCount: jsonResponse?.vote.threshold,
unixtimePoll: jsonResponse?.pool
};
console.log(`${url} ✅ Response done`);
} else {
console.log('❌ failure', jsonResponse.data);
console.log(`${url} 🚧 Response undefined`);
}

} catch(error) {
console.error(error);
} catch (error) {
error.message = `${error.message}`;
throw error;

} finally {
await dataLog.push(result);

}
}

// execute and persist data
getData(petitionUrl) // no top level await... yet
.then(() => {
// persist data
fs.writeFileSync(path.resolve(pathToData), JSON.stringify(data, null, 2));
fs.writeFileSync(
path.resolve(pathToData),
JSON.stringify(dataLog, null, 2)
);
});
});

Expand Down

0 comments on commit 31d1029

Please sign in to comment.