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 random
- import urllib2
- import socket
- import ssl
- import re
- password = open('password','r').read().strip()
- urlregex = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
- def process_mailbox(M,rate,linkrate):
- endrate = 0
- rv, data = M.search(None, "ALL")
- if rv != 'OK':
- print "No messages found!"
- return
- count = 0
- print ''
- for num in data[0].split()[::-1]:
- if random.random() < endrate:
- break
- if not random.random() < rate:
- continue
- count += 1
- rv, data = M.fetch(num, '(BODY[1])')
- if rv != 'OK':
- print "ERROR getting message", num
- return
- msg = email.message_from_string(data[0][1])
- msgstr = str(msg).decode('quopri').replace('\n',' ').replace('\r',' ')
- links = urlregex.findall(msgstr)
- visited = []
- for a in links:
- if random.random() < linkrate:
- try:
- site = urllib2.urlopen(a[0], timeout=1)
- if not 'html' in site.info().getheaders("Content-Type")[0]:
- raise (ValueError('Not HTML'))
- html = site.read()
- visited.append(a[0])
- except (urllib2.HTTPError, ValueError, urllib2.URLError, socket.timeout, ssl.SSLError, IndexError):
- donothing = 0
- print 'MESSAGE: ' + msgstr[0:100]
- print 'LINKS: ' + str(len(links))
- print 'VISITED: ' + str(len(visited))
- print ''
- rate -= 0.02
- endrate += 0.005
- 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"
- rv, mailboxes = M.list()
- if rv == 'OK':
- print "Mailboxes:"
- print mailboxes
- rv, data = M.select("INBOX")
- if rv == 'OK':
- print ''
- print '> Processing mailbox...'
- process_mailbox(M,0.7,0.5)
- print '> Finished Processing'
- M.close()
- M.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement