Skip to content

Commit edfd454

Browse files
committed
News reader to read latest news from Npr news.
1 parent 3616168 commit edfd454

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

auto_news_reader/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<h1 align=center> Auto News Reader</h1>
2+
This bot scraps latest international news from www.npr.org and reads them aloud.
3+
4+
<h3 align=center>How to run</h3>
5+
- Create a virtual environment<br/>
6+
`python3 -m venv /path/to/new/virtual/environment`
7+
- Activate the virtual environment
8+
- Move to folder "auto_news_reader"
9+
- Install requirements from Requirements.txt<br/>
10+
`pip install -r Requirements.txt`
11+
- Run file "read_npr_news.py" and say "<b>news</b>" after it says "Python is listening".
12+
It will start reading the latest news from npr news.

auto_news_reader/Requirements.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
appdirs==1.4.4
2+
argcomplete==1.12.2
3+
arrow==1.2.1
4+
astroid==2.4.2
5+
async-generator==1.10
6+
attrs==21.2.0
7+
beautifulsoup4==4.10.0
8+
certifi==2020.12.5
9+
cffi==1.15.0
10+
charset-normalizer==2.0.7
11+
click==7.1.2
12+
colorama==0.4.3
13+
cryptography==35.0.0
14+
distlib==0.3.1
15+
filelock==3.0.12
16+
h11==0.12.0
17+
idna==3.3
18+
isort==5.5.1
19+
jaraco.context==4.1.1
20+
lazy-object-proxy==1.4.3
21+
mccabe==0.6.1
22+
more-itertools==8.10.0
23+
outcome==1.1.0
24+
packaging==20.8
25+
Pillow==8.0.1
26+
pipenv==2020.11.15
27+
pipx==0.15.6.0
28+
psycopg2==2.8.6
29+
pycparser==2.20
30+
pygame==2.0.2
31+
pylint==2.6.0
32+
pyOpenSSL==21.0.0
33+
pyparsing==2.4.7
34+
python-dateutil==2.8.2
35+
pytz==2020.4
36+
requests==2.26.0
37+
selenium==4.0.0
38+
six==1.15.0
39+
sniffio==1.2.0
40+
sortedcontainers==2.4.0
41+
soupsieve==2.2.1
42+
toml==0.10.1
43+
trio==0.19.0
44+
trio-websocket==0.9.2
45+
urllib3==1.26.7
46+
userpath==1.4.1
47+
virtualenv==20.3.1
48+
virtualenv-clone==0.5.4
49+
wrapt==1.12.1
50+
wsproto==1.0.0
51+
xmltodict==0.12.0

auto_news_reader/mySpeaker.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Import the platform module to identify your OS
2+
import platform
3+
# If you are using Windows, use pyttsx3 for text to speech
4+
if platform.system() == "Windows":
5+
import pyttsx3
6+
try:
7+
engine = pyttsx3.init()
8+
except ImportError:
9+
pass
10+
except RuntimeError:
11+
pass
12+
voices = engine.getProperty('voices')
13+
engine.setProperty('voice', voices[1].id)
14+
engine.setProperty('rate', 150)
15+
engine.setProperty('volume', 1.2)
16+
17+
def print_say(txt):
18+
print(txt)
19+
engine.say(txt)
20+
engine.runAndWait()
21+
# If you are using Mac or Linux, use gtts for text to speech
22+
if platform.system() == "Darwin" or platform.system() == "Linux":
23+
import os
24+
25+
def print_say(texts):
26+
print(texts)
27+
texts = texts.replace('"','')
28+
texts = texts.replace("'","")
29+
os.system(f'gtts-cli --nocheck "{texts}" | mpg123 -q -')

auto_news_reader/read_npr_news.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from io import BytesIO
2+
import requests
3+
import bs4
4+
from pygame import mixer
5+
# Import functions from the local package
6+
from auto_news_reader.voice_to_text import voice_to_text
7+
from auto_news_reader.mySpeaker import print_say
8+
9+
10+
def news_brief():
11+
# Locate the website for the NPR news brief
12+
url = 'https://www.npr.org/podcasts/500005/npr-news-now'
13+
# Convert the source code to a soup string
14+
response = requests.get(url)
15+
soup = bs4.BeautifulSoup(response.text, 'html.parser')
16+
# Locate the tag that contains the mp3 files
17+
casts = soup.findAll('a', {'class': 'audio-module-listen'})
18+
# Obtain the web link for the mp3 file
19+
cast = casts[0]['href']
20+
# Remove the unwanted components in the link
21+
mp3 = cast.find("?")
22+
mymp3 = cast[0:mp3]
23+
# Play the mp3 using the pygame module
24+
mymp3 = requests.get(mymp3)
25+
voice = BytesIO()
26+
voice.write(mymp3.content)
27+
voice.seek(0)
28+
mixer.init()
29+
mixer.music.load(voice)
30+
mixer.music.play()
31+
32+
33+
while True:
34+
print_say('Python is listening…')
35+
inp = voice_to_text().lower()
36+
print_say(f'you just said: {inp}')
37+
if inp == "stop listening":
38+
print_say('Goodbye!')
39+
break
40+
# If "news" in your voice command, play news brief
41+
elif "news" in inp:
42+
news_brief()
43+
print_say("Playing the latest News now!")
44+
# Python listens in the background
45+
while True:
46+
background = voice_to_text().lower()
47+
# Stops playing if you say "stop playing"
48+
if "stop playing" in background:
49+
print_say("Stopping the news.")
50+
mixer.music.stop()
51+
break
52+
continue

auto_news_reader/voice_to_text.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import speech_recognition as sr
2+
speech = sr.Recognizer()
3+
4+
5+
def voice_to_text():
6+
voice_input = ""
7+
with sr.Microphone() as source:
8+
speech.adjust_for_ambient_noise(source)
9+
try:
10+
audio = speech.listen(source)
11+
voice_input = speech.recognize_google(audio)
12+
except sr.UnknownValueError:
13+
pass
14+
except sr.RequestError:
15+
pass
16+
except sr.WaitTimeoutError:
17+
pass
18+
return voice_input

0 commit comments

Comments
 (0)