Last active
February 3, 2020 16:33
-
-
Save vlad-ds/724fde6db60b6377d923b754658f86b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def add_unsubscribe(mails: List, unsub_links: dict = {}) -> dict: | |
for mail in mails: | |
#getting sender | |
sender_line = mail.split('\n')[0] #first line of the mail | |
sender_words = sender_line.split(' ')[1:] #removes the 'From: ' part | |
sender = ' '.join(sender_words) #recreates the name string | |
if 'Q?' in sender: | |
sender = re.findall('.Q.?(.*?)\?', sender)[0] #removing some char coding | |
#getting unsubscribe links | |
for line in mail.split('\n'): | |
if 'List-Unsubscribe' in line and '<' in line: | |
links = re.findall('<(.*?)>', line) | |
url = mailto = None | |
for link in links: | |
if link.startswith('https'): | |
url = link | |
elif link.startswith('mailto'): | |
mailto = link | |
if any(links): | |
if sender in unsub_links: | |
unsub_links[sender]['count'] += 1 | |
else: | |
unsub_links[sender] = {'count': 1, 'url': url, | |
'mailto': mailto} | |
return unsub_links |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment