-
Notifications
You must be signed in to change notification settings - Fork 5
/
Roibal_Markov_Reddit_Reply_Bot.py
140 lines (124 loc) · 5.09 KB
/
Roibal_Markov_Reddit_Reply_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
131
132
133
134
135
136
137
138
139
140
"""
The Purpose of this Reddit Bot is to collect replies to a particular thread, then save to a .txt file
This Python Script has been modified to work in conjunction with 'Markov Chain'-based bot to save a particular users
entire comment history to a text file, then will be added to the list of users callable by Reddit Bot
called by !name such as !jordanp or !marx or !churchill
Read More: https://medium.com/@BlockchainEng/programming-a-markov-chain-based-reddit-bot-21cebb4d1844
Created 5/21/2018 by Joaquin Roibal
Copyright (c) 2018 Joaquin Roibal
All Rights Reserved - MIT License
THIS ENTIRE COMMENT BLOCK MUST BE MAINTAINED THROUGH ANY EDITS!
"""
import time
import random
import praw
import markovify
from samplekeys import rkey
c_id = rkey['client_id']
c_secret = rkey['client_secret']
usr_name = rkey['username']
passw = rkey['password']
#Create Reddit Bot with login and private key - username - password
bot = praw.Reddit(user_agent='Collecting Replies into Spreadsheet bot1 v0.1', client_id=c_id, client_secret=c_secret,
username=usr_name, password=passw)
def run():
#Collect Recent Thread, Collect Comments, Format Comment, Save to .txt file
print("-----------------\n\nHello and Welcome to A Reddit Bot which collects comment replies into spreadsheet")
print("This Bot will collect thread replies and save as text file with author and comment")
print("These Comments will then be used as a markov-based bot for replying to user requests on Reddit")
print("-----------------\n\n")
time.sleep(2)
subreddit1 = 'all' #Subreddit to collect replies
monitor_subreddit = 'testingground4bots'
num_threads = 10 #Number of Threads to collect replies (number of files will be made)
comment_list = [] #New Comment List will be created for each 'thread'/file
#gather list of people to generate text for - from Ash Ologno Script
people = []
with open('_people.txt') as f:
for person in f.readlines():
#exclude people that are "commented out"
print(person)
if person[0:2] != '//':
people.append(person.strip())
subreddit = bot.subreddit(subreddit1)
comments = subreddit.stream.comments()
print(people)
#for all comments with trigger word, reply with random text
for comment in comments:
text = comment.body
for person in people:
if "!" + person in text:
comm_reply = "**" + person.capitalize() + "**\n\n"
comm_reply += sentences_Markov(create_Markov(person), person)
comm_reply += "\n \n ------------------------------------------------------- \n \n"
comm_reply += "The Above Reply was Generated by a Markov Chain-Based Bot!"
comm_reply += " (type !name to call)"
comm_reply += "\n \n People Available:"
for per in people:
comm_reply += " - "+ str(per)
comment.reply(comm_reply)
print("REPLIED!!!")
print(comm_reply, "In Reply To: ", text)
#for person in people:
# file_name_pers = person + ".txt"
# create_Markov(file_name_pers)
#save_comments_to_file()
#Load Text
def create_Markov(person):
text_to_open = person +".txt"
with open(text_to_open) as f:
text=f.read()
#build model
if person == 'sprog':
text_model = markovify.NewlineText(text)
else:
text_model = markovify.Text(text)
return text_model
def sentences_Markov(text_model, person):
i=0
sentences=[]
if person == 'sprog':
j=random.randint(9, 16)
else:
j = random.randint(5,9)
while i<j:
sent = text_model.make_sentence()
print (sent)
if sent == None:
pass
else:
sentences.append(sent)
i+=1
print(sentences)
message = ""
for sent1 in sentences:
if person == 'sprog':
message += sent1 + "\n\n"
else:
message += sent1 + " "
time.sleep(10)
return message
def save_comments_to_file(sub_redditor_comm = 'poem_for_your_sprog'):
#Collect & Save Subredditor's comment replies into file
for comment in bot.redditor(sub_redditor_comm).comments.new(limit=None):
comment_list=[]
print("Author: ", comment.author)
for line1 in comment.body.split('\n'):
print(line1)
if line1 == '':
pass
else:
comment_list.append(line1)
print(comment_list)
#print("Submission Title: ", submission.title) #Submission Title will become FileName
file_name = str(sub_redditor_comm)+".txt"
print(file_name)
write_file(comment_list, file_name)
time.sleep(1)
def write_file(list1, filename):
#Submit format in list form: ['Author', 'Comment']
with open(filename, 'a') as f:
for msg in list1:
f.write(msg + "\n")
if __name__=="__main__":
run()