@@ -78,7 +78,7 @@ def get_gmail_credentials(interactive: bool = True) -> Credentials:
7878 try :
7979 creds = Credentials .from_authorized_user_file (str (token_path ), SCOPES )
8080 except Exception as e :
81- log_error (f"Could not load existing token: { e } " )
81+ log_error (f"[GMAIL] Could not load existing token: { e } " )
8282
8383 # If valid credentials exist, return them
8484 if creds and creds .valid :
@@ -89,10 +89,10 @@ def get_gmail_credentials(interactive: bool = True) -> Credentials:
8989 try :
9090 creds .refresh (Request ())
9191 token_path .write_text (creds .to_json ())
92- log_debug (" ✔ Token refreshed successfully" )
92+ log_debug (f"[GMAIL] ✔ Token refreshed successfully" )
9393 return creds
9494 except Exception as e :
95- log_error (f"Token refresh failed: { e } " )
95+ log_error (f"[GMAIL] Token refresh failed: { e } " )
9696 creds = None
9797
9898 # No valid credentials - need user interaction
@@ -109,10 +109,10 @@ def get_gmail_credentials(interactive: bool = True) -> Credentials:
109109 raise AuthenticationRequiredError (auth_url )
110110
111111 # Interactive mode: open browser for user to authorize
112- log_info (" Opening browser for Gmail authorization..." )
112+ log_info (f"[GMAIL] Opening browser for Gmail authorization..." )
113113 creds = flow .run_local_server (port = 8080 , access_type = "offline" , prompt = "consent" )
114114 token_path .write_text (creds .to_json ())
115- log_success (" Authorization complete, token saved" )
115+ log_success (f"[GMAIL] Authorization complete, token saved" )
116116 return creds
117117
118118
@@ -286,7 +286,7 @@ def extract_events_with_llm(text: str, source: str, publication_date: str = None
286286 return events
287287
288288 except Exception as e :
289- log_error (f"Error extracting events with LLM: { e } " )
289+ log_error (f"[GMAIL] Error extracting events with LLM: { e } " )
290290 return []
291291
292292
@@ -349,7 +349,7 @@ def insert_events_to_db(events: List[Dict]) -> int:
349349 )
350350 inserted_count += 1
351351 except Exception as e :
352- log_error (f"Could not insert event '{ event .get ('event_name' )} ': { e } " )
352+ log_error (f"[GMAIL] Could not insert event '{ event .get ('event_name' )} ': { e } " )
353353
354354 conn .commit ()
355355 finally :
@@ -374,7 +374,7 @@ def sync_email_newsletters_to_sql(interactive: bool = True) -> dict:
374374 # email_errors = [e for e in errors if "EMAIL" in e.upper()]
375375 # if email_errors:
376376 # for error in email_errors:
377- # log_debug(f"✗ Configuration error: {error}")
377+ # log_debug(f"[GMAIL] ✗ Configuration error: {error}")
378378 # stats["errors"].append(error)
379379 # return stats
380380
@@ -383,28 +383,26 @@ def sync_email_newsletters_to_sql(interactive: bool = True) -> dict:
383383 processed_ids = state .get ("processed_email_ids" , [])
384384
385385 # Connect to Gmail API
386- log_debug ("Connecting to Gmail API..." )
387386 try :
388387 service = get_gmail_service (interactive = interactive )
389388 except AuthenticationRequiredError as e :
390389 # Non-interactive mode and auth needed
391390 stats ["auth_required" ] = True
392391 stats ["auth_url" ] = e .auth_url
393392 error_msg = f"Gmail authentication required. Visit: { e .auth_url } "
394- log_error (f"{ error_msg } " )
393+ log_error (f"[GMAIL] { error_msg } " )
395394 stats ["errors" ].append (error_msg )
396395 return stats
397396
398- log_success ("Authenticated with Gmail ." )
397+ log_success (f"[GMAIL] Authenticated ." )
399398
400399 # Get recent newsletters
401- log_debug (f"Scanning inbox for newsletters from last { config .EMAIL_LOOKBACK_DAYS } days..." )
400+ log_debug (f"[GMAIL] Scanning inbox for newsletters from last { config .EMAIL_LOOKBACK_DAYS } days..." )
402401 newsletters = get_recent_newsletters (service , processed_ids , days_back = config .EMAIL_LOOKBACK_DAYS )
403402
404- log_info (f"Found { len (newsletters )} new newsletters to process." )
403+ log_info (f"[GMAIL] Found { len (newsletters )} new newsletters to process." )
405404
406405 if not newsletters :
407- log_info ("No new newsletters. Exiting." )
408406 return stats
409407
410408 all_events = []
@@ -422,60 +420,58 @@ def sync_email_newsletters_to_sql(interactive: bool = True) -> dict:
422420 except Exception :
423421 pub_date = datetime .now ().strftime ("%Y-%m-%d" )
424422
425- log_debug (f"[{ i } /{ len (newsletters )} ] Processing: { subject [:60 ]} ..." )
423+ log_debug (f"[GMAIL] [ { i } /{ len (newsletters )} ] Processing: { subject [:60 ]} ..." )
426424
427425 email_text = extract_text_from_email (msg )
428426 pdf_texts = extract_pdf_attachments (msg )
429427
430428 full_text = email_text
431429 if pdf_texts :
432430 full_text += "\n \n " + "\n \n " .join (pdf_texts )
433- log_debug (f"Found { len (pdf_texts )} PDF attachment(s)" )
431+ log_debug (f"[GMAIL] Found { len (pdf_texts )} PDF attachment(s)" )
434432
435433 if not full_text .strip ():
436- log_debug (" No text content found" )
434+ log_debug (f"[GMAIL] No text content found" )
437435 continue
438436
439437 events = extract_events_with_llm (full_text , source = f"Email: { subject } " , publication_date = pub_date )
440438
441439 if events :
442- log_debug (f"Extracted { len (events )} events" )
440+ log_debug (f"[GMAIL] Extracted { len (events )} events" )
443441 all_events .extend (events )
444442 else :
445- log_debug (" No events found" )
443+ log_debug (f"[GMAIL] No events found" )
446444
447445 processed_ids .append (email_id )
448446 stats ["emails_processed" ] += 1
449447 stats ["events_extracted" ] += len (events )
450448
451449 except Exception as e :
452450 error_msg = f"Error processing email { email_id } : { str (e )} "
453- log_error (f"{ error_msg } " )
451+ log_error (f"[GMAIL] { error_msg } " )
454452 stats ["errors" ].append (error_msg )
455453
456454 if all_events :
457- log_debug (f"Inserting { len (all_events )} events into database..." )
455+ log_debug (f"[GMAIL] Inserting { len (all_events )} events into database..." )
458456 inserted = insert_events_to_db (all_events )
459457 stats ["events_inserted" ] = inserted
460- log_success (f"Inserted { inserted } events successfully" )
458+ log_success (f"[GMAIL] Inserted { inserted } events successfully" )
461459
462460 state ["processed_email_ids" ] = processed_ids [- 1000 :]
463461 save_email_sync_state (state )
464- log_success (" Sync state saved." )
462+ log_success (f"[GMAIL] Sync state saved." )
465463
466464 except Exception as e :
467465 error_msg = f"Fatal error during sync: { str (e )} "
468- log_error (f"{ error_msg } " )
466+ log_error (f"[GMAIL] { error_msg } " )
469467 stats ["errors" ].append (error_msg )
470468
471- log ("\n " + "=" * 60 )
472- log (" Gmail Sync Summary" )
473- log ("=" * 60 )
474- log (f" Emails processed: { stats ['emails_processed' ]} " )
475- log (f" Events extracted: { stats ['events_extracted' ]} " )
476- log (f" Events inserted (SQL): { stats ['events_inserted' ]} " )
477- log (f" Errors: { len (stats ['errors' ])} " )
478- log ("=" * 60 )
469+ log ("[GMAIL] Sync Summary" )
470+ log ("[GMAIL] " + "=" * 60 )
471+ log (f"[GMAIL] Emails processed: { stats ['emails_processed' ]} " )
472+ log (f"[GMAIL] Events extracted: { stats ['events_extracted' ]} " )
473+ log (f"[GMAIL] Events inserted (SQL): { stats ['events_inserted' ]} " )
474+ log (f"[GMAIL] Errors: { len (stats ['errors' ])} " )
479475
480476 return stats
481477
@@ -491,21 +487,21 @@ def sync_email_newsletters_to_sql(interactive: bool = True) -> dict:
491487 try :
492488 if args .auth :
493489 # Just do authentication
494- log_debug (" Running Gmail OAuth authentication..." )
490+ log_debug (f"[GMAIL] Running Gmail OAuth authentication..." )
495491 get_gmail_credentials (interactive = True )
496- log_success (" Authentication successful! Token saved." )
492+ log_success (f"[GMAIL] Authentication successful! Token saved." )
497493 else :
498494 # Run full sync
499495 interactive = not args .non_interactive
500496 sync_email_newsletters_to_sql (interactive = interactive )
501497 except KeyboardInterrupt :
502- log_warning (" Interrupted by user. Exiting..." )
498+ log_warning (f"[GMAIL] Interrupted by user. Exiting..." )
503499 sys .exit (1 )
504500 except AuthenticationRequiredError as e :
505- log_warning (f"Authentication required!" )
506- log_info (f"Visit this URL to authorize: { e .auth_url } " )
507- log_info (" \n Or run: python email_to_calendar_sql.py --auth" )
501+ log_warning (f"[GMAIL] Authentication required!" )
502+ log_info (f"[GMAIL] Visit this URL to authorize: { e .auth_url } " )
503+ log_info (f"[GMAIL] \n Or run: python email_to_calendar_sql.py --auth" )
508504 sys .exit (2 )
509505 except Exception as e :
510- log_error (f"Fatal error: { e } " )
506+ log_error (f"[GMAIL] Fatal error: { e } " )
511507 sys .exit (1 )
0 commit comments