Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/scrape #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions .env-template
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
# lista de eventos que no queremos que aparezcan (separados por coma)
# 19155277 = Infobyte (https://www.meetup.com/infobyte/)
BLACK_LIST=19155277

# 15 minutos de tiempo de vida para los pedidos cacheados
CACHE_EXPIRATION=900000

# 34 = Tecnología
# en el caso de querer usar más de 1, separarlos por coma
CATEGORIES=34

# latitud y longitud que apuntan a Cabildo y Juan B. Justo
LAT=-34.578175
LON=-58.426516

# https://secure.meetup.com/meetup_api/key/
MEETUP_API_KEY=1234567890

NODE_ENV=development

# rango en millas (partiendo desde latitud y longitud) que se va a tener en cuenta
# para buscar meetups
RADIUS=10

# lista de eventos a buscar que sabemos que no entran en el rango (separados por coma)
# WHITE_LIST=
RADIUS=10
93 changes: 56 additions & 37 deletions lib/scrape-events.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,65 @@
const cheerio = require('cheerio')
const got = require('got')
const moment = require('moment')
let yup = require('yup')

let schema = yup.array().of(
yup.object().shape({
date: yup.date().required(),
eventName: yup.string().required(),
eventLink: yup.string().url().required(),
place: yup.string()
})
)

module.exports = async function scrapeEvents() {
const { RADIUS } = process.env
const { body } = await got(
`https://www.meetup.com/es-ES/find/events/tech/?allMeetups=false&radius=${RADIUS}&userFreeform=buenos+ai&mcId=c1000296&mcName=Buenos+Aires%2C+AR`
)
const $ = cheerio.load(body)
const days = $('li.event-listing-container-li')
const dayInfo = $('li.date-indicator')
const eventsArray = []
days.each((dayIndex, li) => {
const events = $(li).find('ul.event-listing-container > li')
events.each((_, li) => {
$(li).each((_, e) => {
const eventDateElement = dayInfo.get(dayIndex)
let eventDate
if (eventDateElement) {
const {
'data-year': year,
'data-month': month,
'data-day': day
} = dayInfo.get(dayIndex).attribs
eventDate = `${year}-${month}-${day}`
} else {
eventDate = moment().format('YYYY-MM-DD')
}
eventsArray.push({
date:
moment.utc(
`${eventDate} ${$(e).find('div a').first().text().trim()}`,
'YYYY-MM-DD HH:mm'
),
eventName:
`${$(e).find('div[itemprop="location"]').text().trim()} - ${$(e).find('a.event span[itemprop="name"]').text().trim()}`,
eventLink: $(e).find('a.event').prop('href'),
place: '',
attendeeCount: $(e).find('div.attendee-count').text().replace(/\n+/g, ' ')
try {
const { RADIUS } = process.env
const { body } = await got(
`https://www.meetup.com/es-ES/find/events/tech/?allMeetups=false&radius=${RADIUS}&userFreeform=buenos+ai&mcId=c1000296&mcName=Buenos+Aires%2C+AR`
)
const $ = cheerio.load(body)
const days = $('li.event-listing-container-li')
const dayInfo = $('li.date-indicator')
const eventsArray = []
days.each((dayIndex, li) => {
const events = $(li).find('ul.event-listing-container > li')
events.each((_, li) => {
$(li).each((_, e) => {
const eventDateElement = dayInfo.get(dayIndex)
let eventDate
if (eventDateElement) {
const {
'data-year': year,
'data-month': month,
'data-day': day
} = dayInfo.get(dayIndex).attribs
eventDate = `${year}-${month}-${day}`
} else {
eventDate = moment().format('YYYY-MM-DD')
}
eventsArray.push({
date:
moment.utc(
`${eventDate} ${$(e).find('div a').first().text().trim()}`,
'YYYY-MM-DD HH:mm'
),
eventName:
`${$(e).find('div[itemprop="location"]').text().trim()} - ${$(e).find('a.event span[itemprop="name"]').text().trim()}`,
eventLink: $(e).find('a.event').prop('href'),
place: ''
})
})
})
})
})
return eventsArray
const isValid = schema.isValidSync(eventsArray)
console.log('schema valido: ', isValid)
if (isValid) {
return eventsArray
} else {
return []
}
} catch (e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hagamos un console.error con el error, además de devolver el array vacío

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sino no vamos a saber por que rompe, que van a ser la mayoría de los casos

return []
}
}
160 changes: 126 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
"micro-dev": "^3.0.0"
},
"dependencies": {
"cheerio": "^1.0.0-rc.3",
"cheerio": "0.22.0",
"dotenv": "^8.0.0",
"got": "^9.6.0",
"memory-cache": "^0.2.0",
"micro": "^9.3.4",
"micro-cors": "^0.1.1",
"moment": "^2.24.0"
"moment": "^2.24.0",
"yup": "^0.27.0"
},
"repository": {
"type": "git",
Expand Down