diff --git a/.gitignore b/.gitignore index 77f6d90..67b357b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /*.egg /*.egg-info venv +*.gif diff --git a/bin/gnews-gui b/bin/gnews-gui new file mode 100644 index 0000000..0ab70c9 --- /dev/null +++ b/bin/gnews-gui @@ -0,0 +1,5 @@ +#!/usr/bin/env/python3 +import os +os.system('python3 ./gnewsclient/GUI/GUI.py') + + diff --git a/gnewsclient/GUI/GUI.py b/gnewsclient/GUI/GUI.py index cff1f31..3ed6b6f 100644 --- a/gnewsclient/GUI/GUI.py +++ b/gnewsclient/GUI/GUI.py @@ -2,6 +2,87 @@ from gnewsclient import gnewsclient from gnewsclient import utils from tkinter import ttk +from PIL import Image +import urllib.request +import webbrowser + +def gotolink(event, x): + webbrowser.open_new(x) + + +class resizingCanvas(Canvas): + def on_resize(self,event): + # determine the ratio of old width/height to new width/height + wscale = float(event.width)/self.width + hscale = float(event.height)/self.height + self.width = event.width + self.height = event.height + # resize the canvas + self.config(width=self.width, height=self.height) + # rescale all the objects tagged with the "all" tag + self.scale("all",0,0,wscale,hscale) + self.configure(scrollregion=self.bbox('all')) + + # def canvas_configure(self, event): + def __init__(self,parent,**kwargs): + Canvas.__init__(self,parent,**kwargs) + self.bind("", self.on_resize) + self.height = self.winfo_reqheight() + self.width = self.winfo_reqwidth() + + + +# function to load news in the east frame(level 1) +# see line numbers 189-234 for detailed explanation +def show_news(): + global east_frame, myscrollbar, frame,canvas, photo + east_frame.destroy() # destroy old news' frame + east_frame = Frame(root, relief=GROOVE, bd=1) # new frame for fresh news + east_frame.pack(side=RIGHT) + + canvas = resizingCanvas(east_frame) + + frame = Frame(canvas) + myscrollbar = Scrollbar(east_frame, orient="vertical", command=canvas.yview) + canvas.configure(yscrollcommand=myscrollbar.set) + myscrollbar.pack(side="right", fill="y") + + canvas.pack(side="left", padx=30, pady=30) + canvas.create_window((0, 0), window=frame, anchor='nw') + + nws = client.get_news() + + l = len(nws) + photo = {} + for i in range(l): + try: + imzlnk = nws[i]['img'] + imz = Image.open(urllib.request.urlopen(imzlnk)) + imz.save('file' + str(i) + '.gif') + f = Frame(frame) + lnk = nws[i]['link'] + ttle = nws[i]['title'] + photo[i] = PhotoImage(file='file' + str(i) + '.gif') + photolabel = Label(f, image=photo[i]).grid(row=0, column=0, rowspan=3, sticky=W, pady=10) + ttlelabel = Label(f, text=str(ttle), font=("Helvetica", 12)).grid(row=0, column=1, sticky=W, pady=10) + read_more = Label(f, text="Read more about this", fg="blue", cursor="hand2") + read_more.grid(row=1, column=1, sticky=W) + read_more.bind("", lambda event, link=str(lnk): gotolink(event, link)) + f.grid(column=1, row=i, sticky=W) + except AttributeError: + imzlnk = nws[i]['img'] + f = Frame(frame) + lnk = nws[i]['link'] + ttle = nws[i]['title'] + ttlelabel = Label(f, text=str(ttle), font=("Helvetica", 12)).grid(row=0, column=1, sticky=W) + read_more = Label(f, text="Read more about this", fg="blue", cursor="hand2") + read_more.grid(row=1, column=1, sticky=W) + read_more.bind("", lambda event, link=str(lnk): gotolink(event, link)) + f.pack() + return + + + # function to set attributes(language, location, topic), and then fetch news accordingly @@ -25,9 +106,7 @@ def news(): client.location = None update_status(client,status) - for i in client.get_news(): - print(i) - print("\n") + show_news() return @@ -36,7 +115,8 @@ def search(): global search_query,client,status client.query= search_query.get() update_status(client,status) - print(client.get_news()) + # print(client.get_news()) + show_news() return # helper function for status bar @@ -73,7 +153,7 @@ def update_status(client, status): search_button = Button(search_frame,text= "search", command=search).grid(row=1, column = 1) west_frame = Frame(root) -west_frame.pack(side=LEFT) +west_frame.pack(side=LEFT, padx=10) # frame for topic, location, and edition filters @@ -106,12 +186,56 @@ def update_status(client, status): location_query = StringVar() location = Entry(tle_frame, textvariable=location_query).grid(row=4, column=1,pady=50) - +# frame to enclose all the stuff related to show news(level 1) +east_frame=Frame(root,relief=GROOVE,bd=1) +east_frame.pack(side=RIGHT) + +# canvas which keeps the news related stuff(level 2) +canvas = resizingCanvas(east_frame) + +# the frame inside canvas which shows news and stuff(level 3) +frame = Frame(canvas) +# scrollbar so that the canvas can be scrolled(level 2) +myscrollbar=Scrollbar(east_frame, orient="vertical", command=canvas.yview) +canvas.configure(yscrollcommand=myscrollbar.set) +myscrollbar.pack(side="right",fill="y") + +canvas.pack(side="left", padx=30, pady=30) +canvas.create_window((0,0),window=frame,anchor='nw') + +# news list which has dictionaries for different news articles +nws = client.get_news() # has the default filtering parameters +l = len(nws) +photo = {} # dictionary which maps numbers with PhotoImage objects +for i in range(l): + try: + imzlnk = nws[i]['img'] + imz = Image.open(urllib.request.urlopen(imzlnk)) + imz.save('file'+str(i)+'.gif') + f = Frame(frame) # frame to display a news article(one frame per article)(level 4) + lnk = nws[i]['link'] + ttle = nws[i]['title'] + photo[i] = PhotoImage(file='file'+str(i)+'.gif') + photolabel = Label(f, image = photo[i]).grid(row=0, column=0, rowspan=3, sticky=W, pady=10) + ttlelabel = Label(f, text=str(ttle), font=("Helvetica", 12)).grid(row=0, column=1, sticky=W, pady=10, padx=5) + # ttlelabel.config() + read_more = Label(f, text="Read more about this", fg="blue", cursor="hand2") + read_more.grid(row=1, column=1, sticky=W, padx=5) + read_more.bind("", lambda event, link=str(lnk): gotolink(event, link)) + f.grid(column=1, row=i, sticky=W) + except AttributeError:# incase there is no photo found, no photo rendering done here + imzlnk = nws[i]['img'] + f = Frame(frame) + lnk = nws[i]['link'] + ttle = nws[i]['title'] + ttlelabel = Label(f, text=str(ttle), font=("Helvetica", 12)).grid(row=0, column=1, sticky=W) + read_more = Label(f, text="Read more about this", fg="blue", cursor="hand2") + read_more.grid(row=1, column=1, sticky=W) + read_more.bind("", lambda event, link=str(lnk): gotolink(event, link)) + f.pack() # button to fetch news getnews = Button(tle_frame, command=news,text = "Get News!").grid(row=6,column=0,columnspan=2, pady=25) - - - - +img = PhotoImage(file='logo.png') +root.tk.call('wm', 'iconphoto', root._w, img) root.mainloop() diff --git a/gnewsclient/GUI/logo.png b/gnewsclient/GUI/logo.png new file mode 100755 index 0000000..3110586 Binary files /dev/null and b/gnewsclient/GUI/logo.png differ diff --git a/logo.png b/logo.png new file mode 100755 index 0000000..3110586 Binary files /dev/null and b/logo.png differ diff --git a/setup.py b/setup.py index 6c856b8..70d44b8 100644 --- a/setup.py +++ b/setup.py @@ -24,6 +24,7 @@ def readme(): 'Topic :: Internet', ], keywords = 'google news feed python client', + scripts = ['bin/gnews-gui'], description = 'Python client for Google News feed.', long_description = readme(), url = 'http://github.com/nikhilkumarsingh/gnewsclient', @@ -38,4 +39,4 @@ def readme(): [console_scripts] gnews=gnewsclient.scripts.gnews:cli ''', - ) \ No newline at end of file + )