Skip to content

Commit

Permalink
Create processing list ids, create timezone definition
Browse files Browse the repository at this point in the history
  • Loading branch information
xl400v authored May 15, 2024
1 parent 4c5583b commit 35d915b
Showing 1 changed file with 62 additions and 50 deletions.
112 changes: 62 additions & 50 deletions json-data-roi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,83 @@
const fs = require('fs');
const path = require('path');
const unirest = require('unirest');
const idsROI = '116733';
const idsROI = [
'116733'
];

// for you to change easily
const dataFolder = '/data/roi';
const now = new Date();
const pathToData = path.join(__dirname, dataFolder, idsROI) + '.json';

// read data, if needed
let data = [];
if (fs.existsSync(pathToData)) {
data = JSON.parse(fs.readFileSync(pathToData));
}

// scrape data, possibly using prior data
async function getData() {
const url = `https://www.roi.ru/api/petition/${idsROI}.json`;
idsROI.forEach(async (id) => {
const dateNow = new Date();
// for you to change easily
const petitionUrl = `https://www.roi.ru/api/petition/${id}.json`;
const tzName = 'Europe/Moscow';
const dataFolder = '/data/roi';
const pathToData = path.join(__dirname, dataFolder, id) + '.json';

// read data, if needed
let data = [];
if (fs.existsSync(pathToData)) {
data = 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(now),
consCount: jsonResponse.data?.vote.negative,
prosCount: jsonResponse.data?.vote.affirmative,
rapidsCount: jsonResponse.data?.vote.threshold,
unixtimePoll: jsonResponse.data?.pool
};

/**
* status.id === 31 - On vote
* status.id === 71 - In archive
*/
if (jsonResponse.data?.status.id === 31) {
console.log('✅ success', jsonResponse.data);
// scrape data, possibly using prior data
async function getData(url) {
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, tzName),
consCount: jsonResponse.data?.vote.negative,
prosCount: jsonResponse.data?.vote.affirmative,
rapidsCount: jsonResponse.data?.vote.threshold,
unixtimePoll: jsonResponse.data?.pool
};

data.push(result);
} else {
console.log('❌ failure', jsonResponse.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);
} else {
console.log('❌ failure', jsonResponse.data);
}

} catch(error) {
console.error(error);
}

} catch(error) {
console.error(error);
}

}

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

/**
*
* utils
*
*/
function dateToString(ts) {
function dateToString(ts, tz) {
const utcDate = new Date(ts.toLocaleString('en-US', { timeZone: 'UTC' }));
const tzDate = new Date(ts.toLocaleString('en-US', { timeZone: tz }));
const offset = utcDate.getTime() - tzDate.getTime();
ts.setTime(ts.getTime() - offset);

const year = ts.getUTCFullYear();
const month = (ts.getUTCMonth() + 1).toString().padStart(2, '0');
const day = ts.getUTCDate().toString().padStart(2, '0');
const hour = ts.getUTCHours().toString().padStart(2, '0');
const minut = ts.getUTCMinutes().toString().padStart(2, '0');
const result = `${year}-${month}-${day} ${hour}:${minut}`;
return result;
const local = `${year}-${month}-${day} ${hour}:${minut}`;
return local;
}

0 comments on commit 35d915b

Please sign in to comment.