Coverage for backend / app / job_email_scraping / gmail.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-17 21:34 +0000

1import re 

2 

3 

4def extract_forwarding_confirmation_link(email_text: str) -> str | None: 

5 """Extract the Gmail forwarding confirmation (approval) link from an email body. 

6 The function is language-independent and relies only on Gmail's stable URL pattern. 

7 It ignores cancellation links (uf-). 

8 :param email_text: Raw email body 

9 :return: Forwarding confirmation link or None if not found""" 

10 

11 urls = re.findall(r"https?://\S+", email_text) 

12 

13 for url in urls: 

14 if "mail-settings.google.com/mail/vf-" in url: 

15 return url 

16 

17 return None 

18 

19 

20def extract_gmail_originator(email_text: str) -> str | None: 

21 """Extract the first @gmail.com address from a Gmail forwarding confirmation email. 

22 :param email_text: Raw email body 

23 :return: Gmail originator email address or None if not found""" 

24 

25 GMAIL_REGEX = r"\b[a-zA-Z0-9._%+-]+@gmail\.com\b" 

26 match = re.search(GMAIL_REGEX, email_text) 

27 return match.group(0) if match else None