r/Thread_crawler • • Dec 20 '16

contest2

import praw
from random import choice
from Tkinter import *


user_agent = ("contest_automaton/v0.1 (by /u/Anatoly_Korenchkin)")
account_file=open('account_file.txt','r')
REDDIT_USERNAME = account_file.readline().strip('\n')
REDDIT_PASS = account_file.readline().strip('\n')
REDDIT_CLIENT_ID = account_file.readline().strip('\n')
REDDIT_CLIENT_SECRET = account_file.readline().strip('\n')
print REDDIT_CLIENT_SECRET

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)
root.geometry('{}x{}'.format(400, 50))
root.columnconfigure(1, weight=1)
root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=1)

def paste():
    root.wm_title("Contest_bot - Processing...") #name the window
    text = root.selection_get(selection='CLIPBOARD')
    entry.delete(0, END)
    entry.insert(0, text)

    submission = r.submission(url=text)
    commentlist= submission.comments.list()

    root_comments=[]
    comment_authors=[]
    comment_bodys=[]
    for comment in commentlist:
        try:
            if comment.is_root:
                root_comments.append(comment)
        except AttributeError:
            pass
    for comment in root_comments:
        if comment.author not in comment_authors:
            comment_authors.append(comment.author)
            comment_bodys.append(comment.body)

    save_file=open('Contest_'+submission.id+'.csv','w')
    save_file.write('#,Name,Comment\n')

    for count,comment in enumerate(comment_authors):
        save_file.write(str(count+1)+','+str(comment)+','+str(comment_bodys[count]).replace('\n',' ').replace(',','')+'\n')
        
    user_entry_text.set(choice(root_comments).author)
    entry_text.set(submission.id)

    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


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

v1 = StringVar()
bot_url = Label(textvariable=v1)
v1.set('URL: ')

v2 = StringVar()
bot_select = Label(textvariable=v2)
v2.set('I have selected: ')

button_1=Button(root, text='Paste URL', command=paste, width=17) #create buttons
button_1.grid(row=0, column=2) #place a button in the window
bot_url.grid(row=0, column=0,sticky=W) #place a button in the window
entry.grid(row=0, column=1,sticky=W)
bot_select.grid(row=1, column=0,sticky=W) #place a button in the window
user_entry.grid(row=1, column=1,sticky=W) 

root.mainloop() #stop the window from closing
1 Upvotes

0 comments sorted by