In this article, we will learn how to create a Python script to read the latest news. We will fetch news from news API and after that, we will read news using pyttsx3.
Modules required :
pyttsx3 - pip install pyttsx3 requests - pip install requests
To get a API for news we will use newsapi.org. we will create account and take API key by clicking on get API button.
Step #1: Import modules needed
import pyttsx3 import requests import json import time |
Step #2: Setting up URL with API key, place your API key here.
'country = in&' 'apiKey =' ) url + = 'your_api_key_here' |
Step #3: Setting an engine for pyttsx3 for reading news.
engine = pyttsx3.init() |
Step #4: Setting up properties of our engine, means reading rate, volume, and sound of a voice.
rate = engine.getProperty( 'rate' ) engine.setProperty( 'rate' , rate + 10 ) volume = engine.getProperty( 'volume' ) engine.setProperty( 'volume' , volume - 0.60 ) sound = engine.getProperty ( 'voices' ); engine.setProperty( 'voice' , 'sound[1].id' ) |
Step #5: Trying to send request to get news. Here, engine.say() function is used to read news.
try : response = requests.get(url) except : engine.say( "can, t access link, plz check you internet " ) news = json.loads(response.text) |
for new in news[ 'articles' ]: print ( "##############################################################\n" ) print ( str (new[ 'title' ]), "\n\n" ) engine.say( str (new[ 'title' ])) print ( '______________________________________________________\n' ) engine.runAndWait() print ( str (new[ 'description' ]), "\n\n" ) engine.say( str (new[ 'description' ])) engine.runAndWait() print ( ".............................................................." ) time.sleep( 2 ) |
Now, everything is ready build a loop to read new articles.
Below is the complete Python implementation :
import pyttsx3 import requests import json import time 'country = in&' 'apiKey =' ) url + = 'your_api_key_here' engine = pyttsx3.init() rate = engine.getProperty( 'rate' ) engine.setProperty( 'rate' , rate + 10 ) volume = engine.getProperty( 'volume' ) engine.setProperty( 'volume' , volume - 0.60 ) sound = engine.getProperty ( 'voices' ); engine.setProperty( 'voice' , 'sound[1].id' ) try : response = requests.get(url) except : engine.say( "can, t access link, plz check you internet " ) news = json.loads(response.text) for new in news[ 'articles' ]: print ( "##############################################################\n" ) print ( str (new[ 'title' ]), "\n\n" ) engine.say( str (new[ 'title' ])) print ( '______________________________________________________\n' ) engine.runAndWait() print ( str (new[ 'description' ]), "\n\n" ) engine.say( str (new[ 'description' ])) engine.runAndWait() print ( ".............................................................." ) time.sleep( 2 ) |
Output:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.