Advertisement
DeafProgrammer

GUI for Reddit News API

Jun 28th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.07 KB | None | 0 0
  1. __author__ = 'Joseph Davies'
  2.  
  3. from tkinter import *
  4. import webbrowser, praw
  5.  
  6. news_program = praw.Reddit(user_agent='News Program v2 + GUI by Joseph')
  7. user_name = 'Redditor' # Put your user name here
  8. password = 'RedditorPassword' # Put your password here
  9.  
  10. class RedditNewsGUIHeader(Frame):
  11.     """
  12.     Generates frame that contains a listbox and entry field.
  13.     Allows users to choose the subreddit and offers basic filtering options
  14.    """
  15.  
  16.     def __init__(self, master=None):
  17.  
  18.         Frame.__init__(self, master)
  19.         self.sort_options = ['Top', 'Rising', 'New', 'Hot']
  20.         self.login_status = Label(self, text='{}'.format(self.login()), font=('Times New Roman', 10), bg='light green')
  21.         self.master.title('GUI for Reddit News API designed by /u/TheEnergizerBunny')
  22.         self.pack(anchor=W)
  23.         self.config(bg='light green')
  24.         self.sort = Listbox(self, height=1, selectmode=SINGLE)
  25.         self.sort.grid(row=0, column=0, padx=10, pady=(20,20))
  26.         for option in self.sort_options:
  27.             self.sort.insert(END, option)
  28.         self.sort_scrollbar = Scrollbar(self, orient=HORIZONTAL)
  29.         self.sort_scrollbar.grid(row=0, column=0, sticky='EW', padx=10, pady=(45,10))
  30.         self.sort_scrollbar.configure(command=self.sort.yview)
  31.         self.sort.configure(yscrollcommand=self.sort_scrollbar.set)
  32.         self.subreddit_label = Label(self, text='Subreddit:', bg='light green', padx=8, font=('Times New Roman', 10))
  33.         self.subreddit_label.grid(row=0, column=2)
  34.         self.input_subreddit = Entry(self)
  35.         self.input_subreddit.grid(row=0, padx=(0,5), column=3)
  36.         self.redditor_label = Label(self, text='Redditor:', bg='light green', padx=1, font=('Times New Roman', 10))
  37.         self.subreddit_submit = Button(self, text='Submit', command=lambda: self.getsub())
  38.         self.subreddit_submit.grid(row=0, column=4, padx=(0, 40))
  39.         self.redditor_label.grid(row=0, column=5)
  40.         self.login_status.grid(row=0, pady=10, padx=(0,200), column=6, sticky=E)
  41.  
  42.     def getsub(self):
  43.         try:
  44.             self.list.destroy()
  45.         except:
  46.             pass
  47.         self.c_sort = list(self.sort.curselection())
  48.         self.c_sort = self.sort_options[self.c_sort[0]]
  49.         self.c_subreddit = self.input_subreddit.get()
  50.         self.list = RedditNewsGUI(self.c_sort, self.c_subreddit)
  51.  
  52.     def login(self):
  53.         """
  54.        Attempts to log in using the provided username and password.
  55.        """
  56.         try:
  57.             news_program.login(user_name, password)
  58.         except ResourceWarning as rw:
  59.             pass
  60.         except praw.errors.InvalidUserPass as iup:
  61.             return '{} could not be logged in'.format(user_name)
  62.  
  63.         return '/u/{} is logged in'.format(user_name)
  64.  
  65. class RedditNewsGUI(Frame):
  66.  
  67.     def __init__(self, sort, subreddit, master=None):
  68.         """
  69.         Creates the frame that contains the retrieved post titles and buttons linking to the source url.
  70.         Allows the user to upvote or downvote.
  71.        """
  72.  
  73.         self.u_buttons = []
  74.         self.d_buttons = []
  75.         self.s_buttons = []
  76.         self.n_labels = []
  77.         self.sort = sort
  78.         self.subreddit = subreddit
  79.  
  80.         Frame.__init__(self, master)
  81.         self.pack()
  82.         self.news_list = self.get_news()
  83.         self.create_widgets()
  84.  
  85.     def create_widgets(self):
  86.         """
  87.        Creates empty lists to store widget objects.
  88.        Loops 10 times to create multiple label and button objects.
  89.        Configures the state of the widget object values.
  90.        Structures button and label objects in a grid layout.
  91.        """
  92.  
  93.         for i in range(len(self.news_list)):
  94.             self.u_buttons.append(Button(self, text='Upvote', width=12, bg='light grey'))
  95.             if self.news_list[i + 1][2].likes is True:
  96.                 self.u_buttons[i].config(bg='green')
  97.  
  98.             self.d_buttons.append(Button(self, text='Downvote', width=12, bg='light grey'))
  99.             if self.news_list[i + 1][2].likes is False:
  100.                 self.d_buttons[i].config(bg='red')
  101.  
  102.             self.s_buttons.append(Button(self, text='{}'.format('S'), width=3, bg='#acff99'))
  103.  
  104.             self.u_buttons[i]['command'] = lambda i=i: self.upvote(i)
  105.             self.d_buttons[i]['command'] = lambda i=i: self.downvote(i)
  106.             self.s_buttons[i]['command'] = lambda i=i: self.goto_source(i+1)
  107.  
  108.             self.u_buttons[i].grid(row=i+1, column=0, padx=5, sticky=W)
  109.             self.d_buttons[i].grid(row=i+1, column=1, sticky=W)
  110.             self.s_buttons[i].grid(row=i+1, column=2, padx='3', sticky=W)
  111.             self.n_labels.append(Label(self, text='{}. {}'.format((i+1), self.news_list[i+1][0]), wraplength=550,
  112.                                       justify=LEFT, font={'font':['courier', 50, 'italic']}).grid(row=i+1, column=3,
  113.                                                                                                   padx=5, sticky=W))
  114.  
  115.  
  116.     def get_news(self):
  117.         """
  118.        Reterives the top 10 trending news from /r/news using PRAW
  119.        Returns the titles and url of each story stored in a dictionary
  120.        """
  121.         news_dict = {}
  122.         if self.sort == 'Hot':
  123.             self.submissions = news_program.get_subreddit(self.subreddit).get_hot(limit=10)
  124.         elif self.sort == 'Top':
  125.             self.submissions = news_program.get_subreddit(self.subreddit).get_top(limit=10)
  126.         elif self.sort == 'Rising':
  127.             self.submissions = news_program.get_subreddit(self.subreddit).get_rising(limit=10)
  128.         else:
  129.             self.submissions = news_program.get_subreddit(self.subreddit).get_new(limit=10)
  130.  
  131.         count = 1
  132.         for submission in self.submissions:
  133.             news_dict[count] = [submission.title, submission.url, submission, submission.likes]
  134.             count += 1
  135.         return news_dict
  136.  
  137.     def downvote(self, num):
  138.         """
  139.         Downvote the post
  140.        """
  141.         if self.d_buttons[num]['text'] == 'Downvote':
  142.             self.news_list[num + 1][2].downvote()
  143.             self.d_buttons[num].config(text='Gromit', bg='red')
  144.             self.u_buttons[num].config(text='Upvote', bg='light grey')
  145.         else:
  146.             self.d_buttons[num].config(text='Downvote', bg='light grey')
  147.             self.news_list[num + 1][2].clearvote()
  148.  
  149.     def upvote(self, num):
  150.         """
  151.         Upvote the post
  152.        """
  153.         if self.u_buttons[num]['text'] == 'Upvote':
  154.             self.news_list[num + 1][2].upvote()
  155.             self.u_buttons[num].config(text='Wallace', bg='dark green')
  156.             self.d_buttons[num].config(text='Downvote', bg='light grey')
  157.         else:
  158.             self.u_buttons[num].config(text='Upvote', bg='light grey')
  159.             self.news_list[num + 1][2].clearvote()
  160.  
  161.     def goto_source(self, num):
  162.         """
  163.        Opens the link in a web browser
  164.        """
  165.         webbrowser.open(self.news_list[num][1], new=1)
  166.         self.s_buttons[(num-1)].config(bg='light grey')
  167.  
  168. ################################################################################
  169. # Main code
  170.  
  171. header = RedditNewsGUIHeader()
  172. header.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement