Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys
- import imaplib
- import getpass
- import email
- import datetime
- import re
- from bs4 import BeautifulSoup
- password = open('password','r').read().strip()
- '''
- urlregex = re.compile(
- r'(?:http|ftp)s?://' # http:// or https://
- r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
- r'localhost|' #localhost...
- r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
- r'(?::\d+)?' # optional port
- r'(?:/?|[/?]\S+)', re.IGNORECASE)'''
- urlregex = re.compile("(?P<url>https?://[^\s]+)")
- def isValidUrl(url):
- if urlregex.match(url) is not None:
- #print re.compile('.*\.(.*)$').match(url).group(1)
- return True;
- return False
- def getlinks(page):
- match = urlregex.match(page)
- if match is None:
- return []
- else:
- return match.groups()
- '''soup = BeautifulSoup(page,'html.parser')
- links = soup.findAll('a',href=True)
- output = []
- print links
- for l in links:
- if isValidUrl(l['href']):
- output.append(l['href'])
- return output'''
- def process_mailbox(M):
- rv, data = M.search(None, "ALL")
- if rv != 'OK':
- print "No messages found!"
- return
- count = 0
- for num in data[0].split():
- #rv, data = M.fetch(num, '(RFC822)')
- rv, data = M.fetch(num, '(BODY[1])')
- if rv != 'OK':
- print "ERROR getting message", num
- return
- msg = email.message_from_string(data[0][1])
- msg = str(str(msg).decode('quopri').replace('\n',' ').replace('\r',' '))
- print 'MESSAGE: '
- print msg
- #htmlregex = re.compile(r'<!DOCTYPE.*<\/html>')
- #msg = msg[msg.find('<!DOCTYPE'):(msg.find('</html>')+7)]
- print 'LINKS: '
- #msg = msg.decode('quopri')
- print getlinks(msg)
- count += 1
- if count > 5:
- break
- '''print 'Message %s: %s' % (num, msg['Subject'])
- print 'Raw Date:', msg['Date']
- date_tuple = email.utils.parsedate_tz(msg['Date'])
- if date_tuple:
- local_date = datetime.datetime.fromtimestamp(
- email.utils.mktime_tz(date_tuple))
- print "Local Date:", \
- local_date.strftime("%a, %d %b %Y %H:%M:%S")'''
- MAIL_SERVERS = {'gmail': 'imap.gmail.com',
- 'yahoo': 'imap.mail.yahoo.com',
- 'aol': 'imap.aol.com'}
- M = imaplib.IMAP4_SSL(MAIL_SERVERS['yahoo'])
- try:
- M.login('npipitone1234@yahoo.com', password)
- except imaplib.IMAP4.error:
- print "LOGIN FAILED!!! "
- # ... exit or deal with failure...
- rv, mailboxes = M.list()
- if rv == 'OK':
- print "Mailboxes:"
- print mailboxes
- rv, data = M.select("INBOX")
- if rv == 'OK':
- print "Processing mailbox...\n"
- process_mailbox(M) # ... do something with emails, see below ...
- M.close()
- M.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement