-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit_bot.py
130 lines (112 loc) · 3.99 KB
/
reddit_bot.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import schedule
import praw
import config
import time
import os #Used for making the .txt files of words
import re
from vocabulary.vocabulary import Vocabulary as vb
import datetime
# Made by reddit user /u/Stronger1088
# Feel free to use this without crediting me but if anyone asks please let them know of the original developer! (Yours truly)
# This is my second bot so I hope its good enough! I tried to rule out all of the bugs possible but there might still be some. Feel free to message me! Ty!
now = datetime.datetime.now()
def authentication():
print ("Authenticating...")
reddit = praw.Reddit(username = config.username,
password = config.password,
client_id = config.client_id,
client_secret = config.client_secret,
user_agent = "Stronger1088's WOTD bot for a /r/requestABot user.")
print ("Authenticated as {}".format(reddit.user.me()))
time.sleep(2)
return reddit
def pick_word():
with open("unused_words.txt", "r") as f:
unused_words = f.read()
unused_words = unused_words.split("\n")
unused_words = list(filter(None, unused_words))
word = unused_words[0]
print ("Picked word: "+ word.title())
return word
def use_word(word, unused_words, used_words):
if word in unused_words:
unused_words.remove(word)
with open('unused_words.txt', 'r+') as f:
t = f.read()
f.seek(0)
for line in t.split('\n'):
if line != word:
f.write(line + '\n')
f.truncate()
if word not in used_words:
used_words.append(word)
with open ("used_words.txt", "a") as f:
f.write(word + "\n")
return unused_words, used_words
def run_bot(reddit):
print ("Running...")
word = (pick_word()).lower()
deff = vb.meaning(word, format = "list")
if deff is False or len(word) < config.minimum or len(word) > config.maximum:
use_word(word, unused_words, used_words)
deff = []
print ("Could not find deffinition/didnt meet length requirements of '" + word.title() + "'. Finding another...")
run_bot(reddit)
else:
print ("Found deffinition for '" + word.title() + "'. Posting...")
string = "# Word of the day: " + word.title()
for i in range(len(deff)):
if i < config.ammount:
string += "\n\n"
string += str(i + 1)
string += ": "
reFormattedString = re.sub(r"\<.\>", "", deff[i])
reFormattedString1 = re.sub(r"\[.\]", "", reFormattedString)
reFormattedString2 = re.sub(r"\</.\>", "", reFormattedString1)
reFormattedString3 = re.sub(r"\[/.\]", "", reFormattedString2)
string += reFormattedString3
i += 1
string += config.message
submitted = reddit.subreddit(config.subreddit).submit("Word Of The Day - " + now.strftime("%B %d, %Y") + " - " + word.title(), string)
if config.distinguish is True and config.sticky is False and config.moderator is True:
submitted.mod.distinguish()
elif config.sticky is True and config.moderator is True:
submitted.mod.sticky()
print ("Posted Successfully!")
use_word(word, unused_words, used_words)
print ("Next: " + str(unused_words[0]).title())
print ("Waiting till " + config.time + "...")
def unused_words():
if not os.path.isfile("unused_words.txt"):
unused_words = []
with open ("unused_words.txt", "w") as f:
f.write("")
else:
with open("unused_words.txt", "r") as f:
unused_words = f.read()
unused_words = unused_words.split("\n")
unused_words = list(filter(None, unused_words))
return unused_words
def used_words():
if not os.path.isfile("used_words.txt"):
used_words = []
with open ("used_words.txt", "w") as f:
f.write("")
else:
with open ("used_words.txt", "r") as f:
used_words = f.read()
used_words = used_words.split("\n")
used_words = list(filter(None, used_words))
return used_words
reddit = authentication()
unused_words = (unused_words())
used_words = (used_words())
print ("Unused Words: " + str(unused_words))
print ("Used Words: " + str(used_words))
print ("Next: " + str(unused_words[0]).title())
#schedule.every().day.at(config.time).do(run_bot, reddit)
#print ("Waiting till " + config.time + "...")
run_bot(reddit)
while True:
schedule.run_pending()
time.sleep(1)