Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 822b456

Browse files
Merge pull request avinashkranjan#124 from Yagueteiro/master
Created a voice assistant for reading news
2 parents 089d9c9 + a958368 commit 822b456

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

‎News Voice Assistant/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<h1> News Voice Assistant </h1>
2+
Hear your favourite news in your computer!
3+
4+
<h2> How-To </h2>
5+
<h4> Get API key </h4>
6+
7+
* Go to https://newsapi.org/register and sign up to get an API key
8+
9+
* Create a file called `api_key.txt` and paste it there
10+
11+
<h4> Setup </h4>
12+
13+
* Install the dependencies
14+
15+
`pip install -r requirements.txt`
16+
* Run the file
17+
18+
`python news_assistant.py`
19+
20+
* Set a topic...
21+
![example](https://raw.githubusercontent.com/Yagueteiro/Amazing-Python-Scripts/master/News%20Voice%20Assistant/example.png)
22+
* Et voilà!
23+
24+
<h2> Troubleshooting </h2>
25+
* You may see a lot of output related to the playback. If this happens, make sure you have installed the package "SimpleAudio"

‎News Voice Assistant/example.png

8.95 KB
Loading[フレーム]

‎News Voice Assistant/news_assistant.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import io
2+
import os
3+
import sys
4+
from contextlib import contextmanager
5+
6+
import newsapi
7+
from gtts import gTTS
8+
from pydub import AudioSegment
9+
from pydub.playback import play
10+
11+
more_news = True
12+
article_number = 0
13+
14+
15+
def fetch_news() -> dict:
16+
query = input("What do you want to hear about? ")
17+
return newsapi.get_everything(q=query)
18+
19+
20+
# To play audio text-to-speech during execution
21+
def speak(text: str):
22+
with io.BytesIO() as f:
23+
gTTS(text=text, lang='en').write_to_fp(f)
24+
f.seek(0)
25+
audio = AudioSegment.from_file(f, format="mp3")
26+
play(audio)
27+
28+
29+
if __name__ == '__main__':
30+
with open("api_key.txt", "r") as file:
31+
api_key = file.readline()
32+
newsapi = newsapi.NewsApiClient(api_key=api_key)
33+
34+
headlines = fetch_news()
35+
while more_news:
36+
article_number +=1
37+
try:
38+
print()
39+
article = headlines["articles"][article_number]
40+
print(article["title"], "-", article["source"]["name"])
41+
speak(article["title"])
42+
print(article["description"])
43+
speak(article["description"])
44+
print("Continue reading on this URL:", article["source"]["name"])
45+
except IndexError:
46+
speak("It looks as there are no more news on this topic. Why not search something else? ")
47+
except KeyboardInterrupt:
48+
break
49+
if article_number == 10:
50+
article_number = 0
51+
if input("Want to hear more news? [y/n] ") != "y":
52+
more_news = False

‎News Voice Assistant/requirements.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
beautifulsoup4==4.9.3
2+
certifi==2020年6月20日
3+
chardet==3.0.4
4+
click==7.1.2
5+
gTTS==2.1.1
6+
gTTS-token==1.1.3
7+
idna==2.10
8+
newsapi-python==0.1.1
9+
requests==2.24.0
10+
six==1.15.0
11+
soupsieve==2.0.1
12+
urllib3==1.25.11
13+
pydub==0.24.1
14+
simpleaudio==1.0.4

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /