Skip to content

Commit 0881040

Browse files
committed
Format location of events
1 parent 1e4766d commit 0881040

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

tools/events-automation/generate_events_meetup.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from jwt_auth import generate_signed_jwt
77
from urllib.parse import urlsplit
8+
from geopy.geocoders import Nominatim
89
from event import Event
910

1011
def authenticate():
@@ -27,9 +28,14 @@ def authenticate():
2728
print("Response:", response.text)
2829
return None, None
2930

31+
# Initialize variables for querying and formatting data:
32+
ACCESS_TOKEN, REFRESH_TOKEN = authenticate()
33+
# initialize Nominatim API
34+
GEOLOCATOR = Nominatim(user_agent="TWiR")
35+
3036
def fetch_groups(endCursor=""):
3137
URL = "https://api.meetup.com/gql"
32-
access_token, refresh_token = authenticate()
38+
access_token, refresh_token = ACCESS_TOKEN, REFRESH_TOKEN
3339

3440
if not access_token:
3541
print("Authentication failed, cannot proceed to fetch events.")
@@ -138,7 +144,7 @@ def get_known_rush_groups(fileName):
138144
def get_20_events(groups) -> list[Event]:
139145
events = []
140146
URL = "https://api.meetup.com/gql"
141-
access_token, refresh_token = authenticate()
147+
access_token, refresh_token = ACCESS_TOKEN, REFRESH_TOKEN
142148

143149
if not access_token:
144150
print("Authentication failed, cannot proceed to fetch events.")
@@ -196,29 +202,40 @@ def get_20_events(groups) -> list[Event]:
196202
for edge in edges:
197203
node = edge["node"]
198204
if node:
199-
name = node["title"]
200-
lat, lng = 0, 0
201-
virtual = True
202205
venue = node["venue"]
203-
# TODO: Handle events don't have venue, flagging the events and they will have to be check manually, or putting them in separate list to check
206+
# TODO: Handle events don't have venue:
207+
# 1. Flagging the events and they will have to be check manually,
208+
# 2. Putting them in separate list to check
204209
# (for now ignore those events)
205210
if venue:
206-
lat, lng = venue["lat"], venue["lng"]
211+
name = node["title"]
212+
virtual = True
207213
if venue["venueType"] != "online":
208214
virtual = False
209-
location = f"{lat}, {lng}" # TODO: Use GeoPy to convert(lat, long) to address/location
215+
address = (GEOLOCATOR.reverse(str(venue["lat"]) +","+ str(venue["lng"]))).raw["address"]
216+
location = format_location(address)
210217
date = node["dateTime"]
211218
url = node["eventUrl"]
212219
organizerName = group.get("name", urlName)
213220
organizerUrl = group["link"]
214-
print(f"Event({name}, location={location}\ndate={date}, url={url}, virtual={virtual}\norganizerName={organizerName}, organizerUrl={organizerUrl}\n")
221+
# print(f"Event({name}, location={location}\ndate={date}, url={url}, virtual={virtual}\norganizerName={organizerName}, organizerUrl={organizerUrl}\n")
215222
events.append(Event(name, location, date, url, virtual, organizerName, organizerUrl))
216223
return events
217224

225+
def format_location(address):
226+
if not address:
227+
return "No location"
228+
229+
# Components in the order for location
230+
components = ['road', 'city', 'state', 'postcode', 'country']
231+
232+
# Get available components
233+
location = [address.get(component, "") for component in components if address.get(component)]
234+
return ', '.join(location) if location else "No location"
235+
218236
def get_events() -> list[Event]:
219237
events_meetup_groups = get_20_events(get_rush_groups())
220238
events_known_groups = get_20_events(get_known_rush_groups("rust_meetup_groups.csv"))
221239
return events_meetup_groups + events_known_groups
222240

223-
get_events()
224-
# print(len(get_events()))
241+
print(len(get_events()))

0 commit comments

Comments
 (0)