Skip to content

Commit 3dbb6b6

Browse files
authored
Merge pull request #808 from PranjalPandey/news_reader
News reader to read latest news from Npr news.
2 parents 3616168 + 0b825ed commit 3dbb6b6

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

auto_news_reader/README.md

+12
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

+51
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

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

auto_news_reader/read_npr_news.py

+52
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

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

0 commit comments

Comments
 (0)