Skip to content

Instantly share code, notes, and snippets.

@icantrank
Created September 18, 2017 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icantrank/ee4302cdbe22cd7c9df23d9e3a7d7fcc to your computer and use it in GitHub Desktop.
Save icantrank/ee4302cdbe22cd7c9df23d9e3a7d7fcc to your computer and use it in GitHub Desktop.
Repeating OpenWeather API in node.js with database
let cron = require('node-schedule'), //npm install these or you're gna hae a bad time
loki = require('lokijs'), //loki is db with persistance coz saves json as a file
fetch = require('node-fetch') //browser fetch in node
let locations = ['London','Newcastle','Glasgow'] //or export an arr of locs in another file and require('./locations.js')
let openWeatherKey = '123456789abcdefghijk' //not a reel api key
let openWeatherUrl = (location) => {
return 'http://api.openweathermap.org/data/2.5/weather?q=' + location + ',uk&appid=' + openWeatherKey //change uk if you're grabbing data from elsewhere durr
}
console.log('number of locations: ', locations.length)
cron.scheduleJob('00 */2 * * *', () => { //run every 2 hours (bc there are 2k locations in total, takes 45m to run)
// cron:s? m h dm m dw
//init db
let db = new loki('weather.loki', {
autoload: true,
autosave: true,
autoloadCallback: () => {
//db ready do stuff
for (let i = 0; i < locations.length; ++i) {
setTimeout(() => { // max 1 request per second
fetch(openWeatherUrl(locations[i])).then(res => {
return res.json()
}).then(weather => {
let weatherColl = db.getCollection('weather') //loading n saving the db in the loop bc in my app I can load this from web - I want the data persisted after every req
if (weatherColl == null) {
weatherColl = db.addCollection('weather')
}
let newDate = new Date()
try {
weatherColl.insert({ date: newDate, weatherData: weather })
db.saveDatabase()
console.log('inserted weather #' + i + ' total: ' + weatherColl.count())
} catch (err) {
console.log('err', err)
}
let temp = weather.main.temp + '\xB0F' //degree synmbol
console.log(' Time: ', newDate)
}).catch(err => { //catch fetch errs so we don't kill tha script, or just use forever
console.log('err:', err)
})
}, i * 1000) //prob hacky but stfu, works for 2.7k locations idk
}
db.close()
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment