From 88cffdc47239d0e98c67f8dcfa5f11c84e9f7e8a Mon Sep 17 00:00:00 2001 From: HarshitGourlariya Date: Sun, 16 Feb 2025 09:32:24 +0530 Subject: [PATCH 1/4] Add NewsHub_Python --- NewsHub_Python/Newshub.py | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 NewsHub_Python/Newshub.py diff --git a/NewsHub_Python/Newshub.py b/NewsHub_Python/Newshub.py new file mode 100644 index 00000000000..d55d7df3159 --- /dev/null +++ b/NewsHub_Python/Newshub.py @@ -0,0 +1,56 @@ +import tkinter as tk +import requests +from PIL import Image, ImageTk + +# Defining global variables +Content = "" +News_run = "" + +def news(): + global Content + ApiKey = "Your_Api_Key" # Get your API key + url = f"https://newsapi.org/v2/everything?q={Content}&from=2025-01-27&sortBy=popularity&apiKey={ApiKey}" + response = requests.get(url) + result = response.json() + # Create a formatted string from the result + news_str = "" + for article in result['articles']: + news_str += f"Title: {article['title']}\nDescription: {article['description']}\n\n" + return news_str + +def save(): + global Content, label + Content = str(search.get()) + news_content = news() + label.config(text=news_content) + +window = tk.Tk() +window.geometry("800x600") +window.title("News_App") + +# Label for displaying news +label = tk.Label(window, text="", wraplength=750, justify="left") +label.place(x=20, y=100) + +# Title label +title_label = tk.Label(window, text="NewsHub", justify="left", font=("Helvetica", 50)) +title_label.place(x=10, y=10) + +# Display image +img = Image.open("D:\\Downloads\\img.png") +photo_img = ImageTk.PhotoImage(img) +my_img = tk.Label(window, image=photo_img, justify="right") +my_img.place(x=850, y=0) + +# Keep a reference to the image to avoid garbage collection +photo_img.image = photo_img + +# Search entry field +search = tk.Entry(window, font=("Arial", 20)) +search.place(x=300, y=40) + +# Search button +find = tk.Button(window, borderwidth=3, cursor="hand2", text="Search", highlightbackground="black", command=save) +find.place(x=650, y=40) + +window.mainloop() \ No newline at end of file From 8222abbedce16986856883567caa60ae06d849fe Mon Sep 17 00:00:00 2001 From: HarshitGourlariya Date: Sun, 16 Feb 2025 09:46:01 +0530 Subject: [PATCH 2/4] Add Error Handling Cases --- NewsHub_Python/Newshub.py | 68 +++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/NewsHub_Python/Newshub.py b/NewsHub_Python/Newshub.py index d55d7df3159..f7d91e63abf 100644 --- a/NewsHub_Python/Newshub.py +++ b/NewsHub_Python/Newshub.py @@ -1,56 +1,62 @@ -import tkinter as tk -import requests +import tkinter as Tk from PIL import Image, ImageTk # Defining global variables -Content = "" -News_run = "" - -def news(): - global Content - ApiKey = "Your_Api_Key" # Get your API key - url = f"https://newsapi.org/v2/everything?q={Content}&from=2025-01-27&sortBy=popularity&apiKey={ApiKey}" - response = requests.get(url) - result = response.json() - # Create a formatted string from the result +content = "" +news_content = "" + +def fetch_news(): + global content + api_key = "Your_Api_Key" + url = f"https://newsapi.org/v2/everything?q={content}&from=2025-01-27&sortBy=popularity&apiKey={api_key}" + + try: + response = requests.get(url) + response.raise_for_status() + result = response.json() + except requests.exceptions.RequestException as e: + return f"An error occurred: {e}" + news_str = "" - for article in result['articles']: + for article in result.get('articles', []): news_str += f"Title: {article['title']}\nDescription: {article['description']}\n\n" return news_str def save(): - global Content, label - Content = str(search.get()) - news_content = news() - label.config(text=news_content) + global content, label + content = search.get() + news_str = fetch_news() + label.config(text=news_str) -window = tk.Tk() +window = Tk.Tk() window.geometry("800x600") -window.title("News_App") +window.title("News App") # Label for displaying news -label = tk.Label(window, text="", wraplength=750, justify="left") +label = Tk.Label(window, text="", wraplength=750, justify="left") label.place(x=20, y=100) # Title label -title_label = tk.Label(window, text="NewsHub", justify="left", font=("Helvetica", 50)) +title_label = Tk.Label(window, text="NewsHub", justify="left", font=("Helvetica", 50)) title_label.place(x=10, y=10) # Display image -img = Image.open("D:\\Downloads\\img.png") -photo_img = ImageTk.PhotoImage(img) -my_img = tk.Label(window, image=photo_img, justify="right") -my_img.place(x=850, y=0) - -# Keep a reference to the image to avoid garbage collection -photo_img.image = photo_img +try: + img = Image.open("D:\\Downloads\\img.png") + photo_img = ImageTk.PhotoImage(img) + my_img = Tk.Label(window, image=photo_img, justify="right") + my_img.place(x=850, y=0) + # Keep a reference to the image to avoid garbage collection + photo_img.image = photo_img +except Exception as e: + print(f"Error loading image: {e}") # Search entry field -search = tk.Entry(window, font=("Arial", 20)) +search = Tk.Entry(window, font=("Arial", 20)) search.place(x=300, y=40) # Search button -find = tk.Button(window, borderwidth=3, cursor="hand2", text="Search", highlightbackground="black", command=save) +find = Tk.Button(window, borderwidth=3, cursor="hand2", text="Search", highlightbackground="black", command=save) find.place(x=650, y=40) -window.mainloop() \ No newline at end of file +window.mainloop() From 4cd5bd9fa4c909e562d6dd468ab5b61395621540 Mon Sep 17 00:00:00 2001 From: HarshitGourlariya Date: Sun, 16 Feb 2025 09:50:17 +0530 Subject: [PATCH 3/4] Changed Image path --- NewsHub_Python/Newshub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NewsHub_Python/Newshub.py b/NewsHub_Python/Newshub.py index f7d91e63abf..75ed4283284 100644 --- a/NewsHub_Python/Newshub.py +++ b/NewsHub_Python/Newshub.py @@ -42,7 +42,7 @@ def save(): # Display image try: - img = Image.open("D:\\Downloads\\img.png") + img = Image.open("img.png") photo_img = ImageTk.PhotoImage(img) my_img = Tk.Label(window, image=photo_img, justify="right") my_img.place(x=850, y=0) From 1296abfcfe45ca1cb0b734b5faf23e8d8a0d2d8e Mon Sep 17 00:00:00 2001 From: HarshitGourlariya Date: Sun, 16 Feb 2025 09:55:23 +0530 Subject: [PATCH 4/4] Error Fixed --- NewsHub_Python/Newshub.py | 67 ++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/NewsHub_Python/Newshub.py b/NewsHub_Python/Newshub.py index 75ed4283284..bfbda4f358e 100644 --- a/NewsHub_Python/Newshub.py +++ b/NewsHub_Python/Newshub.py @@ -1,62 +1,57 @@ -import tkinter as Tk +import tkinter as tk +import requests from PIL import Image, ImageTk # Defining global variables -content = "" -news_content = "" - -def fetch_news(): - global content - api_key = "Your_Api_Key" - url = f"https://newsapi.org/v2/everything?q={content}&from=2025-01-27&sortBy=popularity&apiKey={api_key}" - - try: - response = requests.get(url) - response.raise_for_status() - result = response.json() - except requests.exceptions.RequestException as e: - return f"An error occurred: {e}" - +Content = "" +News_run = "" + +def news(): + global Content + ApiKey = "Your_Api_Key" + url = f"https://newsapi.org/v2/everything?q={Content}&from=2025-01-27&sortBy=popularity&apiKey={ApiKey}" + response = requests.get(url) + result = response.json() + # Create a formatted string from the result news_str = "" - for article in result.get('articles', []): + for article in result['articles']: news_str += f"Title: {article['title']}\nDescription: {article['description']}\n\n" return news_str def save(): - global content, label - content = search.get() - news_str = fetch_news() - label.config(text=news_str) + global Content, label + Content = str(search.get()) + news_content = news() + label.config(text=news_content) -window = Tk.Tk() +window = tk.Tk() window.geometry("800x600") -window.title("News App") +window.title("News_App") # Label for displaying news -label = Tk.Label(window, text="", wraplength=750, justify="left") +label = tk.Label(window, text="", wraplength=750, justify="left") label.place(x=20, y=100) # Title label -title_label = Tk.Label(window, text="NewsHub", justify="left", font=("Helvetica", 50)) +title_label = tk.Label(window, text="NewsHub", justify="left", font=("Helvetica", 50)) title_label.place(x=10, y=10) # Display image -try: - img = Image.open("img.png") - photo_img = ImageTk.PhotoImage(img) - my_img = Tk.Label(window, image=photo_img, justify="right") - my_img.place(x=850, y=0) - # Keep a reference to the image to avoid garbage collection - photo_img.image = photo_img -except Exception as e: - print(f"Error loading image: {e}") +img = Image.open("img.pg") +photo_img = ImageTk.PhotoImage(img) +my_img = tk.Label(window, image=photo_img, justify="right") +my_img.place(x=850, y=0) + +# Keep a reference to the image to avoid garbage collection +photo_img.image = photo_img # Search entry field -search = Tk.Entry(window, font=("Arial", 20)) +search = tk.Entry(window, font=("Arial", 20)) search.place(x=300, y=40) # Search button -find = Tk.Button(window, borderwidth=3, cursor="hand2", text="Search", highlightbackground="black", command=save) +find = tk.Button(window, borderwidth=3, cursor="hand2", text="Search", highlightbackground="black", command=save) find.place(x=650, y=40) window.mainloop() +