|
1 | 1 | import praw |
| 2 | +import prawcore |
2 | 3 | from modules.logger import setup_logger |
3 | 4 | from modules.login import reddit |
4 | 5 | import logging |
|
9 | 10 | def main(): |
10 | 11 | try: |
11 | 12 | for item in reddit.inbox.stream(): |
12 | | - written_to = 0 |
13 | | - text = '' |
14 | | - with open('comments_written_to.txt', 'r') as file: |
15 | | - for line in file.readlines(): |
16 | | - if line == str(item) and written_to == 0: |
17 | | - written_to = 1 |
18 | | - logging.info('Comment {} has been already replied to.'.format(str(item))) |
19 | | - if written_to == 0: |
20 | | - if 'good bot' in str(item.body.lower()): |
21 | | - text = 'Good human' |
22 | | - item.reply(text+footer) |
23 | | - logging.info('Message with the text {} replied to comment {}'.format(text, str(item))) |
24 | | - with open('comments_written_to.txt', 'a') as file: |
25 | | - file.write(str(item)+'\n') |
26 | | - elif 'bad bot' in str(item.body.lower()): |
27 | | - text = 'Bad human' |
28 | | - item.reply(text+footer) |
29 | | - logging.info('Message with the text {} replied to comment {}'.format(text, str(item))) |
30 | | - with open('comments_written_to.txt', 'a') as file: |
31 | | - file.write(str(item)+'\n') |
32 | | - elif 'average bot' in str(item.body.lower()): |
33 | | - text = 'Average human' |
34 | | - item.reply(text+footer) |
35 | | - logging.info('Message with the text {} replied to comment {}'.format(text, str(item))) |
36 | | - with open('comments_written_to.txt', 'a') as file: |
37 | | - file.write(str(item)+'\n') |
38 | | - elif 'bot' in str(item.body.lower()): |
39 | | - strings = str(item.body.lower()).split('bot') |
40 | | - text = '{} human'.format(strings[0]) |
41 | | - item.reply(text+footer) |
42 | | - logging.info('Message with the text {} replied to comment {}'.format(text, str(item))) |
43 | | - with open('comments_written_to.txt', 'a') as file: |
44 | | - file.write(str(item)+'\n') |
| 13 | + try: |
| 14 | + written_to = 0 |
| 15 | + text = '' |
| 16 | + with open('comments_written_to.txt', 'r') as file: |
| 17 | + for line in file.readlines(): |
| 18 | + if line == str(item) and written_to == 0: |
| 19 | + written_to = 1 |
| 20 | + logging.info('Comment {} has been already replied to.'.format(str(item))) |
| 21 | + if written_to == 0: |
| 22 | + if 'good bot' in str(item.body.lower()): |
| 23 | + text = 'Good human' |
| 24 | + item.reply(text+footer) |
| 25 | + logging.info('Message with the text {} replied to comment {}'.format(text, str(item))) |
| 26 | + with open('comments_written_to.txt', 'a') as file: |
| 27 | + file.write(str(item)+'\n') |
| 28 | + elif 'bad bot' in str(item.body.lower()): |
| 29 | + text = 'Bad human' |
| 30 | + item.reply(text+footer) |
| 31 | + logging.info('Message with the text {} replied to comment {}'.format(text, str(item))) |
| 32 | + with open('comments_written_to.txt', 'a') as file: |
| 33 | + file.write(str(item)+'\n') |
| 34 | + elif 'average bot' in str(item.body.lower()): |
| 35 | + text = 'Average human' |
| 36 | + item.reply(text+footer) |
| 37 | + logging.info('Message with the text {} replied to comment {}'.format(text, str(item))) |
| 38 | + with open('comments_written_to.txt', 'a') as file: |
| 39 | + file.write(str(item)+'\n') |
| 40 | + except(prawcore.exceptions.Forbidden): |
| 41 | + logging.info('Blocked on /r/{}'.format(str(item.subreddit))) |
| 42 | + except(KeyboardInterrupt): |
| 43 | + raise KeyboardInterrupt |
| 44 | + except Exception as e: |
| 45 | + logging.error('Error on item {}. {}'.format(str(item),str(e)), exc_info=True) |
45 | 46 | except(KeyboardInterrupt): |
46 | 47 | raise KeyboardInterrupt |
47 | | - except: |
48 | | - logging.error('Error', exc_info = True) |
| 48 | + except Exception as e: |
| 49 | + logging.critical('Error on main loop! {}'.format(str(e)), exc_info=True) |
49 | 50 |
|
50 | 51 | if __name__ == '__main__': |
51 | 52 | while True: |
|
0 commit comments