Skip to content

Instantly share code, notes, and snippets.

@aitorjs
Created July 16, 2016 23:40
Show Gist options
  • Save aitorjs/d6d3b35d1ea96fcbd5c0d1586758c9b3 to your computer and use it in GitHub Desktop.
Save aitorjs/d6d3b35d1ea96fcbd5c0d1586758c9b3 to your computer and use it in GitHub Desktop.
'use strict'
var request = require('request')
var cheerio = require('cheerio')
function fetch(day, month, lang, events) {
return new Promise(function(resolve) {
var events = []
var metadata = []
request(`https://${lang}.wikipedia.org/wiki/${day}_de_${month}`, function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html)
var li = $('div#toc.toc').next().next().next().children()
$(li).each(function(i, element){
let date = $(this).children().html()
let data = $(this).children().parent().text().slice(2)
if (!isNaN(parseInt(date))) {
let scraped = {
date: parseInt(date),
data: capitalizeFirstLetter(data)
}
metadata.push(scraped)
}
})
Promise.resolve(metadata)
}
})
})
}
var lastDay = getLastDayOfMonth(2016)
var month = 6 // meterlo en for luego
var events = []
for (var day = 1; day <= 2; day++) {
fetch(day, 'julio', 'es', events).then(function(res) {
console.log(res)
})
}
// Utils
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function getLastDayOfMonth(year) {
var lastDay = []
for (var month = 1; month <= 12; month++) {
lastDay[month] = new Date(year, month, 0).getDate();
}
return lastDay
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment