Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. #importing modules gives us more features
  2. 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
  3. from random import choice #get the function "choice" from the "random" library, choice = randomly selects an item from a list
  4. from Tkinter import * #allows us to create a GUI
  5.  
  6. printable = set(string.printable) #removes anything that's not letters numbers or punctuation
  7.  
  8. user_agent = ("contest_automaton/v0.1 (by /u/Anatoly_Korenchkin)") #this is what the bot tells reddit it's name is
  9. account_file=open('account_file.txt','r') #open the account file so we can read it
  10. REDDIT_USERNAME = account_file.readline().strip('\n') #read the first line, python automatically move to the next line after reading
  11. REDDIT_PASS = account_file.readline().strip('\n') #read the second line
  12. REDDIT_CLIENT_ID = account_file.readline().strip('\n') #read the third line
  13. REDDIT_CLIENT_SECRET = account_file.readline().strip('\n') #read the forth line
  14.  
  15. #logon to reddit
  16. r = praw.Reddit(user_agent = user_agent,client_id=REDDIT_CLIENT_ID,client_secret=REDDIT_CLIENT_SECRET,username=REDDIT_USERNAME,password=REDDIT_PASS)
  17.  
  18. root = Tk() #create the window
  19. root.wm_title("Contest_bot") #name the window
  20. root.resizable(0,0) #stops the user from being able to resize the window
  21. root.geometry('{}x{}'.format(400, 50)) #sets the windows size
  22. root.columnconfigure(1, weight=1) #these three lines make sure the "Paste" button takes up less space than the entry boxes
  23. root.rowconfigure(0, weight=1)
  24. root.rowconfigure(1, weight=1)
  25.  
  26. directory=os.getcwd()+'/Excel Contest Records' #the directory of the folder "Excel Contest Records"
  27. if not os.path.exists(directory): #if the folder doesn't exist
  28. os.makedirs(directory) #then create the folder
  29. 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
  30.  
  31. def paste(): #run this when the button is pressed
  32. root.wm_title("Contest_bot - Processing...") #name the window
  33. text = root.selection_get(selection='CLIPBOARD') #get the text from the clipboard
  34.  
  35. submission = r.submission(url=text) #get data about the post in the pasted url
  36. submission.comments.replace_more(limit=None, threshold=1) #get ALL comments
  37. commentlist= submission.comments #only get the top levlel/root comments
  38.  
  39. root_comments=[] #create a list to store all the comments
  40. comment_authors=[] #create a list that stores all the comments, but removes duplicates
  41. comment_bodys=[] #stores the text in each comment
  42. for comment in commentlist: #go over each comment
  43. root_comments.append(comment) #add it to the list "root_comments"
  44. root_comments.sort(key=lambda comment: comment.created_utc, reverse=True) #sort the comments by date
  45. for comment in root_comments: #go over each comment
  46. if comment.author not in comment_authors: #if we have not seen this user before
  47. comment_authors.append(comment.author) #add him to the list
  48. comment_bodys.append(comment.body) #also add his comment text to the list
  49. print 'Collected '+str(len(root_comments))+' root comments. Removed '+str(len(root_comments)-len(comment_authors))+' duplicates.'
  50.  
  51. save_file=open('Contest_'+submission.id+'.csv','w') #create a file to save the data
  52. save_file.write('#,Name,Comment\n') #write the headers to the file
  53.  
  54. winner = choice(comment_authors) #select a winner
  55. user_entry_text.set(winner) #display the winners name in the GUI
  56. entry_text.set(submission.id) #display the post ID in the GUI
  57.  
  58. for count,comment in enumerate(comment_authors): #go over every comment so we can write it to the file
  59. if comment==winner: #if the user was the selected winner then add WINNER to his number
  60. tag='WINNER'
  61. else:
  62. tag=''
  63. text=filter(lambda x: x in printable, comment_bodys[count]) #removes anything that's not letters numbers or punctuation
  64. save_file.write(tag+str(count+1)+','+str(comment)+','+str(text).replace('\n',' ').replace(',','')+'\n') #write the user to the file
  65.  
  66.  
  67.  
  68. root.wm_title("Contest_bot - Done") #name the window
  69.  
  70.  
  71.  
  72. entry_text=StringVar() #create the variables where the input to the entry boxes is stored
  73. entry = Entry(root, width=67, textvariable=entry_text) #create the entry boxes for the post ID
  74.  
  75.  
  76. user_entry_text=StringVar() #create the variables where the input to the entry boxes is stored
  77. user_entry = Entry(root, width=67, textvariable=user_entry_text) #create the entry boxes for the winner
  78.  
  79. bot_url = Label(text='URL: ') #add this text to the GUI
  80.  
  81. bot_select = Label(text='I have selected: ') #add this text to the GUI
  82.  
  83. button_1=Button(root, text='Paste URL', command=paste, width=17) #create a button
  84. button_1.grid(row=0, column=2) #place a button in the window
  85. bot_url.grid(row=0, column=0,sticky=W) #place the text in the window
  86. entry.grid(row=0, column=1,sticky=W) #place the entry box in the window
  87. bot_select.grid(row=1, column=0,sticky=W) #place the text in the window
  88. user_entry.grid(row=1, column=1,sticky=W) #place the entry box in the window
  89.  
  90. root.mainloop() #stop the window from closing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement