Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")

Expand Down
32 changes: 32 additions & 0 deletions src/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from datetime import datetime
from subprocess import CalledProcessError, TimeoutExpired

import feedparser
import googlesearch
import pyautogui as pag
import pygetwindow
Expand Down Expand Up @@ -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.
Expand Down