-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
142 lines (105 loc) · 4.52 KB
/
Copy pathmain.py
File metadata and controls
142 lines (105 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import re
import os
from collections import Counter
# Common prefixes that indicate an automated/system email rather than a person
SYSTEM_PREFIXES = (
"no-reply", "noreply", "donotreply", "do-not-reply",
"notifications", "notification", "alerts", "alert",
"automated", "system", "mailer", "postmaster",
"bounce", "bounces", "newsletter",
)
def extract_emails(input_file):
# Check if file exists
if not os.path.exists(input_file):
print(f"[!] File not found: {input_file}")
return []
with open(input_file, "r", encoding="utf-8") as f:
content = f.read()
# Regex pattern to match email addresses
pattern = r'[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}'
emails = re.findall(pattern, content)
# Remove duplicates while preserving order
seen = set()
unique_emails = []
for email in emails:
if email.lower() not in seen:
seen.add(email.lower())
unique_emails.append(email)
return unique_emails
def categorize_email(email):
"""Return 'system' if the email looks automated, else 'personal'."""
local_part = email.split("@")[0].lower()
for prefix in SYSTEM_PREFIXES:
if local_part.startswith(prefix) or local_part == prefix:
return "system"
return "personal"
def get_domain(email):
return email.split("@")[1].lower()
def build_stats(emails):
categories = Counter()
domains = Counter()
domain_categories = {}
for email in emails:
category = categorize_email(email)
domain = get_domain(email)
categories[category] += 1
domains[domain] += 1
domain_categories.setdefault(domain, []).append(category)
return categories, domains, domain_categories
def save_emails(emails, output_file="extracted_emails.txt"):
categories, domains, domain_categories = build_stats(emails)
with open(output_file, "w", encoding="utf-8") as f:
f.write("Email Extraction Report\n")
f.write("=" * 40 + "\n")
f.write(f"Total emails found: {len(emails)}\n")
f.write("=" * 40 + "\n\n")
# ── Category breakdown ──────────────────────────────────────────────
f.write("Category Breakdown:\n")
f.write(f" Personal/Work emails: {categories['personal']}\n")
f.write(f" System/No-reply emails: {categories['system']}\n")
f.write("\n")
# ── Domain breakdown ─────────────────────────────────────────────────
f.write("Domain Breakdown:\n")
for domain, count in domains.most_common():
f.write(f" {domain}: {count}\n")
f.write("\n")
# ── Personal/Work emails ─────────────────────────────────────────────
f.write("-" * 40 + "\n")
f.write("Personal/Work Emails\n")
f.write("-" * 40 + "\n")
for email in emails:
if categorize_email(email) == "personal":
f.write(email + "\n")
f.write("\n")
# ── System/No-reply emails ──────────────────────────────────────────
f.write("-" * 40 + "\n")
f.write("System/No-reply Emails\n")
f.write("-" * 40 + "\n")
for email in emails:
if categorize_email(email) == "system":
f.write(email + "\n")
return output_file
def main():
print("=" * 40)
print(" Email Extractor — Task Automation")
print("=" * 40)
input_file = input("\nEnter the .txt filename (e.g. sample.txt): ").strip()
print(f"\n[*] Scanning {input_file} for email addresses...")
emails = extract_emails(input_file)
if not emails:
print("[!] No email addresses found.")
return
print(f"[+] Found {len(emails)} unique email(s):")
for email in emails:
print(f" {email}")
categories, domains, _ = build_stats(emails)
print("\n[*] Category Breakdown:")
print(f" Personal/Work: {categories['personal']}")
print(f" System/No-reply: {categories['system']}")
print("\n[*] Top Domains:")
for domain, count in domains.most_common(5):
print(f" {domain}: {count}")
output_file = save_emails(emails)
print(f"\n[✓] Saved to: {output_file}")
if __name__ == "__main__":
main()