Skip to content

Instantly share code, notes, and snippets.

@scratchmex
Last active June 24, 2019 08:49
Show Gist options
  • Save scratchmex/243c1b09e4813a9d7fdfeb60b083f5cd to your computer and use it in GitHub Desktop.
Save scratchmex/243c1b09e4813a9d7fdfeb60b083f5cd to your computer and use it in GitHub Desktop.
Download, organize and search logs
import email
import imaplib
import os
class FetchEmail():
connection = None
error = None
def __init__(self, mail_server, username, password):
self.connection = imaplib.IMAP4_SSL(mail_server)
self.connection.login(username, password)
self.connection.select(readonly=False) # so we can mark mails as read
def close_connection(self):
self.connection.close()
def save_attachment(self, msg, download_folder='tmp'):
try:
data = self.connection.fetch(msg, '(RFC822)')
except Exception:
print "\033[1;36m[Error fetching]\033[1;m"
self.close_connection()
exit()
att_path = None
m = email.message_from_string(data[1][0][1])
for part in m.walk():
filename = part.get_filename()
if (
part.get_content_maintype() is 'multipart'
or not part.get('Content-Disposition')
or not filename
or 'Keys' not in filename
):
continue
filename = '[{}]'.format(msg.zfill(6)) + filename
att_path = os.path.join(download_folder, filename)
if os.path.isfile(att_path):
print att_path, '\033[1;32m[EXIST]#\033[1;m'
continue
open(att_path, 'wb').write(part.get_payload(decode=True))
print att_path, '\033[1;32m[OK]#\033[1;m'
if not att_path:
print "\033[1;36m[No attachment found in: {}]\033[1;m".format(msg)
def save_all_attachment(self, WEmails, download_folder='tmp'):
if WEmails.startswith('a'):
WEmails = 'ALL'
elif WEmails.startswith('u'):
WEmails = 'UnSeen'
else:
print 'Set ALL or UNSEEN to download'
exit()
items = self.connection.search(None, WEmails)
mails = items[1][0]
if not mails:
print "\033[1;36m[No new mail]\033[1;m"
exit()
for emailid in mails.split(' '):
print '\033[1;33m[Fetching...] => \033[1;m', emailid
self.save_attachment(emailid, download_folder)
if __name__ == "__main__":
datos = ['imap.gmail.com', '', '']
FetchEmail(datos[0], datos[1], datos[2]).save_all_attachment('u')
import glob
import os
import json
from bs4 import BeautifulSoup
try:
with open('database', 'r') as database:
my_dict = json.load(database)
except IOError:
my_dict = {}
for File in sorted(glob.glob('tmp/*')):
filename = os.path.basename(File)
if filename in my_dict:
continue
htmlfile = open(File).read()
tmplist = []
soup = BeautifulSoup(htmlfile, 'html.parser')
c = soup.find('body').findAll(text=True, recursive=False)
for i in c:
tmplist.append(i)
my_dict[filename] = tmplist
print filename, '=> Done#'
with open('database', 'w') as outfile:
print '[Writing...]'
json.dump(my_dict, outfile)
import json
string = raw_input('String to search: ')
with open('database') as f:
my_dict = json.load(f)
for i in sorted(my_dict):
for ii in my_dict[i]:
if string in ii:
print '\033[1;31m[{}] => \033[1;m'.format(i) + \
'\033[1;32m{}\033[1;m'.format(ii.encode('utf-8')), ' #'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment