Search This Blog
Exploring the Wonders of Science, Technology, and Human Potential
Featured
- Get link
- X
- Other Apps
Python News Reader with Text-to-Speech Capability
1. `import requests`: This line imports the `requests` module, which allows Python to send HTTP requests easily.
2. `from gtts import gTTS`: This line imports the `gTTS` class from the `gtts` module. `gTTS` stands for "Google Text-to-Speech." It's a library that converts text to speech.
3. `import os`: This line imports the `os` module, which provides a way of using operating system dependent functionality.
4. `def get_news(api_key, country='us', category='general'):`
- This line defines a function named `get_news` which takes three parameters: `api_key`, `country`, and `category`. The default values for `country` and `category` are set to 'us' and 'general' respectively.
- Inside the function, it constructs a URL for fetching news from the News API, sends a request to the API with the specified parameters, and returns the JSON response containing news articles.
5. `def display_news(articles):`
- This line defines a function named `display_news` which takes a list of news articles (`articles`) as input.
- Inside the function, it iterates over each news article, printing its title, author, description, and URL. It also uses the `text_to_speech` function to convert the title and description of each article to speech.
- After printing each article, it waits for the user to press Enter before displaying the next article.
6. `def text_to_speech(text):`
- This line defines a function named `text_to_speech` which takes a text string (`text`) as input.
- Inside the function, it creates a `gTTS` object with the provided text and language ('en' for English), saves the generated speech as an MP3 file named 'temp.mp3', and then plays the MP3 file using the `os.system` function.
7. `if __name__ == "__main__":`
- This line checks if the script is being run directly by the Python interpreter (as opposed to being imported as a module into another script).
8. `api_key = 'your api`:
- This line assigns the News API key to the variable `api_key`.
9. `news = get_news(api_key, country='us', category='general')`:
- This line calls the `get_news` function with the specified parameters ('us' for country and 'general' for category) and assigns the result to the variable `news`.
10. `if news:`
- This line checks if `news` contains any articles (i.e., if it's not None or an empty list).
11. `display_news(news)`:
- This line calls the `display_news` function with the retrieved news articles as input. If there are articles, they will be displayed and read aloud using text-to-speech. If there are no articles, a message indicating there is no news to display will be printed.
- Get link
- X
- Other Apps
Popular Posts
What If India Loses this mindset of Reusing Things?
- Get link
- X
- Other Apps
Polar Bear is Suffering to Find Land Here is Why?
- Get link
- X
- Other Apps
Smarter move through technology revolution
- Get link
- X
- Other Apps
The Role of UX Design in Evolving Technology
- Get link
- X
- Other Apps
Comments
Post a Comment