Why I made it
I’m building a voice assistant and thought it would be great if it could deliver news updates.
What it does
It cleans up the user’s query, determines whether it refers to a news category or a specific search term, and then calls the NewsAPI to fetch the most recent matching headline. After receiving the response, it extracts and tidies the article’s title, author, source, and description before returning a conversational summary which the text-to-speech module of my voice assistant (which I haven't posted yet) can say out loud with ease.
Code
Here is the Python code:
import requests
import json5
def NewsConcerning(text):
""" Search for news concerning the text """
text = (
str(text)
.replace("news", "")
.replace("new", "")
.replace("for ", "")
.replace("concerning", "")
.replace("can ", "")
.replace("you ", "")
.replace("give ", "")
.replace("me ", "")
.replace("the ", "")
.replace("most ", "")
.replace("recent ", "")
.replace("in ", "")
.replace("category ", "")
.replace(" currently ", "")
.replace("current ", "")
.replace("now ", "")
.replace("what ", "")
.replace("whats ", "")
.replace(" is ", "")
.replace("about ", "")
.replace(" ", "")
.replace("today", "")
)
if "business" in text:
text = "category=business"
elif "entertainment" in text:
text = "category=entertainment"
elif "general" in text:
text = "category=general"
elif "health" in text:
text = "category=health"
elif "science" in text:
text = "category=science"
elif "sports" in text:
text = "category=sports"
elif "technology" in text:
text = "category=technology"
elif text == "": # Eg input has been fully stripped and nothing is left
text = "category=general"
else: # If specific text is queried:
text = "q=" + text
url = (
"https://newsapi.org/v2/top-headlines?"
+ text
+ "&sortBy=publishedAt&pageSize=1&apiKey=fc8307a1197745a68e9a4479056fedd8"
)
response = requests.get(url)
parsed_data = json5.loads(response.text)
#print(parsed_data)
try:
#print(text)
if "articles" in parsed_data and parsed_data["articles"]:
print("<ALL OK>")
first_article = parsed_data["articles"][0]
name = first_article["source"]["name"]
author = first_article["author"]
title = first_article["title"]
desc = first_article["description"]
title = (
title.replace(name, "")
.replace(" - ", " ")
.replace("LISTEN | ", "")
.replace(
" | Opinion",
". I have also come to notice how this is an opinion-based text.",
)
.replace(" | ", " ")
.replace("Watch Live: ", "live, ")
.replace("!!!!!!", ", followed by six exclamation marks")
.replace("!!!!!", ", followed by five exclamation marks")
.replace("!!!!", ", followed by four exclamation marks")
.replace("!!!", ", followed by three exclamation marks")
.replace("!!", ", followed by two exclamation marks")
.replace("\n", "")
)
if author:
if author.lower() != name.lower():
author = author.replace(",", "and")
author = " by " + author
title = title.replace(author, "")
else:
author = ""
else:
author = ", though I did not find who wrote it"
if desc == "":
desc = "Sadly, I could not find any description."
else:
content = desc.split("<p>") # In case description comes in HTML format
if len(content) > 1:
desc = content[1]
desc = desc.replace(""", "").replace(".com", "")
if len(name):
name = "on " + name
return f"\n\nHere is the recently published news I found {name}{author}:\nThe title to it is {title}... \n{desc}"
except IndexError:
text = text.replace("q=", "").replace("category=", "")
return f"I could not find anything concerning what you asked for. Mabe I misheard, were you asking about {text}? Try again if not."
except KeyError:
return "Whoops! Looks like you have been very interested in the news today! I'm sorry to say that my daily request limit, one hundred per day or fifty per twelve hours, has been reached."
i = input("Enter what you would ask of the voice assistant (eg: news concerning bitcoin): ")
print(NewsConcerning(i))
Critique request
Please, tell me anything that comes to mind.