# pdf_parser.py import re from datetime import datetime from PyPDF2 import PdfReader from config import Config def extract_text_from_pdf(pdf_path=None): if pdf_path is None: pdf_path = Config.PDF_PATH reader = PdfReader(pdf_path) full_text = "" for page in reader.pages: full_text += page.extract_text() return full_text def parse_email_thread(text): """ Splits the raw text into individual email records. Returns a list of dicts with keys: from, to, date, subject, body. """ emails = [] # Split by common email header patterns # We look for "From:" at the start of a line, but sometimes it's "From: " with spaces. parts = re.split(r'(?=^From:)', text, flags=re.MULTILINE) for part in parts: if not part.strip(): continue from_match = re.search(r'^From:\s*(.*?)$', part, re.MULTILINE) to_match = re.search(r'^To:\s*(.*?)$', part, re.MULTILINE) # Try multiple date formats date_match = re.search(r'^(?:Sent|Date):\s*(.*?)$', part, re.MULTILINE) subject_match = re.search(r'^Subject:\s*(.*?)$', part, re.MULTILINE) # Body is everything after the headers – we can split on the first blank line after headers lines = part.splitlines() body_start = 0 for i, line in enumerate(lines): if re.match(r'^(From|To|Sent|Date|Subject|Cc|Bcc|Reply-To):', line, re.I): continue if line.strip() == "": body_start = i + 1 break body = "\n".join(lines[body_start:]) if body_start < len(lines) else "" emails.append({ 'from': from_match.group(1).strip() if from_match else "", 'to': to_match.group(1).strip() if to_match else "", 'date': date_match.group(1).strip() if date_match else "", 'subject': subject_match.group(1).strip() if subject_match else "", 'body': body.strip() }) return emails def extract_days_from_text(text): """Find a phrase like '95 days' and return the integer.""" days_match = re.search(r'(\d+)\s*days', text) if days_match: return int(days_match.group(1)) return None def get_days_without_account(text): """Try to extract from text; otherwise fallback to config start date.""" days = extract_days_from_text(text) if days is not None: return days from datetime import datetime start = Config.get_start_date() return (datetime.now() - start).days