-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.py
58 lines (40 loc) · 1.42 KB
/
events.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Functions to get and parse meetup events from the web"""
import os
import logging
import datetime
from requests_html import HTMLSession
logger = logging.getLogger(__name__)
def get_events():
"""Get events from meetup website, parse them and return a list of dicts"""
group = os.environ['MEETUP_GROUP']
url = f'https://www.meetup.com/{group}/events/'
resp = HTMLSession().get(url, timeout=10)
if not resp.ok:
raise ConnectionError(
f"Received http {resp.status_code} when connecting to {url}"
)
events = resp.html.find('.flex.flex--column.flex--spaceBetween')
events = [parse_event(event) for event in events]
return events
def parse_event(event_html):
"""Parse each event card and returns a dict"""
event = {}
# date
aux = event_html.xpath('.//time/@datetime')[0][:-3]
aux = int(aux)
event['date'] = datetime.datetime.fromtimestamp(aux)
# title
event['title'] = event_html.xpath('.//a/text()')[0]
# description
aux = event_html.xpath(
'.//p[contains(@class, "text--small")]/text()'
)
aux = [text for text in aux if len(text) > 1]
event['description'] = '\n'.join(aux)
# url
event['url'] = event_html.url + \
event_html.xpath('.//a/@href')[0].split('/')[-2] + '/'
# address
event['location'] = event_html.xpath('.//address//text()')[0]
logger.debug(f'Event:\n{event}')
return event