Skip to content

Instantly share code, notes, and snippets.

@gerhardberger
Created October 27, 2016 16:34
Show Gist options
  • Save gerhardberger/55787d5db68731720faeb0e28858b5b8 to your computer and use it in GitHub Desktop.
Save gerhardberger/55787d5db68731720faeb0e28858b5b8 to your computer and use it in GitHub Desktop.
const got = require('got')
const cheerio = require('cheerio')
const { writeFile } = require('fs')
let data = []
function fetch (year, month, cb) {
const url = `https://www.metnet.hu/?m=napi-adatok&sub=4&pid=6321&date=${
year}-${month < 10 ? '0' + month : month}-01`
got.get(url).then(res => {
const $ = cheerio.load(res.body)
const trs = $('.sblokk342 table tr')
for (let i = 0; i < trs.length; ++i) {
if (i === 0 || trs[i].children.length !== 13) continue
const tds = $(trs[i]).find('td')
const day = Number($(tds[0]).text())
const min = Number($(tds[1]).html().replace(/ &#xFFFD;C/i, '').replace(',', '.'))
const max = Number($(tds[2]).html().replace(/ &#xFFFD;C/i, '').replace(',', '.'))
const avg = (min + max) / 2
const o = {
year: year,
month: month,
day, min, max, avg
}
data.push(o)
}
if (month < 12) {
fetch(year, month + 1, cb)
} else {
if (year < 2015) fetch(year + 1, 1, cb)
else cb()
}
}).catch(err => console.error(err))
}
fetch(2000, 4, () => {
writeFile('data.json', JSON.stringify(data), 'utf8', err => {
if (err) return console.error(err)
console.log('written')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment