-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
68 lines (54 loc) · 1.96 KB
/
main.py
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
from dotenv import load_dotenv
import imaplib
import email
import os
from bs4 import BeautifulSoup
import requests
load_dotenv()
username = os.getenv("EMAIL")
password = os.getenv("PASSWORD")
def connect_to_mail():
mail = imaplib.IMAP4_SSL("imap.gmail.com")
mail.login(username, password)
mail.select("inbox")
return mail
def extract_links_from_html(html_content):
soup = BeautifulSoup(html_content, "html.parser")
links = [link["href"] for link in soup.find_all("a", href=True) if "unsubscribe" in link["href"].lower()]
return links
def click_link(link):
try:
response = requests.get(link)
if response.status_code == 200:
print("Successfully visited", link)
else:
print("Failed to visit", link, "error code", response.status_code)
except Exception as e:
print("Error with", link, str(e))
def search_for_email():
mail = connect_to_mail()
_, search_data = mail.search(None, '(BODY "unsubscribe")')
data = search_data[0].split()
links = []
for num in data:
_, data = mail.fetch(num, "(RFC822)")
msg = email.message_from_bytes(data[0][1])
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() == "text/html":
html_content = part.get_payload(decode=True).decode()
links.extend(extract_links_from_html(html_content))
else:
content_type = msg.get_content_type()
content = msg.get_payload(decode=True).decode()
if content_type == "text/html":
links.extend(extract_links_from_html(content))
mail.logout()
return links
def save_links(links):
with open("links.txt", "w") as f:
f.write("\n".join(links))
links = search_for_email()
for link in links:
click_link(link)
save_links(links)