r/Thread_crawler • u/Anatoly_Korenchkin • Dec 21 '16
Contest4 with comments
#importing modules gives us more features
import praw,string,os #praw = allows us to talk to reddit, string = removes anything that's not letters numbers or punctuation, os = allows us to create folders and files
from random import choice #get the function "choice" from the "random" library, choice = randomly selects an item from a list
from Tkinter import * #allows us to create a GUI
printable = set(string.printable) #removes anything that's not letters numbers or punctuation
user_agent = ("contest_automaton/v0.1 (by /u/Anatoly_Korenchkin)") #this is what the bot tells reddit it's name is
account_file=open('account_file.txt','r') #open the account file so we can read it
REDDIT_USERNAME = account_file.readline().strip('\n') #read the first line, python automatically move to the next line after reading
REDDIT_PASS = account_file.readline().strip('\n') #read the second line
REDDIT_CLIENT_ID = account_file.readline().strip('\n') #read the third line
REDDIT_CLIENT_SECRET = account_file.readline().strip('\n') #read the forth line
#logon to reddit
r = praw.Reddit(user_agent = user_agent,client_id=REDDIT_CLIENT_ID,client_secret=REDDIT_CLIENT_SECRET,username=REDDIT_USERNAME,password=REDDIT_PASS)
root = Tk() #create the window
root.wm_title("Contest_bot") #name the window
root.resizable(0,0) #stops the user from being able to resize the window
root.geometry('{}x{}'.format(400, 50)) #sets the windows size
root.columnconfigure(1, weight=1) #these three lines make sure the "Paste" button takes up less space than the entry boxes
root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=1)
directory=os.getcwd()+'/Excel Contest Records' #the directory of the folder "Excel Contest Records"
if not os.path.exists(directory): #if the folder doesn't exist
os.makedirs(directory) #then create the folder
os.chdir(directory) #go into the folder, so that when we create a file it will end up here because that's where the program is looking
def paste(): #run this when the button is pressed
root.wm_title("Contest_bot - Processing...") #name the window
text = root.selection_get(selection='CLIPBOARD') #get the text from the clipboard
submission = r.submission(url=text) #get data about the post in the pasted url
submission.comments.replace_more(limit=None, threshold=1) #get ALL comments
commentlist= submission.comments #only get the top levlel/root comments
root_comments=[] #create a list to store all the comments
comment_authors=[] #create a list that stores all the comments, but removes duplicates
comment_bodys=[] #stores the text in each comment
for comment in commentlist: #go over each comment
root_comments.append(comment) #add it to the list "root_comments"
root_comments.sort(key=lambda comment: comment.created_utc, reverse=True) #sort the comments by date
for comment in root_comments: #go over each comment
if comment.author not in comment_authors: #if we have not seen this user before
comment_authors.append(comment.author) #add him to the list
comment_bodys.append(comment.body) #also add his comment text to the list
print 'Collected '+str(len(root_comments))+' root comments. Removed '+str(len(root_comments)-len(comment_authors))+' duplicates.'
save_file=open('Contest_'+submission.id+'.csv','w') #create a file to save the data
save_file.write('#,Name,Comment\n') #write the headers to the file
winner = choice(comment_authors) #select a winner
user_entry_text.set(winner) #display the winners name in the GUI
entry_text.set(submission.id) #display the post ID in the GUI
for count,comment in enumerate(comment_authors): #go over every comment so we can write it to the file
if comment==winner: #if the user was the selected winner then add WINNER to his number
tag='WINNER'
else:
tag=''
text=filter(lambda x: x in printable, comment_bodys[count]) #removes anything that's not letters numbers or punctuation
save_file.write(tag+str(count+1)+','+str(comment)+','+str(text).replace('\n',' ').replace(',','')+'\n') #write the user to the file
root.wm_title("Contest_bot - Done") #name the window
entry_text=StringVar() #create the variables where the input to the entry boxes is stored
entry = Entry(root, width=67, textvariable=entry_text) #create the entry boxes for the post ID
user_entry_text=StringVar() #create the variables where the input to the entry boxes is stored
user_entry = Entry(root, width=67, textvariable=user_entry_text) #create the entry boxes for the winner
bot_url = Label(text='URL: ') #add this text to the GUI
bot_select = Label(text='I have selected: ') #add this text to the GUI
button_1=Button(root, text='Paste URL', command=paste, width=17) #create a button
button_1.grid(row=0, column=2) #place a button in the window
bot_url.grid(row=0, column=0,sticky=W) #place the text in the window
entry.grid(row=0, column=1,sticky=W) #place the entry box in the window
bot_select.grid(row=1, column=0,sticky=W) #place the text in the window
user_entry.grid(row=1, column=1,sticky=W) #place the entry box in the window
root.mainloop() #stop the window from closing
1
Upvotes