· 9 years ago · Oct 02, 2016, 08:28 PM
1import praw
2import imaplib
3import email
4import time
5import getpass
6import RUAlerts
7from datetime import datetime
8app_id = '*****'
9app_secret = '*****'
10app_uri = '*****'
11app_scopes = 'account creddits edit flair history identity mysubreddits privatemessages read report save submit subscribe vote'
12app_ua='*****'
13app_account_code = '*****'
14app_refresh = '*****'
15def login():
16 r = praw.Reddit(app_ua)
17 r.set_oauth_app_info(app_id, app_secret, app_uri)
18 r.refresh_access_information(app_refresh)
19 return r
20
21mail = imaplib.IMAP4_SSL('imap.gmail.com')
22
23while True:
24 try:
25 emailpass = getpass.getpass('Please enter the password for *****@gmail.com: ')
26 mail.login('*****@gmail.com', emailpass)
27 break
28 except imaplib.IMAP4.error:
29 print('Incorrect password')
30
31mail.select("inbox")
32
33while True:
34 try:
35 r = RUAlerts.login()
36
37 while 1:
38 result, response = mail.uid('search', None, "(UNSEEN)")
39 unread_msg_nums = response[0].split()
40
41 result, data = mail.uid('search', None, "ALL")
42 latest_email_uid = data[0].split()[-1]
43 result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
44 raw_email = data[0][1]
45
46 email_message = email.message_from_bytes(raw_email)
47
48 if len(unread_msg_nums)>0:
49 print('\t' + str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + ' - Something\'s wrong!')
50 for part in email_message.walk():
51 if part.get_content_type()=='text/plain':
52 Alert=part.get_payload()
53 while True:
54 try:
55 r.submit(subreddit='*****',title=Alert,text=str(Alert)+"\n \n ******** \n \n*^^I ^^am ^^a ^^bot. ^^Do ^^not ^^rely ^^on ^^me ^^for ^^security ^^alerts!* \n \n [^^\[Sign ^^up ^^for ^^text ^^alerts\]](*****) [^^[nixle]](*****) [^^[Contact: ^^*****]](mailto://*****)")
56 print('\t' + str(Alert))
57 break
58 except praw.errors.ExceptionList as e:
59 print('\tReddit error!' + str(e) + '\tRetrying in 5 minutes - ' + str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
60 ##mail.uid('STORE', latest_email_uid, '-FLAGS', '\SEEN')
61 time.sleep(300)
62 else:
63 print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + ' - All clear on the RU front')
64 time.sleep(30)
65 break
66 except Exception as e:
67 print('\t' + str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + ' - No connection! Retrying in 5 minutes')
68 print('\t' + str(e))
69 time.sleep(300)