Skip to content

Commit 4c89e2b

Browse files
authored
Merge pull request #782 from KazushiR/speech_recognition_news
adding speech recognition news
2 parents dfef054 + 0d83bfe commit 4c89e2b

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

speech_recognition_news/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Speech News
2+
3+
## Execution
4+
5+
To execute the script, the dotenv and newspaper3 package will need to be downloaded. Documentation can be found below.
6+
- newspaper3: https://newspaper.readthedocs.io/en/latest/
7+
- dotenv: https://pypi.org/project/python-dotenv/
8+
9+
From there, you will need to go to https://newsapi.org/ and create an account and get the api key".
10+
11+
Documentation for this can be found per below.
12+
- Getting Started: https://newsapi.org/docs/get-started#search
13+
- Python: https://newsapi.org/docs/client-libraries/python
14+
15+
Once you have the required packages, execute the scipt and it should pull the latest news articles, summarize them and put it into a json format under the folder "news_summary".

speech_recognition_news/newsapi.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
import requests
3+
import json
4+
from dotenv import load_dotenv, find_dotenv
5+
from newspaper import Article
6+
from newspaper import Config
7+
from datetime import datetime
8+
9+
news_information = dict()
10+
11+
failed_port_connection = []
12+
13+
now = datetime.now()
14+
todays_date = now.strftime("%b %d, %Y")
15+
16+
17+
def get_top_news(news_information, todays_date):
18+
load_dotenv(find_dotenv(r"path to env variable"))
19+
news_api_key = os.getenv("news_api")
20+
params = (
21+
('country', 'us'),
22+
('apiKey', f'{news_api_key}'),
23+
)
24+
response = requests.get(
25+
"""https://newsapi.org/v2/top-headlines""",
26+
params=params)
27+
news_articles = response.json()['articles']
28+
for i in news_articles:
29+
user_agent = """Mozilla/5.0 (Macintosh;
30+
Intel Mac OS X 10.15; rv:78.0)
31+
Gecko/20100101 Firefox/78.0"""
32+
config = Config()
33+
config.browser_user_agent = user_agent
34+
config.request_timeout = 10
35+
article = Article(i['url'], config=config)
36+
print(i["url"])
37+
article.download()
38+
article.parse()
39+
article.nlp()
40+
summary = article.summary # noqa
41+
news_information[i["source"]["name"]] = {"title": i["title"], "summary": summary, "url": i["url"], "date": f"{todays_date}"} # noqa
42+
43+
44+
def json_file(news_information, todays_date):
45+
json_file = json.dumps(news_information, indent=4)
46+
with open(f"news_summary/news_summary {todays_date}.txt", "w") as outfile:
47+
outfile.write(json_file)
48+
outfile.close()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
newsapi-python==0.2.6
2+
newspaper3k==0.2.8

0 commit comments

Comments
 (0)