From 35d915b805e71be8e3afa226847aba0cf300e210 Mon Sep 17 00:00:00 2001 From: Honda XL400V Transalp <49412889+xl400v@users.noreply.github.com> Date: Wed, 15 May 2024 14:52:22 +0000 Subject: [PATCH] Create processing list ids, create timezone definition --- json-data-roi.js | 112 ++++++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 50 deletions(-) diff --git a/json-data-roi.js b/json-data-roi.js index a2de98bf3..4ec6c7490 100644 --- a/json-data-roi.js +++ b/json-data-roi.js @@ -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; }