-
Notifications
You must be signed in to change notification settings - Fork 5
/
Gmail Extractor.py
192 lines (152 loc) · 6.02 KB
/
Gmail Extractor.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
# coding: utf-8
# This code -
# 1. Extracts emails from Gmail from 'Inbox' and 'Spam' folders in .txt format.<br>
# 2. Then converts each folder to Excel files.<br>
# 3. Adds 'spam' and 'non-spam' Labels and 1/0 Label_Numbers.<br>
# 4. Merges both Excel files into one and shuffles them
# **Important Note : enable 'Less Secure App' access for the code to work**<br>
# https://myaccount.google.com/lesssecureapps
import os, re
import pandas as pd
import codecs
from getpass import getpass
import imaplib, email
from bs4 import BeautifulSoup
from nltk import word_tokenize
class emails:
def __init__(self,username,password):
self.mail = imaplib.IMAP4_SSL('imap.gmail.com')
self.mail.login(username, password)
self.select_folder('Inbox',True)
def select_folder(self,folder, read_only):
self.mail.select(folder,readonly=read_only)
def get_all_emails(self):
result,data=self.mail.uid('search',None,'All')
return result,data[0].decode('utf-8').split()
def raw_email(self,uid):
result, data = self.mail.uid('fetch', uid, '(RFC822)')
return result,data[0][1]
def __clean__(self,text):
text = text.replace(' ', ' ')
text = text.replace('</n', '<')
text = text.replace('{*}', '')
#replacing patterns
text = re.sub(r'<[^>]+>', '', text)
text = re.sub(r'\n+', '\n', text)
output=''
words=[word for word in word_tokenize(text) if len(word)<=15 and re.match('[A-Za-z0-9,._]+',word)]
for word in words:
if output=='':
output=word
else:
output = output + ' ' + word
return output
def parse_email(self,uid):
dict={}
result, raw_email = self.raw_email(uid)
raw_email = raw_email.decode('utf-8','ignore')
parsed = email.message_from_string(raw_email)
dict['To']=email.utils.parseaddr(parsed['To'])[-1]
dict['From']=email.utils.parseaddr(parsed['From'])[-1]
dict['Subject']=parsed['Subject']
body=''
for part in parsed.walk():
if part.get_content_type()=='text/html':
html=str(part.get_payload())
soup = BeautifulSoup(html,'html5lib')
try:
text=soup.find('body').text.strip()
except Exception:
continue
text=self.__clean__(text)
body=body + text
body=self.__clean__(body)
dict['Body']=body
return dict
#Directory
def directory(label):
cdir =os.getcwd()
#Setting target folder
if label.find('/')>0:
folder=label[label.find('/')+1:].lower()
else:
folder=label.lower()
#Create folder, if it does not exist
if not os.path.exists(cdir + '\Downloads\\' + folder):
os.mkdir(cdir + '\Downloads\\' + folder)
return folder
def download_emails(label):
#Getting all the emails
mail.select_folder(label,True)
result, data = mail.get_all_emails()
#Exit, if no emails returned
if not result=='OK':
exit(1)
#Saving the Data
cdir =os.getcwd()
print('Downloading ' + emails.directory(label) + ' emails...')
for uid in data:
dict = mail.parse_email(uid)
subject = dict['Subject']
subject=re.sub('[^a-zA-Z ]','',subject)
if len(subject) > 50:
subject = subject[:50]
file = cdir + '\Downloads\\' + emails.directory(label) + '\\' + subject + '.txt'
with open(file, 'w', encoding='utf-8') as f:
body = subject + '\n' + str(dict['Body'])
f.write(body)
#Status Display
print('Downloaded ' + emails.directory(label) + ' emails at '+ cdir + '\Downloads\\' + emails.directory(label) + '.')
print('------------------------------')
#Creates List of Emails
def create_email_list(folder_path):
email_list = []
folder = os.listdir(folder_path)
for txt in folder:
file_name = fr'{folder_path}/{txt}'
with codecs.open(file_name, 'r', encoding='utf-8',errors='ignore') as f:
email = f.read()
email_list.append(email)
return email_list
#main
if __name__=='__main__':
email_id = getpass('Enter Gmail ID :')
email_password = getpass('Enter Password :')
mail=emails(email_id,email_password)
#This might take a while
emails.download_emails('[Gmail]/Spam')
emails.download_emails('INBOX')
cdir = os.getcwd()
spam_list = emails.create_email_list(cdir + '\Downloads\\' + emails.directory('[Gmail]/Spam'))
spam_df = pd.DataFrame(spam_list)
spam_df.to_excel(cdir + '\Downloads\spam.xlsx')
print('Converted spam emails to Excel file.')
print('------------------------------')
non_spam_list = emails.create_email_list(cdir + '\Downloads\\' + emails.directory('INBOX'))
non_spam_df = pd.DataFrame(non_spam_list)
non_spam_df.to_excel(cdir + '\Downloads\inbox.xlsx')
print('Converted inbox emails to Excel file.')
print('------------------------------')
df1 = pd.read_excel(cdir + '\Downloads\inbox.xlsx')
df2 = pd.read_excel(cdir + '\Downloads\spam.xlsx')
df1.columns
df2.columns
df1.insert(1,"Label", "non_spam")
df1.insert(3,"Label_Number", 0)
df1.rename(columns = {'Unnamed: 0': 'ID',0: 'Text'}, inplace = True)
df1.head()
df2.insert(1,"Label", "spam")
df2.insert(3,"Label_Number", 1)
df2.rename(columns = {'Unnamed: 0': 'ID',0: 'Text'}, inplace = True)
df2.head()
df_all = pd.concat([df1, df2])
df_all.reset_index(inplace=True, drop=True)
df_all.pop('ID')
df_final = df_all.sample(frac = 1)
df_final.reset_index(inplace=True, drop=True)
df_final
excel_file_name = input("Enter name of Excel File: ")
df_final.to_excel(cdir + '\Downloads\\'+excel_file_name + '.xlsx')
print('Downloaded all emails dataset in '+ cdir + '\Downloads\\'+excel_file_name + '.xlsx')
print('------------------------------')