Last active
June 30, 2019 08:11
-
-
Save SnitchRUS66/36979ed8bd2cad4564b9c96579f3838b to your computer and use it in GitHub Desktop.
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
const Promise = require('bluebird') | |
// link to API (just sign up to get the key )): | |
// https://rapidapi.com/divad12/api/numbers-1 | |
const https = require('https') | |
//======================= jsonGet Function =======================// | |
function jsonGet (day=1, month=0){ | |
const options = { | |
host: 'numbersapi.p.rapidapi.com', | |
port: 443, | |
path: `/${month}/${day}/date?fragment=true&json=true`, | |
headers: { | |
'X-RapidAPI-Host': 'numbersapi.p.rapidapi.com', | |
'X-RapidAPI-Key': 'API_KEY' | |
} | |
} | |
return new Promise((resolve, reject) => { | |
https.get(options, function(res) { | |
let body = '' | |
res.on('data', function(chunk) { | |
body += chunk | |
}) | |
res.on('end', function() { | |
resolve(body) | |
}) | |
}).on('error', function(e) { | |
reject(e) | |
}) | |
}) | |
} | |
function getDaysArray(startDate, endDate) { | |
let dateArray = [] | |
for( let dt=startDate; dt<=endDate; dt.setDate(dt.getDate()+1)){ | |
const date = new Date(dt) | |
dateArray.push({day: date.getDate(), month: date.getMonth()}) | |
} | |
return dateArray | |
} | |
async function getAllPrograms(callback) { | |
const YEARS = 10 | |
const startDate = new Date(new Date().setFullYear(new Date().getFullYear() - YEARS)) | |
const endDate = new Date() | |
const dates = getDaysArray(startDate, endDate) | |
let requestCounter = 0 | |
await Promise.map(dates, | |
date => jsonGet(date.day, date.month) | |
.then(responseData =>{ | |
requestCounter++ | |
const data = JSON.parse(responseData) | |
if (data && data.number && data.year && data.text) { | |
console.log(`${requestCounter} :: Year: ${data.year}, fact №: ${data.number}, - ${data.text.slice(0, 50)}`) | |
} else { | |
console.log(`${requestCounter} :: REQUEST FAIL`) | |
} | |
}), | |
{ concurrency: 365 } | |
) | |
callback() | |
} | |
const START_TIME = new Date() | |
process.stdout.write('Test start') | |
getAllPrograms(function(err){ | |
if (err) console.log(err) | |
console.log(`============================ time ${(new Date() - START_TIME)/1000}s ============================`) | |
process.exit() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment