-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredditBot.py
More file actions
103 lines (95 loc) · 5.42 KB
/
redditBot.py
File metadata and controls
103 lines (95 loc) · 5.42 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import praw
import config
import time
from oracleScrapper import *
# This function creates the connection between this script and reddit via PRAW
def bot_login():
print("Logging " + config.username + " in...")
credentials = praw.Reddit(username=config.username,
password=config.password,
client_id=config.client_id,
client_secret=config.client_secert,
user_agent="Programming Language Documentation Fetcher Bot v0.5")
return credentials
# this function takes in all replied to comments from a file and inserts it into a list
def get_saved_comments():
with open("comments_replied_to.txt", "a+") as f:
f.seek(0)
comments = f.read()
comments = comments.split("\n")
comments = list(filter(None, comments))
return comments
# This function reads through reddit comments in a subreddit
# once a user calls the bot, it looks for keywords such as programming language names and searches for class
# the typical command should look like this "!DocFetcher <Language> <KeyWord>"
def run_bot(access, comments):
for comment in access.subreddit('learnjava+javahelp').comments(limit=25):
time.sleep(3)
print("Searching for comments...")
if ("!DocFetcher" in comment.body or "!df" in comment.body or "!DF" in comment.body) and\
comment.id not in comments and not comment.author == access.user.me():
print("Found Comment...")
if "j11" in comment.body or "java11" in comment.body:
com = comment.body.split()
if len(com) == 3:
if (com[0] == "!DocFetcher" or com[0] == "!df" or com[0] == "!DF") and\
("j" in com[1] and "11" in com[1]):
if "::" in com[-1]:
method_class = com[-1].split("::")
reply = get_method_information(method_class[1], get_j11_class(method_class[0]))
elif "." in com[-1]:
method_class = com[-1].split(".")
reply = get_method_information(method_class[1], get_j11_class(method_class[0]))
else:
# search_jdk is a method inside oracleScrapper
reply = search_jdk(com[1], com[-1])
print("Leaving Reply for " + comment.id + "...")
comment.reply(reply)
comments.append(comment.id)
# append the comment ID to the comments file so as not to spam comments already replied to
with open("comments_replied_to.txt", "a") as f:
f.write(comment.id + "\n")
elif "java7" in comment.body or "j7" in comment.body:
com = comment.body.split()
if len(com) == 3:
if (com[0] == "!DocFetcher" or com[0] == "!df" or com[0] == "!DF") and\
("j" in com[1] and "7" in com[1]):
if "::" in com[-1]:
method_class = com[-1].split("::")
reply = get_method_information(method_class[1], get_j7_class(method_class[0]))
elif "." in com[-1]:
method_class = com[-1].split(".")
reply = get_method_information(method_class[1], get_j7_class(method_class[0]))
else:
# search_jdk is a method inside oracleScrapper
reply = search_jdk(com[1], com[-1])
print("Leaving Reply for " + comment.id + "...")
comment.reply(reply)
comments.append(comment.id)
with open("comments_replied_to.txt", "a") as f:
f.write(comment.id + "\n")
elif "java8" in comment.body or "j8" in comment.body or "java" in comment.body:
com = comment.body.split()
if len(com) == 3:
if (com[0] == "!DocFetcher" or com[0] == "!df" or com[0] == "!DF") and\
(com[1] == "j8" or com[1] == "java" or com[1] == "java8"):
if "::" in com[-1]:
method_class = com[-1].split("::")
reply = get_method_information(method_class[1], get_j8_class(method_class[0]))
elif "." in com[-1]:
method_class = com[-1].split(".")
reply = get_method_information(method_class[1], get_j8_class(method_class[0]))
else:
# search_jdk is a method inside oracleScrapper
reply = search_jdk(com[1], com[-1])
print("Leaving Reply for " + comment.id + "...")
comment.reply(reply)
comments.append(comment.id)
with open("comments_replied_to.txt", "a") as f:
f.write(comment.id + "\n")
print("sleeping...")
time.sleep(60)
while True:
comments_replied = get_saved_comments()
login = bot_login()
run_bot(login, comments_replied)