Repository navigation
Expand file tree
/
Copy pathBot.py
More file actions
78 lines (69 loc) · 2.82 KB
/
Copy pathBot.py
File metadata and controls
78 lines (69 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import praw
import time
from reddit_classes import User
from config import config
from random import randrange
REDDIT_INTERFACE = None
USER_AGENT = config['user_agent']
REDDIT_USERNAME = config['reddit_username']
REDDIT_PASSWORD = config['reddit_password']
BAN_MESSAGE = "User_History_Bot has been banned from this subreddit"
#
def parse_comment_text(text):
string_list = text.split()
username, special = None, None
if len(string_list) > 1:
if (((string_list[0]).lower()).strip() == "/u/user_history_bot"):
username = (string_list[1]).strip()
if len(string_list) > 2:
special = string_list[2]
return username, special
def process_message(username, special):
if not (username is None):
try:
user = User(username, REDDIT_INTERFACE)
user.process_comments()
comment_statistics = user.get_comment_statistics()
print("comment statistics successfuly retrieved")
return comment_statistics
except:
print("remember to log error that comment failed")
return "I'm a bot that just gets simple statistics about your comment history. Don't hurt me."
print("Username is none")
return None
def reply(message, reply_object):
if not (reply_object is None):
try:
message.reply(reply_object)
except:
REDDIT_INTERFACE.send_message(message.author,
subject= BAN_MESSAGE, message = reply_object)
# parse a message string and do appropriate
def launch_response(message):
username, special = parse_comment_text(message.body)
reply_object = process_message(username, special)
reply(message, reply_object)
print("MARK READ")
message.mark_as_read()
def main():
running = True
while (running):
try:
unread_messages = REDDIT_INTERFACE.get_unread()
for message in unread_messages:
launch_response(message)
except KeyboardInterrupt:
running = False
time.sleep(10)
if __name__ == '__main__':
try:
REDDIT_INTERFACE = praw.Reddit(user_agent = USER_AGENT)
REDDIT_INTERFACE.login(REDDIT_USERNAME, REDDIT_PASSWORD)
except praw.errors.InvalidUser:
print("remember to log error that user is invalid")
except praw.errors.InvalidUserPass:
print("remember to log error that password is invalid")
except:
print()
else:
main()