Skip to content

Commit 306f690

Browse files
fix (meetup_query.py): error handling
Handles `KeyError: 'date'` traceback for weeks without events
1 parent 072e905 commit 306f690

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

app/meetup_query.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,19 @@ def sort_json(filename) -> None:
260260
"""
261261
Sort JSON keys
262262
"""
263+
# Check if file exists and has content
264+
if not os.path.exists(filename) or os.stat(filename).st_size == 0:
265+
print(f"{Fore.YELLOW}{warning:<10}{Fore.RESET}No events found to sort")
266+
return
263267

264268
# pandas remove duplicate keys by eventUrl key
265269
df = pd.read_json(filename, orient='records')
270+
271+
# Check if DataFrame is empty
272+
if df.empty:
273+
print(f"{Fore.YELLOW}{warning:<10}{Fore.RESET}No events found to sort")
274+
return
275+
266276
df = df.drop_duplicates(subset='eventUrl')
267277

268278
# replace '1-07-19 17:00:00' with current year '2022-07-19 17:00:00' via regex
@@ -321,13 +331,16 @@ def export_to_file(response, type: str='json', exclusions: str='') -> None:
321331
"""
322332
Export to CSV or JSON
323333
"""
324-
325334
if exclusions != '':
326335
df = format_response(response, exclusions=exclusions)
327336
else:
328337
df = format_response(response)
329338

330-
# create directory if it doesn't exist
339+
# If DataFrame is empty, return early
340+
if df.empty:
341+
return
342+
343+
# Create directory if it doesn't exist
331344
Path('raw').mkdir(parents=True, exist_ok=True)
332345

333346
if type == 'csv':
@@ -364,8 +377,14 @@ def export_to_file(response, type: str='json', exclusions: str='') -> None:
364377
# TODO: disable in prod (use `main.py`)
365378
def main():
366379
tokens = gen_token()
367-
access_token = tokens['access_token']
368-
# refresh_token = tokens['refresh_token']
380+
if not tokens:
381+
print(f"{Fore.RED}{error:<10}{Fore.RESET}Failed to get access tokens")
382+
sys.exit(1)
383+
384+
access_token = tokens.get('access_token')
385+
if not access_token:
386+
print(f"{Fore.RED}{error:<10}{Fore.RESET}No access token in response")
387+
sys.exit(1)
369388

370389
# exclude keywords in event name and title (will miss events with keyword in description)
371390
exclusions = ['36\u00b0N', 'Tulsa', 'Nerdy Girls', 'Bitcoin']

0 commit comments

Comments
 (0)