Advertisement
Nickpips

Untitled

Mar 26th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import imaplib
  5. import getpass
  6. import email
  7. import datetime
  8. import re
  9. from bs4 import BeautifulSoup
  10.  
  11. password = open('password','r').read().strip()
  12.  
  13. '''
  14.  
  15. urlregex = re.compile(
  16. r'(?:http|ftp)s?://' # http:// or https://
  17. r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
  18. r'localhost|' #localhost...
  19. r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
  20. r'(?::\d+)?' # optional port
  21. r'(?:/?|[/?]\S+)', re.IGNORECASE)'''
  22. urlregex = re.compile("(?P<url>https?://[^\s]+)")
  23.  
  24. def isValidUrl(url):
  25. if urlregex.match(url) is not None:
  26. #print re.compile('.*\.(.*)$').match(url).group(1)
  27. return True;
  28. return False
  29.  
  30. def getlinks(page):
  31. match = urlregex.match(page)
  32. if match is None:
  33. return []
  34. else:
  35. return match.groups()
  36. '''soup = BeautifulSoup(page,'html.parser')
  37. links = soup.findAll('a',href=True)
  38. output = []
  39. print links
  40. for l in links:
  41. if isValidUrl(l['href']):
  42. output.append(l['href'])
  43. return output'''
  44.  
  45. def process_mailbox(M):
  46. rv, data = M.search(None, "ALL")
  47. if rv != 'OK':
  48. print "No messages found!"
  49. return
  50. count = 0
  51. for num in data[0].split():
  52. #rv, data = M.fetch(num, '(RFC822)')
  53. rv, data = M.fetch(num, '(BODY[1])')
  54. if rv != 'OK':
  55. print "ERROR getting message", num
  56. return
  57.  
  58. msg = email.message_from_string(data[0][1])
  59. msg = str(str(msg).decode('quopri').replace('\n',' ').replace('\r',' '))
  60. print 'MESSAGE: '
  61. print msg
  62. #htmlregex = re.compile(r'<!DOCTYPE.*<\/html>')
  63. #msg = msg[msg.find('<!DOCTYPE'):(msg.find('</html>')+7)]
  64. print 'LINKS: '
  65. #msg = msg.decode('quopri')
  66. print getlinks(msg)
  67. count += 1
  68. if count > 5:
  69. break
  70. '''print 'Message %s: %s' % (num, msg['Subject'])
  71. print 'Raw Date:', msg['Date']
  72. date_tuple = email.utils.parsedate_tz(msg['Date'])
  73. if date_tuple:
  74. local_date = datetime.datetime.fromtimestamp(
  75. email.utils.mktime_tz(date_tuple))
  76. print "Local Date:", \
  77. local_date.strftime("%a, %d %b %Y %H:%M:%S")'''
  78.  
  79. MAIL_SERVERS = {'gmail': 'imap.gmail.com',
  80. 'yahoo': 'imap.mail.yahoo.com',
  81. 'aol': 'imap.aol.com'}
  82.  
  83. M = imaplib.IMAP4_SSL(MAIL_SERVERS['yahoo'])
  84. try:
  85. M.login('npipitone1234@yahoo.com', password)
  86. except imaplib.IMAP4.error:
  87. print "LOGIN FAILED!!! "
  88. # ... exit or deal with failure...
  89.  
  90. rv, mailboxes = M.list()
  91. if rv == 'OK':
  92. print "Mailboxes:"
  93. print mailboxes
  94.  
  95. rv, data = M.select("INBOX")
  96. if rv == 'OK':
  97. print "Processing mailbox...\n"
  98. process_mailbox(M) # ... do something with emails, see below ...
  99. M.close()
  100. M.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement