@@ -260,9 +260,19 @@ def sort_json(filename) -> None:
260
260
"""
261
261
Sort JSON keys
262
262
"""
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
263
267
264
268
# pandas remove duplicate keys by eventUrl key
265
269
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
+
266
276
df = df .drop_duplicates (subset = 'eventUrl' )
267
277
268
278
# 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:
321
331
"""
322
332
Export to CSV or JSON
323
333
"""
324
-
325
334
if exclusions != '' :
326
335
df = format_response (response , exclusions = exclusions )
327
336
else :
328
337
df = format_response (response )
329
338
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
331
344
Path ('raw' ).mkdir (parents = True , exist_ok = True )
332
345
333
346
if type == 'csv' :
@@ -364,8 +377,14 @@ def export_to_file(response, type: str='json', exclusions: str='') -> None:
364
377
# TODO: disable in prod (use `main.py`)
365
378
def main ():
366
379
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 )
369
388
370
389
# exclude keywords in event name and title (will miss events with keyword in description)
371
390
exclusions = ['36\u00b0 N' , 'Tulsa' , 'Nerdy Girls' , 'Bitcoin' ]
0 commit comments