diff --git a/requirements.txt b/requirements.txt index d4e73e2..6291927 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,6 +28,8 @@ speechrecognition~=3.10.0 urllib3~=2.2.2 wheel~=0.41.2 wikipedia~=1.4.0 +feedparser~=6.0.11 +sgmllib3k~=1.0.0 # The following packages are considered to be unsafe in a requirements file: # pip diff --git a/src/assistant.py b/src/assistant.py index 6fff5ef..6ba3c62 100755 --- a/src/assistant.py +++ b/src/assistant.py @@ -16,6 +16,9 @@ from voice_interface import VoiceInterface LISTENING_ERROR = "Say that again please..." +MAX_FETCHED_HEADLINES = ( + 10 # Maximum number of news headlines to fetch when news function is called +) class Assistant: @@ -130,6 +133,9 @@ def execute_query(self, query: str) -> None: ) # Extract the city name just after the word 'of' commands.weather_reporter(self.__voice_interface, cities[0]) + elif "news" in query: + commands.fetch_news(self.__voice_interface, MAX_FETCHED_HEADLINES) + else: self.__voice_interface.speak("could not interpret the query") diff --git a/src/commands.py b/src/commands.py index bc2eaa1..e33643a 100644 --- a/src/commands.py +++ b/src/commands.py @@ -15,6 +15,7 @@ from datetime import datetime from subprocess import CalledProcessError, TimeoutExpired +import feedparser import googlesearch import pyautogui as pag import pygetwindow @@ -296,6 +297,37 @@ def simple_scroll(direction: str) -> None: print("Invalid direction") +def fetch_news(vi: VoiceInterface, max_fetched_headlines: int) -> None: + """ + Fetches and reads out the top 5 headlines from the Google News RSS feed. + + This function fetches news headlines from the Google News RSS feed (specific to India in English). + It then reads out the top 5 headlines using the provided VoiceInterface instance. If the feed fetch is successful, + it reads the headlines one by one. If the fetch fails, it informs the user that the news couldn't be fetched. + + Args: + vi (VoiceInterface): The VoiceInterface instance used to speak the news headlines. + + Raises: + requests.exceptions.RequestException: If there is an issue while fetching the RSS feed. + AttributeError: If the feed does not contain expected attributes or entries. + """ + + feed_url = "https://news.google.com/rss?hl=en-IN&gl=IN&ceid=IN:en" + + vi.speak("Fetching news from servers.") + feed = feedparser.parse(feed_url) + if feed.status == 200: + headlines_list = [] + for entry in feed.entries[:max_fetched_headlines]: + headlines_list.append((entry.title).split(" -")[0]) + vi.speak("Here are some recent news headlines.") + for headline in headlines_list: + vi.speak(headline) + else: + vi.speak("Failed to fetch the news.") + + def weather_reporter(vi: VoiceInterface, city_name: str) -> None: """ Fetches and reports the weather conditions for a given city.